Sign in to follow this  

Simple 2D game question

This topic is 2839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to figure out how to make an actor in a 2D tiled game take a direct-looking path to a target, but I can't seem to figure it out. To explain... Right now, the movement code is basic as can be and looks like this:
void Actor::walk()
{
	if ( dest_y < actor_y ) { --actor_y; }
	if ( dest_y > actor_y ) { ++actor_y; }
	if ( dest_x < actor_x ) { --actor_x; }
	if ( dest_x > actor_x ) { ++actor_x; }
}

Which produces a movement pattern like this ('A' is the actor, 'D' is its destination): current Now what I'd prefer is for the actor to take a more natural-looking path, where it walks the same number of tiles, but doesn't go diagonally and then straight. Like this: desired However I'm clueless as to how to make the actor do that. v[headshake]v Suggestions?

Share this post


Link to post
Share on other sites
Two suggestions:

1. Use a pathfinding algorithm like A* or Djikstra. Pros: accuracy, obstacle avoidance. Cons: more expensive cpu-wise, more complex to implement, although you can look for libraries that already have this implemented.

2. Use trigonometry. Use a reference line, like the horizontal axis. Then calculate the angle between the reference line and the line formed thru points A and D. Then you will have some movement magnitude, let's call it m.

Then,

x += m * cos ( angle ) and y += m * sin ( angle ). This will move the actor in a straight, crow's flight line, but does not account for obstacles, yet it is simpler than something like A*.

Share this post


Link to post
Share on other sites
I would recommend learning about 2D vector maths to solve this kind of thing.

It can look a bit intimidating at first but it is actually pretty simple and I would say one of the most essential tools in a 2D game developers mental toolkit.

Once you've got your head round it, you will never look back.

Share this post


Link to post
Share on other sites
Thanks all for your thoughts about this. Having read Zahlman's post, and figured out the vector math bits, I decided to go with the idea of storing an 'accumulator' of movement as part of each actor's data.

So, I assigned each actor a floating-point X and Y co-ordinate as well as integer X and Y co-ordinates, and each time a move is made, I would take the vertical and horizontal components of the motion, add them to the floats, and make the integer co-ordinates equal to the floats rounded to the nearest integer.

This works, pretty much, except for one issue, which is that even if a actor is moving towards something which is (for example) a long way to the right, and only a few squares above... every now and then the floating-point addition will add, say, +0.9 to X, and +0.1 to Y, and the Y-increment will trigger an upward move but the X-increment won't trigger a sideways move.

What happens in this case is that the actor will make a move straight up, even when the target is almost directly to its right, which kind of makes the pathing look weird:

im on a boat

The "!" represents the undesirable move here, obviously. It happens more frequently the closer to diagonal the straight-line path is. I'd like to stop this from happening as, again, it appears to be an unnatural path for the actor to take, but I'm not sure how to modify my implementation to effect that.

Share this post


Link to post
Share on other sites

This topic is 2839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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