Sign in to follow this  
Eldyn

Need help with directional movement

Recommended Posts

Eldyn    100
I am having a problem with my sprite's movement. My game is purely 2D. I am using 6-way movement (N, NE, NW, S, SE, SW). The code I have works fine, except that the NPCs will tend to "slide", or face the wrong direction at some points while moving. I've tried to manipulate it several different ways, but can't get sprites to follow the strict directional movement I want them to. For example, sometimes while moving SW, they will face SW properly, other times they may be moving SW but facing S. Can someone offer suggestions that I may have not thought of, or code example that might work better? Thank you.

Share this post


Link to post
Share on other sites
jyk    2094
This sounds like more of a programming problem than an AI problem.

I doubt that we'll be able to tell you what the problem is based on the information you've provided. Presumably the visual orientations of your sprites are determined by the particular sprite that is being rendered at any given time, or by an assigned transform. If a sprite is moving SW but is facing S, then clearly the visual representation is not synced properly with the character's motion. The syncing of one with the other is a programming detail, more or less, so it's difficult to say why it's not working without seeing the actual code.

Have you tried using the debugger (or debug output of some sort) to determine where and why the visual representation is getting out of sync?

Share this post


Link to post
Share on other sites
Eldyn    100
You're probably right, this may be a topic better suited to general programming. Not sure if I would do better to repost there, or request a move.

I'm not at home right now, or I would provide the code for it.

It's along the lines of the typical

If npc.x < destination.x and npc.y < destination.y then
npc.x += 1
npc.y += 1
npc.direction = 3 (facing SE)

The thing is, I think that I may have confused the program by doing this because if they are moving SE (or SW for that matter), they are still moving in a S direction. So even when moving SW, sometimes if their goal destination may not be toward the direct S at all, the correct frame just hasn't displayed, because it is in fact true that they are moving toward the S, even though not directly S.

Share this post


Link to post
Share on other sites
IADaveMark    3731
Quote:
Original post by jyk
This sounds like more of a programming problem than an AI problem.

This.

Quote:
The syncing of one with the other is a programming detail, more or less, so it's difficult to say why it's not working without seeing the actual code.

This.

Quote:
Have you tried using the debugger (or debug output of some sort) to determine where and why the visual representation is getting out of sync?

And always this. (Not enough people actually bother to step through their code.)

Moving to general programming.

Share this post


Link to post
Share on other sites
jolid    143
First, I'm assuming your Y isn't backwards and the SE was just a typo (+Y = Up/N, right?).

Second, from your post, I'm guessing you've got something like:

If npc.x > destination.x and npc.y < destination.y then
npc.x -= 1
npc.y += 1
npc.direction = 2 // (facing NW)

If npc.x < destination.x and npc.y < destination.y then
npc.x += 1
npc.y += 1
npc.direction = 3 // (facing NE)

If npc.x > destination.x and npc.y > destination.y then
npc.x -= 1
npc.y -= 1
npc.direction = 4 // (facing SW)

// Etc, etc for all 6+1 directions (+1=not moving)
Doing it like that, your NPCs are facing in whatever direction the last one of those IFs triggers. npc.direction = S is probably lower and overriding npc.direction = SE. Assuming your coords/math/logic aren't goofy and causing the problem (likely), try separating the X/Y movement logic:

// reset npc.direction=0, calculate each iteration
if npc.y != destination.y then
if npc.y < destination.y then
npc.y += 1
npc.direction = 1 // (facing N)
if npc.y > destination.y then
npc.y -= 1
npc.direction = 2 // (facing S)
if npc.x != destination.x then
if npc.x < destination.x then
npc.x += 1
npc.direction += 3 // (3 facing E, 4 facing NE, 5 facing SE)
if npc.x > destination.x then
npc.x -= 1
npc.direction += 6 // (6 facing W, 7 facing NW, 8 facing SW)
Or something like that. Hope that helps.

Whoops, forgot you were only using 6 directions. Set npc.basedirection=(1,2) initially and set npc.direction = npc.basedirection+npc.directionmodifier, where directionmodifier is determined by (lack of) X movement. I'm betting you don't need psuedocode for that!

Share this post


Link to post
Share on other sites
Eldyn    100
Yes, sorry, I did make a mistake in my example above. My code is actually correct for the direction.

I did make an attempt at separating the X and Y logic, but seeing you suggest it makes me think I gave up too quickly on that method when I couldn't get it to work correctly the way I did it.

I can't wait to get home from work to work on it again. It'll work out I'm sure. Thank you for the help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this