Handling Character Animation & Movement?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Handling Character Animation & Movement?

Post by dandymcgee »

Hey guys, I've been messing around with this little 2D RPG style project for ages now and I just can't seem to get it looking quite like I want.
I've been thinking about how to go about giving each player the ability to move, animate, and be followed by a scrolling camera in my world.

NOTE: Most of the following is pseudo-code to represent the methods used in their simplest forms.

The first thing I had set up involved the character's movement being dependent on its animation.

Code: Select all

case UP:
                character.y -= walkHeight / totalFrames;
This worked fine for some time. Then I added a camera.

Code: Select all

case UP:
                character.y -= walkHeight / totalFrames;
                camera.y -= character.y;
And of course the camera looked jerky because it's moving ~10.5 pixels per update. So I changed it a bit.

Code: Select all

case UP:
                character.y -= tileHeight / totalFrames;

Code: Select all

if( camera.y > character.y - (camera.h/2 + character.h/2) ){
                    camera.y -= speed;
The camera could now follow the character at its own pace. This produced really jerky movement, as the character was only center screen half of the time.
If I slowed the camera down the animation would look okay, as well as a smooth camera, but I didn't really like the camera following so far behind.
So I changed character movement to be no longer based on animation.

Code: Select all

case UP:
                    character.y -= speed;

Code: Select all

camera.speed = charToFollow.speed;
if( camera.y > character.y - (camera.h/2 + character.h/2) ){
                    camera.y -= camera.speed;
This looked really nice as far as scrolling, but since the character's speed was no longer based on animation, the animation would run on it's own time and be completely independent from where the character was (if the character was moving at all, the animation would be updating at a constant speed).
The issue was mostly due to my tilebased movement, where the character would stop moving at the center of the next tile unless it found input still in which case it would carry on. When moving more than one tile execution went something like: character.walking is set to false, animation is stopped, input is checked, character walking is set to true, animation is started again. This constant start/stop cause jerky animation.
At this point I decide let's just forget tile-based movement and let the player go where he pleases.

This works beautifully, everything is smooth and constant until you let up on the key. But now I have to rewrite the entire collision detection portion of the engine due to no longer using tile-based movement (keep track of which tile character is on, check a single tile in front of him in the direction of travel). With the added possibility of standing on four tiles at a time, I have to check every tile's collision in the direction of travel that collide with the character's width or height. While this wouldn't be all that hard to change, it would also mess up my object interaction system because it wouldn't know which object you're trying to interact with if you're colliding with two at once.

Anyways.. I've just been playing around with a bunch of ideas and this project isn't anything real important, just something to spend some time on. I'd like to know how you guys have handled or would handle movement, animation, scrolling, and collision in an RPG-style environment in comparison to what I've tried.
Constructive criticism is always welcome, so feel free to tell me how horrifying my attempts have been. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Handling Character Animation & Movement?

Post by wearymemory »

Have you thought about checking only for the dominating tile that the player is on:

Code: Select all

int x = (player.x / walkWidth) * walkWidth
int y = (player.y / walkHeight) * walkHeight
...
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Handling Character Animation & Movement?

Post by dandymcgee »

wearymemory wrote:Have you thought about checking only for the dominating tile that the player is on:

Code: Select all

int x = (player.x / walkWidth) * walkWidth
int y = (player.y / walkHeight) * walkHeight
...
Yeah, but collision still has to check all of the object you're colliding with (even if just a little bit) and decide which one to act on. I guess I could just respond to the object the player is colliding with most and just pick one if he's directly in the middle.

I wasn't really asking for help as much as just getting feedback on how others have handled interaction between animation, movement, and collision in their own project.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Handling Character Animation & Movement?

Post by wearymemory »

dandymcgee wrote:I wasn't really asking for help as much as just getting feedback on how others have handled interaction between animation, movement, and collision in their own project.
I wouldn't suggest it if I hadn't used it myself.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Handling Character Animation & Movement?

Post by dandymcgee »

wearymemory wrote: I wouldn't suggest it if I hadn't used it myself.
Sorry, it was just so short and simple. That might work for now though. :mrgreen:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Handling Character Animation & Movement?

Post by Bakkon »

I'm not sure how the identity property solves anything.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Handling Character Animation & Movement?

Post by dandymcgee »

Bakkon wrote:I'm not sure how the identity property solves anything.
Bakkon wrote:I'm not sure how the identity property solves anything.
Integers get rounded down when you divide by anything that yields a remainder. While his equation represents the identity property in mathematics, it does not in programming (provided you're using int not float).

Regardless, I'm still having the same issue. While that equation would tell me which object I'm colliding with most, it doesn't allow me to check for all collisions. Since movement was tile-based I'm only checking the collision status of the tile next to the one I'm standing on in the direction of travel. This used to only ever be one tile, but now it can be up to four (If the player is equal to or smaller than the tiles, otherwise it could be more).

I'm still curious how others have handled collision and animation in these cases, and how to handle collision in a world without tile-based character movement. Would I have to redesign my collision so that rather than checking the collision status of the single tile being walking towards, to some sort of proximity list collision checking against every object in the area?

What would be the easiest way to decide which tiles on the map the character is about to collide with?

EDIT: Nevermind, I'm all set for now. My collision algorithms were all messed up. I haven't quite fixed it yet, but now that I know what's up it should be much easier to solve. Feel free to post your ideas anyways though.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply