Sign in to follow this  
mthicke

constrain sprite to path

Recommended Posts

I have a sprite that is controlled by the user, I would like it’s movement to be constrained to a polyline path – the object would be able to move freely but not leave the path. The path would be a series of straight line segments input by the user and would cross itself. I’m not sure how to approach this – it would be easy enough if the object was always centered on the path but I would like it to be able to move freely though never off the path.

Share this post


Link to post
Share on other sites
So, each line segment has 1 to n legal exit segments, and each path has a radius of free movement?

Would something like this work?

1) Assign sprite to a starting segment

2) When the sprite moves, check to see if it's movement takes it more than the radius from the path (find closest point on the current line segment, check if distance > than threshold).
---If in radius, do nothing
---If out of radius:
------a) Check if the sprite is a legal distance from all legal exit segments, if so, assign the sprite to the segment it is closest to.
------b) Otherwise, reverse the sprite's last movement until it is just inside the threshold of the current path





Share this post


Link to post
Share on other sites
Quote:
Original post by mthicke
I have a sprite that is controlled by the user, I would like it’s movement to be constrained to a polyline path – the object would be able to move freely but not leave the path.

If the sprite travels along a line, you can view that line as the (normalized) velocity of the sprite (in two directions). You can travel along the line by taking the current position and add some constant speed multiplied by that 'velocity'. Make sure a sprite cannot 'drive off' the line segment by checking the end points and applying the resulting speed to the next segment (and its direction).
Quote:
The path would be a series of straight line segments input by the user and would cross itself.

The only problem here is to determine whether the sprite is exactly at the center of a crossing, since a (mathematical) line has no thickness. I suppose you can consider introducing a margin of error, depending how user input is handled.
Quote:
I’m not sure how to approach this – it would be easy enough if the object was always centered on the path but I would like it to be able to move freely though never off the path.

I'm not sure what you mean by "centered on a path". If it means that a sprite doesn't have to follow a line segment exactly but can wonder off a bit, like on a road (a line with a certain thickness), you have to limit its movement by making sure that it doesn't exceed a certain minimum distance to the line.

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