Jump to content
• Advertisement

# Adding a direction change to a travel vector?

This topic is 5051 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

Hey guys, I'm still working on my 2D 'breakout' style game and I think I'm getting the hang of this vector math stuff... Here's one that's got me stumped. Let's say you have two square objects, object1 and object2, and object1 is moving on a 2D travel vector of (0.707, 0.707) at a speed of 36. Now, that thing collides with the top of object2 moving on a travel vector of (-1, 0) that's travelling at a speed of 32. I want to bounce object1 off of object2, but I can't figure how to do it without getting object1 stuck inside of object2's volume. Doing the math, object1 is travelling at a speed of (25.452, 25.452) and object2 is travelling at a speed of (-32, 0). Since both objects are moving, I can't simply negate object1's X vector (turn it into (-0.707, 0.707)), because a speed of -25.452 will not allow object1 to 'out-run' object2's speed of -32. How do I change object1's travel vector to allow it to move faster than object2? I don't want to add the velocity of object2 to object1, because I don't want to actually change the speed of object1 (its speed value should remain at 36). I just want to change object1's travel vector enough to where it will actually outrun object2. Would anybody help me on this? Thanks!

#### Share this post

##### Share on other sites
Advertisement
If object2 speed is (-32,0) and object1 speed is (25.452,25.452) when they collide, if object2 doesn't change its speed and object1 doesn't change its speed value, you can change its movement vector but you'll not change velocity, only direction. And since it is moving too slow with respect to object2, the two object will overlap. To make something move faster you have to change the speed with acceleration.

You negate the direction of the object when it collide something with big mass and that doesnt' move (ie. the ball against the wall). In your case you have to take into account the masses of the two object. Maybe they have the same mass. When they collide elasticly they change their mass. Otherwise they will keep in touch with a speed that is the sum of their speed (if I remember well [grin]).

Hope this can be of help.

#### Share this post

##### Share on other sites
I *think* I understood that... I am confused about one thing though. I assume object 1 is the ball and object 2 is the paddle? If that's the case, and the paddle is moving laterally, and the ball collides with the top of the paddle, how is there a problem? Now, I can see if the ball hit the *side* of the paddle there might be a problem with the ball getting stuck in the paddle. But maybe I'm wrong about the whole ball/paddle thing.

Anyway, I'm sure this problem is solvable, but maybe you could give a few more specifics.

#### Share this post

##### Share on other sites
Sorry I'm sorta vague, but I really stink at explaining stuff. :D

Here's a pic of what I'm trying to do:

The blocks move! You can see the red block is moving up at a rate of -32 (its speed is 32, travelling on a vector of (0, -1), and the ball is moving diagonally at a rate of 25 (its speed is roughly 36, travelling on a vector of (0.707, 0.707)). When the ball bounces off the block, its vector is changed to compensate for the block's travel vector and speed. So when this ball bounces off the block, its vector has been changed to (0.208, -0.978) (this is a rough guess) so its Y speed will outrun the block... However, I don't know how to put together an equasion that will calculate this vector change.

Sorry I can't explain this very well, it's one of those things where 'I know what I want to do, but I don't know how to say what I want to do'. D'oh! But if you still don't understand, I'll try to explain it better.

Thanks again!

*EDIT - Made edits because I confused the X and Y... Can you tell I haven't had my morning coffee yet? :)

#### Share this post

##### Share on other sites
Got it. Here's a quick 'n dirty solution.

You have a velocity vector x, y. You know that you want the length to be 36. You know you want the y to be -32. So you know the speed s, and the y, and you want to know x:

s = sqrt(x^2 + y^2)

s^2 = x^2 + y^2;

s^2 - y^2 = x^2

x = sqrt(s^2 - y^2)

So now you can find x given s and y (I think you'll have to keep track of the signs yourself).

So what I would do is use the above equation only if the y component of the bounce vector is < 32 (or > -32, or whatever). Also, using exactly 32 for y may still cause problems due to floating point error (the ball will just barely outrun the brick, and may get stuck after all), so I would use 32 + e, where e is some appropriately small epsilon.

There may be other solutions to the problem, but that's the first one that occured to me. Anyway, 'hope that helps.

#### Share this post

##### Share on other sites
Thanks for the reply, jyk. Also thanks for explaining it step-by-step, that REALLY helps! That's pretty close to what I'm trying to do, but that equasion seems to change the speed of the ball rather than only change the travel vector. And when I normalize those values and then apply the '36' speed to the ball, the ball ends up going too slow again and gets run over by the block.

I guess what I want to do is change the ball's normalized travel vector to where when the ball's velocity, 36, is applied to the travel vector the Y value will match the block's Y speed (plus a little extra, so the ball outruns it), and the X value will change since the ball's travel vector will be normalized. This will change the ball's angle of travel, but it won't change its velocity.

Thanks!

PS - I do my ball's travel like this:
1) Create a normalized travel vector
2) Multiply the ball's 'speed' value to the normalized vector
I don't create a vector that also contains a length (speed), because I have a hard time keeping the ball's velocity constant when I change the ball's direction of travel. Me still a noob to vector math! I find it easier to just deal with a normalized vector and simply multiply a speed value to the X and Y values of that vector.
Thanks again, I really appreciate the help!

#### Share this post

##### Share on other sites
Never mind, I got it to work... I just got my signs wrong. It works great! Thanks for the help jyk, I really appreciate it!

#### Share this post

##### Share on other sites
Quote:
 Thanks for the help jyk, I really appreciate it!
No problem :-)

#### Share this post

##### Share on other sites
Quote:
 Original post by DookieNever mind, I got it to work... I just got my signs wrong. It works great! Thanks for the help jyk, I really appreciate it!

aside from the fact that it has no bearing on physical reality, i doubt that solution is very rubost.

i suggest you do some reading on the subject. impulse and energy formulas are what you need fora correct solution of this problem.

#### Share this post

##### Share on other sites
Quote:
 aside from the fact that it has no bearing on physical reality, i doubt that solution is very rubost.
Hi Eelco,

I appreciate you questioning my response to the OP, as any such criticisms help me to learn. Let me offer an explanation as to why I gave the response that I did.

The results that the OP was looking for could not be derived from a realistic physical simulation. He wanted two objects to collide, with object 1 not changing velocity at all and object 2 to change velocity but not speed. As I understand it, object 1 would have to have infinite mass in order for its velocity to be unchanged after collision. And even then, object 2's speed would be changed drastically by the collision.

So that's why I didn't recommend a physically realistic solution to the OP. Instead, I offered the first practical solution that occured to me, with the caveat that there were probably other solutions.

As far as robustness, with an appropriate epsilon I'm not sure why it would fail.

Again, though, there are probably better solutions, but like everyone else here I don't always have that much time to devote to my posts. Whenever that is the case, though, I try to make sure and offer an appropriate disclaimer.

If you have any other specific criticisms of the method I proposed, or can offer a better solution to the OP's original problem, I'm sure it would be very helpful to him.

I took another look at the equation x = sqrt(s^2 - y^2)for any robustness issues. If the ball is travelling straight down, after collision y + e might be > s, in which case sqrt() might fail. I think a simple solution would be to set x to 0 if y + e > s - e. Otherwise, use the equation as is.

[/Edit]

[Edited by - jyk on February 14, 2005 2:45:28 PM]

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5
• Advertisement

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633704
• Total Posts
3013460
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!