Restrict movement within a radius

Posted by Phil on Game Development See other posts from Game Development or by Phil
Published on 2011-03-06T16:55:22Z Indexed on 2011/03/07 0:18 UTC
Read the original article Hit count: 215

Filed under:
|

I asked a similar question recently but now I think I know more about what I really want to know. I can answer my own question if I get to understand this bit.

I have a situation where a sprite's center point needs to be constrained within a certain boundary in 2d space. The boundary is circular so the sprite is constrained within a radius. This radius is defined as a distance from the center of a certain point.

I know the position of the center point and I can track the center position of the sprite.

This is the code to detect the distance:

    float distance = Vector2.Distance(centerPosition, spritePosition));
    if (distance > allowedDistance) {

    }

The positions can be wherever on the grid, they are not described as in between -1 or 1. So basically the detecting code works, it only prints when the sprite is outside of it's boundary I just don't know what to do when it oversteps. Please explain any math used as I really want to understand what you're thinking to be able to elaborate on it myself.

© Game Development or respective owner

Related posts about c#

Related posts about math