# constrain sprite to path

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

## 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 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).
------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 on other sites
Quote:
 Original post by mthickeI 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.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 17
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631758
• Total Posts
3002163
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!