Limiting movement of Object to within a certain area

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
fallingbrickwork
Posts: 52
Joined: Sun Jun 22, 2008 4:09 pm
Contact:

Limiting movement of Object to within a certain area

Post by fallingbrickwork »

I have an object that is positioned by the world coords of the mouse cursor. I also have a player model.

Basically, what i'm trying to achieve is...

i want to only be able to move the mouse/cursor object a certain distance from the player model... if the mouse gets moved further than this, the cursor object gets limited to a certain radius from the player object. If that makes sense... Not sure of the maths to do this.

Any pointers greatly received...

Thanks in advance,
Matt.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Break it down into simpler steps that you do understand. Imagine that you have two objects A, and B and that you want B to never get further than R units away from A. How do you do this? What information do you need to be able to solve the problem?

It seems to me that you need the position of A, the position of B and the maximum distance between the objects. You should be able to use this information to find the distance from A to B. Subtracting the position of B from the position of A gets a vector that describes the direction and distance from A to B. We'll call this vector V. The length of V is the distance between A and B. If this distance is less than or equal to R, then B is close enough to A, right?

What if B is further from A than allowed? You probably want to snap B back to a position that is within the desired distance from A. If you get the direction of the V, and multiply it by R, that will get you the position that is as R units from A, but in the direction of B. That is probably a pretty good place to put B, right?

Once you've got that code working, the next step would be to snap the mouse cursor back to a position that keeps at a screen position that is within the predetermined radius distance. If B gets to far away from A, and you snap it back, you'd snap the mouse cursor back to the new position of B in screen space. The collision manager has code to get the screen position of a node given its world position.

Travis
Last edited by vitek on Mon Sep 28, 2009 10:44 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9720
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Subtract the position from the mouse world coordinates from the position of your character, save the result in another vector and use the length of the resulting vector to find the distance. You can use getLength() in irr::vector3d for that. Using setLength you can set it to your maximal length then if the length was above that.

Not really sure if that was what you wanted already...
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
fallingbrickwork
Posts: 52
Joined: Sun Jun 22, 2008 4:09 pm
Contact:

Post by fallingbrickwork »

Many thanks for the above, both reply have given me a way forward.

Matt.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

CuteAlien wrote:...Using setLength you can set it to your maximal length then if the length was above that.
Ohhhh. I totally forgot about setLength(). Good tip.
Post Reply