• 15
• 15
• 11
• 9
• 10

# Moving sphere to an exact point

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

## Recommended Posts

Hi there. I'm currently stuck trying to implement something that sounds fairly simple. Basically, what I want to happen, is when two spheres collide (one is stationary), the other one moves into the exact position of the stationary sphere. At the moment my collision detection basically detects a hit when the spheres radii intersect, which is fine. What I want to happen is the movable sphere then moves into position of the second sphere (with a check to ensure it does this) Maybe this picture illustrates the problem a bit further: - Now I've presuming you have to calculate some sort of difference vector, like so
Vector3 diff = ball_two.position - ball_one.position;


This usually outputs a vector like: -
Quote:
 diff = (0, 0, 0.560001)
My question is, how do I reduce that vector to 0 so both points are aligned exactly? Thanks for any help. EDIT: And to clarify, once the balls have collided, I've implemented a "already collided" boolean variable that stops it from continuing to check for collisions. EDIT #2: further clarification! I want the ball to move into the new position on a gradual basis, so it moves towards the point until they are both aligned.

##### Share on other sites
can't you just do

ball_one.position = ball_two.position;

##### Share on other sites
Whoops, I was meant to clarify that, I want the ball to move to the point on a gradual basis until they're both aligned, so there's a smooth transition

##### Share on other sites
Ah, since you want gradual movement, just normalize your difference vector, scale it by the speed you want to move at, and add it to the ball_one vector, like so:

Vector diff =  ball_two.position - ball_one.position;diff.Normalize();ball_one.position = ball_one.position + diff * 0.1; // adjust the constant to change the speed

##### Share on other sites
Thanks BlodBath, I think something might be going wrong though, it's probably something to do with my checking code.

	Vector3 diff;int flag = 0;while(flag==0){    if(ball_one.position == ball_two.position) //check whether they're aligned    {        flag = 1;    }    else    {	diff = ball_two.position - ball_one.position;	diff.normalise();	diff.print("Difference");	ball_one.position = ball_one.position + diff * 0.1;    }}

The problem is, it does something that I guess should work, but when it hits a certain point the program just crashes, throwing out the following difference vector

Quote:
 Difference = (0, 0, -3.8147e-006)

I'm presuming that's some sort of floating point error, but do you have any suggestions to solve this?

##### Share on other sites
If the distance between the spheres is less than 0.1, you will move the sphere too far I guess (so it will oscillate around the second sphere)? Perhaps you should clamp the values so if distance < 0.1, you simply set ball_one.position = ball_two.position? Perhaps you get some kind of divide by zero error when you normalize the diff-vector, because of the precision of floats.