How to calculate the angle of rebound?

I played with him for a while, but I just can't understand.

I made a tank that shoots rockets, and when the rockets hit the walls, I want them to bounce off, but I want them to bounce off at right angles.

Right now I have no obstacles, the rockets just bounce off when they go beyond the viewportRectangle that I made.

Is the solution I'm looking for quite advanced?

Is there a relatively easy way to do this?

+57
c # xna physics
Feb 21 '09 at 14:11
source share
9 answers

I think an easier way to do this is to use rocket speed instead of calculating angles. Say you have a rocket with xVelocity and yVelocity to represent its horizontal and vertical motion. These speeds can be positive or negative to represent left, right, up or down.

  • If the rocket hits the upper or lower boundary, the opposite sign is yVelocity .
  • If the rocket hits the left or right border, the opposite sign is xVelocity .

This will support movement on the opposite axis.

Borrowing an image from ChrisF's answer , let's say that the rocket starts at position I.

Angle of reflection

If the xVelocity and yVelocity are both positive (in the 2D-graph on the right and bottom, usually positive) the rocket will move in the indicated direction. Let's just assign values

 xVelocity = 3 yVelocity = 4 

When a rocket hits the wall in position C , its xVelocity should not be changed, but its yVelocity should be changed to -4 so that it returns in the upward direction, but continues to go to the right.

The advantage of this method is that you only need to track the xPosition , yPosition , xVelocity and yVelocity . Using only these four components and the refresh rate of your game, the rocket will always be redrawn in the correct position. When you deal with more difficult obstacles that do not have right angles or move, it will be much easier to work with speeds X and Y than with corners.

+61
Feb 21 '09 at 14:25
source share

You might think that since your walls are aligned with the coordinate axes, it makes sense to write a special case code (for a vertical wall, negate the x coordinate for speed, for a horizontal wall, negate the y-coordinate of speed). However, as soon as you play well with vertical and horizontal walls, perhaps the next thing you will think about is: "What about walls at arbitrary angles?" Therefore, it is worth considering the general case from the very beginning.

In the general case, suppose your rocket has velocity v and hits a wall with a normal surface n .

Missile with vector v about to obliquely hit a wall with surface normal n.

Divide v into components u , perpendicular to the wall, and w .

Right-angled triangle with hypotenuse for v, short side u parallel to wall and long side w parallel to wall.

Where:

u = ( v - n / n - n ) n
w = v - u

Here v - n - dot product of vectors v and n . See Link for an explanation of how to calculate it. The point product n - n estimates the square of the length of the normal vector; if you always save your normals as unit vectors , then n n = 1, and you can omit division.

After bouncing, the motion component parallel to the wall depends on friction f, and the component perpendicular to the wall depends on elasticity, which can be set in the form of a restitution coefficient r.

So, the velocity after the collision is v ' = f w - r u . In an ideal elastic, friction-free collision, v ' = w - u ; those. the movement is reflected relative to the normal at the point of collision, as in the diagram given in Bill's answer.

This approach works the same way in three dimensions.

(Obviously, this is a very simplified concept of bouncing, it does not take into account angular momentum or deformation. But for many types of video games, this simplification is quite adequate.)

+172
Feb 21 '09 at 15:17
source share

For ideal particles (& light), the angle of reflection is equal to the angle of incidence, as shown in this diagram (from commons.wikimedia.org).

Angle of reflection

Search Google for the “reflection angle” (without quotation marks).

This is a little more complicated when you consider the elasticity and materials of the object and obstacles;)

+10
Feb 21 '09 at 14:18
source share

As an answer to the specific physical question that you are asking, I would recommend Wendy Staler the book "Elementary Mathematics and Physics for Game Programmers." I found this quite useful for my games / physics programming projects.

The code that comes with the book is C ++, but if you know C #, it would be pretty easy to do the conversion.

Have a good one!

+4
Feb 21 '09 at 15:51
source share

I had this problem, the only way I found was to split the collision axes!

Try:

 x += velocity * Math.cos(angle * Math.PI /180); y += velocity * Math.sin(angle * Math.PI /180); if (x < 0 || x > canvas.width) { angle = 180 - angle; } else if (y < 0 ||y > canvas.height) { angle = 360 - angle; } 

Hope this helps you!

+4
Jun 18 '15 at 1:43 on
source share

Not complicated at all - pseudocode:

 angleObjectHitWall = a; bounceAngle = 180-a; 

Of course, this is a very simple calculation, and it absolutely does not matter if you start to take into account factors such as material, gravitational, walls that are not straight, etc.

+3
Feb 21 '09 at 14:14
source share

180-a will not work in all cases, unless you just work with rollback on the top surface when X increases.

One direction for the head is the XNA forums or the choice of XNA sample code. This is C #, and it is designed to create games. I do not claim that you want to create your own games in XNA, but it is a great tool and it is free.

+2
Feb 21 '09 at 14:27
source share

This is really a physical question, so if you are not a physicist (and since you are asking this question, I will assume that it is not), this will require a lot of reading and brainstorming correctly.

I suggest reading this wikipedia entry to get a general idea of ​​the depth of your question.

If you want to make it “look believable,” I will not worry too much about it and use Bill Lizard's answer, however, if you want to do it right, you will have quite an adventure. Do not let this scare you! Good luck

0
Feb 21 '09 at 14:45
source share
 if(!Collide(Missle, Mainchar)){ (Velocity.x)*-1; (Velocity.y)*-1; } 

It works and is simple, good luck.

-3
Jul 08 '15 at 21:18
source share



All Articles