Practical game animation? - animation

I understand that game development involves a 'game loop', in which the scene is drawn. This game loop either runs as fast as possible (no timer, frame animation) or is called based on a timer (timer animation). I'm curious as to how advanced animation (something beyond just simple geometry) is actually done, considering how many frames might take place in say, a character walking. Obviously the programmer has not hard-coded in the position of the character's foot at every frame of the animation, so I'm wondering how this would actually be done?
If any clarification of the question is necessary, I'll be happy to fill more in.

Related

Skeletal animations in C# SFML 2D

I'm completely green when it comes to skeletal animations in 2D. I create a platform game and I got graphics for it - body parts from which to stick the character. My problem is that I have absolutely no idea how to go about it. The question is: How do you implement animations in the game?
I could do it frame by frame from previously glued body parts, but I need the character to look at the mouse cursor, so it falls off. I am asking for some ideas.
About your problem, if you want all parts of your character look at the mouse, you should rotate your character, if just a part (like character's head) you need to rotate the head.
I could give you some methods to implement animation that I known
Draw animation frame by frame: simple, easy to approach and handle but hard to scale up.
Separate character into many parts and each part has a unique animation: flexible, good for scale up but hard to handle due to you must ensure all parts are stick together in a right way (from scratch without tools or engine). I think this video may help you to know more the way their create skeletal animation in Unity (same as Spine).
Hope it help.

Should I render all the frames every secound?

I'm programming a 2D game with HTML5 canvas. so it won't take a lot of time for rendering all the objects and frames.
I want to ask about both 2D and 3D games.
Suppose that I made a change or more on one of the objects.should I render all the objects when I only need to redraw that object? is rendering all objects and frames is the only option? and if the game was 3D, won't rendering take a lot of time? specially on slow internet connections
So [performance] depends only on the browser and computer of the user.
If you are making a game which is played simultaneously across several computers then the connection speed is important. Otherwise, the connection speed is mostly irrelevant (except as it relates to initially downloading your game resources). A better CPU/GPU, memory on the local computer will most affect your app performance.
About performance
In theory, you get better performance if you redraw only those game items which have changed since the last frame.
In practice, redrawing one game item often requires redrawing multiple other game items. You might want to redraw the player who moved, but in doing so you must also redraw the background plus any other game items colliding with the player.
As a result, it's often better to clear the entire canvas and redraw all the game items in their current position. So the unsatisfying answer to your question is that you should test your unique game app to determine if you can efficiently redraw just changed items or if a complete redrawing is faster.

Player-Sprite with hat/robe/staff for each direction?

Some friends and I started to create a game, in which a mage will be in the center of the screen, being able to run in all 4 directions (as it is a 2D game)+diagonals, so 8 directions total (so the level below the player is moving).
The mage should wear a hat, a robe, and a staff for now, of which there should be a hole lot more than just one.
Additionally the mage should have a walking animation of let's say 3 pictures.
So overall it would be:
8(directions)*3(walking animation)*k(hats)*n(robes)*m(staffs) = 24*k*n*m sprites
(Which are currently BufferedImages as PNG with alphachannel drawn on a canvas), which is like waaaay too much for my designer (because we want a hole lot of staffs, robes and hats, maybe later even add boots and stuff)
So my idea was to make a "naked" playersprite and have separate sprites for staffs, robes and hats, which I then just render on top of the player.
Is that a good idea, or am I missing anything that would make all that a hole lot easier?
Combining sprites to draw the player is the way to go. You can reduce the amount of work more if you sacrifice some details.
You can mirror the animation for left and right. The weapon is always in the players hand facing you (instead of e.g. always in the player's right hand)
You can omit diagonal walking animations. Just reuse the horizontal or vertical animations.
Some games even omit the vertical walking animations, meaning you always see the player from the side even though you can walk up. It may interferes with the game mechanics if you can't attack up or down.
If the sprites are high res enough, you can keep static items as one single sprite and move/rotate them with the player's animation. So every weapon (or weapon type) has the same animation for walking, attacking etc. Probably doesn't look to good in retro style games because it won't be pixel perfect.
Reuse sprites. Not every robe needs a new design, just change some colors.
If you plan on finishing the game, detailed sprites and animations should be the last thing you implement. Make an ugly prototype first and if it is fun, then you can still add more details and make it look good.

Writing an efficient game-loop

I have recently asked a question here about programming a game so it will be efficient and won't lag.
I have made a game that lags like hell. I suspect that the way that I programmed it's game-loop, could be the source of the problem.
I want to describe to you in general the situation in my game, and then present my way of programming it's game loop.
It's a game for two players. Each player controls a tank. Each tank can shoot missiles. Each tank can collect 'gifts' from the field.
Each missile of tank1 can collide with tank2, and each missile of tank2 can collide with tank1.
Each missile can collide with a boundary of the screen.
Each tank can collide with the other tank.
Each tank can collide with 'gifts'.
All missiles constantly move.
Tanks move when specific buttons on the keyboard are pushed.
My game loop (Each stage is inside a method called from the game loop):
Loop through all the missiles on the screen, and update their location (move them).
Loop through all of tank1's missiles, and for every one check if collides with tank2.
Loop through all of tank2's missiles, and for every one check if collide with tank1.
Loop through all the missiles on the screen, and check if collide with screen boundaries.
Check if specific keys are pressed on the keyboard. If so, move tanks.
Check if the two tanks collide.
Loop through all of the 4 'gifts' on the screen, and check if they touch a tank.
Questions:
This game-loop is probably inefficient. How inefficient is it? A little, or a lot?
How can I improve the game loop and/or the way it's methods work? How can I achieve the same thing, more efficiently?
How likely is it for a game-loop to be the main cause for poor performance level? How common is it in games for this to be the source of the problem?
Appreciate your help.
I wouldn't necessarily say it's inefficient since you're just giving us the general idea of what might be done in several other games. It sounds like all you're doing is moving objects and checking for collisions, which I'm assuming uses AABB collision detecting. Nothing really expensive here. Of course it has room for improvement. See #2.
You're probably getting input more than once, so what you could do is get the input at the beginning of each frame and store it in some way. I don't know what form of input you're using, but if you're using a keyboard for example, you'd get and store the state of the WASD keys and check those states when handling tank input.
The most notable optimization you could make is you loop through every missile 3 times. The first time is to move them, the second to check their collisions, and the third time to check if they are outside the screen. In one loop, you can move them, check if they're outside of the screen, and then check if they collide with the opposing tank. I recommend doing it in this order, because if the bullet does happen to move outside the screen you can avoid doing an unnecessary collision check.
Finally, you only have to check if the two tanks collide when they move, and you only have to do it once. If you need it so something happens for every frame the two tanks are on top of each other, you can store the state of the collision and you only need to update it again when one tank moves.
It really depends. You're probably locking the frame rate by sleeping, so you could be sleeping for too long. Try to remove your method of frame rate limiting and seeing what happens.

Corona SDK performance. Animation or Sprite sheet

General Corona SDK question, what would get better performance having a runtime event listener on enterFrame or a sprite sheet. So essentially what i have is:
local function animate(e)
star.rotation = star.rotation +3;
end
Runtime:addEventListener ("enterFrame", animate);
return star;
Would this get better performance than having a sprite sheet that has the automation on loop. Both essentially go in a loop indefinitely (or until an action happens).
It's really a trade off. Your sprite is going to take up more memory, but it's probably faster to flip frames than it is to calculate the new angle and rotate the image. But it's going to be a pretty trivial difference.

Resources