Sign in to follow this  

Moving sphere to an exact point

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

If you intended to correct an error in the post then please contact us.

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: - description of your image 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Because of the finite precision of floats you're almost by definition not going to get the exact same position unless you set them equal. So use zurekx's suggestion: When the norm (length) of the Difference vector is less than some epsilon, set ball1.position = ball2.position. If you choose your epsilon small enough you wont be able to see it.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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