views:

3297

answers:

7

I played around with it for a while, but i simply cant figure it out.

I made a tank that fires missiles, and when the missiles hit the walls, i want them to bounce off, but i want them to bounce off to the right angle.

Right now i havent got any obstacles, the missiles just bounce off when they get outside the viewportRectangle i made.

I have a suspicion that the solution im looking for is quite advanced?

Is there a relativly simple way to do

Thanks in advance :)

+4  A: 

Not complicated at all - pseudo-code:

angleObjectHitWall = a;
bounceAngle = 180-a;

Of course this is a very simple calculation, and is totally irrelevant once you start to take into account factors such as material, gravity, walls which aren't straight, etc...

Yuval A
+6  A: 
ChrisF
The image is at http://upload.wikimedia.org/wikipedia/commons/2/24/Angle_of_Reflection_(PSF).png
ChrisF
+1 for the image I hijacked. :)
Bill the Lizard
How do I get the image to display inline?
ChrisF
Right click the image and select "Copy Image Location" (or get its URL from the browswer address bar). Insert the image using the Image button on top of the editor pane.
Bill the Lizard
I did that and it appears in the preview, but not on the final page.
ChrisF
Oh, and thanks for the +1
ChrisF
Try it again. Now that you're above 15 reputation you have image-posting privileges.
Bill the Lizard
Thanks - I'd seen the other restrictions based on reputation but not that one.
ChrisF
No problem. Thanks again for the image.
Bill the Lizard
+25  A: 
Bill the Lizard
That's a remarkably elegant way to handle this problem.
acrosman
I use the angle of the missile to make it go forward.using Math.Sin and Math.Cos.I had problems understanding the other way, using velocity, like in the XNA example with the canonballs.So all i have to change is the angle of the missile, then it will go forward by itself.
Moulde
Just good physics. A VERY nice illustration. +1 just for that, since a picture is worth 10K words. I'm doling out micropoints, so it's 0.001 per word saved. 8)
duffymo
Make that 0.0001 per word saved - I'm a doofus.
duffymo
This only works if all your walls are aligned to the coordinate axes, and even then you need separate case for vertical and horizontal walls. It's probably easier to get it right in the general case.
Gareth Rees
No, it isnt. In the general case what you have to do is align your vectors using some trigonometry so that the wall becomes horizontal or vertical for the sake of your calculation, then do this calculation, and convert it back. It's onyl 2-4 more trig calcs, but not easier.
Karl
@Gareth: You're right this only works for the borders and for walls that are aligned with the borders. I was only answering the question that was asked, which is much easier than the general case.
Bill the Lizard
@Karl: No trigonometry is required!
Gareth Rees
Of course, you can easily adapt this to work for objects at different angles that are stationary or moving. Just use the velocities of both objects in a collision to calculate the new velocities of each.
Bill the Lizard
+2  A: 

180-a will not work in all instances, unless you are merely working a bounce on a top surface when X is increasing.

One direction to head is the XNA forums or pick up XNA sample code. It is C# and it is for building games. I am not stating you want to build your games in XNA, but it is a great tool, and it is free.

Gregory A Beamer
It is XNA im using.And yes, there is a problem..When firering missiles at the left and right border of the window, the missiles reflect properly, but when shooting at the bottom or top border of the window, i can see the reflect, just not the right angle.
Moulde
+1  A: 

This is really a physics question, so if you are not a physicist (and since you are asking this question, I'm going to take it that you are not) it will require a lot of reading and brainstorming to get it right.

I suggest reading this wikipedia entry to get the basic idea about the depth of your question.

If you only want to make it "look plausible" then I wouldn't worry about it too much and use Bill the Lizard's answer, however if you want to make it right you will have quite an adventure. Don't let this scare you tho! Good luck!

Rekreativc
+31  A: 

You might think that because your walls are aligned with the coordinate axes that it makes sense to write special case code (for a vertical wall, negate the x-coordinate of the velocity; for a horizontal wall, negate the y-coordinate of the velocity). However, once you've got the game working well with vertical and horizontal walls, probably the next thing you'll think is, "what about walls at arbitrary angles?" So it's worth thinking about the general case from the beginning.

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

Split v into components u perpendicular to the wall and w parallel to it.

Here u = − v (v.n / n.n) and w = v − u. ("v.n" is the dot product of the vectors v and n. See the link for an explanation of how to compute it. The dot product n.n evaluates to the square of the length of the normal vector; if you always keep your normals in the form of unit vectors then n.n = 1 and you can omit the division.)

After bouncing, the component of motion parallel to the wall is affected by friction f, and the component perpendicular to the wall is affected by elasticity, which can be given in the form of a coefficient of restitution r.

So the velocity after the collision is v' = wf − ur. In a perfectly elastic, frictionless collision, v' = w − u; that is, the motion is reflected about the normal at the point of collision, as in the diagram given in Bill's answer.

This approach works just the same in three dimensions too.

(Obviously this is a very simplified notion of bouncing; it takes no account of angular momentum or deformation. But for many kinds of video games this kind of simplification is perfectly adequate.)

Gareth Rees
Excellent explanation!
Andy Mikula
I agree that starting with the special-case coding for vertical and horizontal walls is the wrong way to go. It's not a stepping stone to the correct route, but is instead a dead-end path.
Adam Kane
+2  A: 

As an aside to the specific physics question you are asking, I would recommend the book "Beginning Math and Physics for Game Programmers" by Wendy Stahler. I found it quite useful for my game/physics programming projects.

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

Have a good one!

itsmatt