开发者

2D Game Physics Vectors issue

I've been working on a simple program in C# in which a Ball [X,Y] cordinates are periodical incremented.

I've managed to implement a collision detection method, but I'm trying to determine how to r开发者_高级运维eflect the ball at an angle oposed bouncing it back along the same linear path.

dx = -dx //This bounces the ball back along the same linear path
dy = -dy

Solution Trigonometry

theta = range between 0<theta<=360 depending on where it bounced
x = cos(theta)*time
y=  sin(theta)*time


The whole point of Newtonian physics is that it is not random, it is deterministic. If you throw the same ball against the same wall at the same angle and with the same velocity and the same spin, it goes to the same place every time.

This sort of program is a really great learning opportunity for both programming and physics. What I encourage you to do is to first write a program that simulates very simple bouncing. As you note, when an object is moving straight down and hits a horizontal surface, then you can model the bounce as simply reversing the vertical velocity component. Just get that right; no gravity, no nothing. That's a great start.

Then try adding bouncing off of horizontal walls, the same way.

Then try adding bouncing off of walls that are not aligned with horizontal or vertical directions. That's where you're going to have to learn how vectors and trigonometry work, because you'll have to work out what component of the ball's velocity is changed by striking the wall obliquely.

Then add gravity. Then add friction from the air. Then add the fact that the ball can be spinning. Add elasticity, so that you can model deformation of the ball.

Once you get to that point, if you want to introduce randomness you'll be able to figure out how to do it. For example, you might introduce randomness by saying "well, when the ball strikes the wall and deforms, I'll introduce a random element that changes its deformation by 0-10%". That will then change how the simulation bounces the ball. You can experiment with different kinds of randomness: add random air currents, for instance.


You will have to add in randomness yourself. To rephrase your question: "Deterministically, it bounces off at angle theta. How can I make it bounce back at angle theta + epsilon, where epsilon is some random value?"

To rotate a vector, see this. You will just specify theta.

pseudocode:

RotateVector(vec):
    bounce_vec = [-vec.x vec.y];  //deterministic answer is negative x, normal y
    bounce_angle = acos(dot(vec,bounce_vec) / (norm(vec)*norm(bounce_vec)));
    modified_angle = bounce_angle + random_number();
    ca = cos(modified_angle);
    sa = sin(modified_angle);
    rotation_matrix = [ca -sa; sa ca];
    return rotation_matrix * vec;

Line 3 uses the law of cosines to figure out the angle. In line 4, that angle is modified randomly. The rest of the function rotates the original vector by your new angle.


As long as it's a perfect ball with a perfect surface it will not bounce back randomly. Neither vectors nor trigonometry will give you any randomness.


"randomly, though applying to the basic laws of physics" seems like an oxymoron. However...

If you want it to bounce in a random direction, while maintaining its current speed, you might do something like this (pseudocode):

  • first, bounce back the canonical way (dx = -dx or dy = -dy depending on the collision)
  • then convert the dx and dy to polar coordinates (theta and r)
  • jitter theta by a small amount (+ or - a few degrees, according to your taste)
  • make sure theta isn't heading into a wall that you just bounced off
  • convert theta and r back to dx and dy

That would be conserving scalar momentum.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜