Amiga.org

Coffee House => Coffee House Boards => CH / Science and Technology => Topic started by: motorollin on August 14, 2007, 02:36:52 PM

Title: Angle of deflection
Post by: motorollin on August 14, 2007, 02:36:52 PM
(http://i298.photobucket.com/albums/mm244/motorollin/aorg/deflect.gif)

Assuming the ball is travelling at constant velocity with no spin, is it possible to calculate its angle of deflection given the angle in degrees in which it is travelling and on which side of the box it impacts?

--
moto
Title: Re: Angle of deflection
Post by: Karlos on August 14, 2007, 02:59:46 PM
For a perfectly elastic collision, yes. The component velocity that is (anti)parallel to the normal of the plane with which it collides will be inverted.

Or in plain English, the angle of reflection will equal the angle of incidence, meaning if it is coming at x degrees to the horizontal, it will bounce off at 180-x degrees to the horizontal (assuming you measure the angle in the same sense each time).
Title: Re: Angle of deflection
Post by: motorollin on August 14, 2007, 03:35:27 PM
For some reason this doesn't quite work in the application I'm writing. Here's what happens:

1. Sprite starts moving at 45 degrees to the horizontal.
2. Collision with right edge of playfield
3. Sprite deflects at 135 degrees to horizontal (180-45)
4. Sprite starts moving at 135 degrees to horizontal
5. Collision with top edge of playfield
6. Sprite deflects at 45 degrees to horizontal (180-135)

Step 6 means that the sprite exist the playfield at 45 degrees instead of bouncing back in to it as it should :-?

--
moto
Title: Re: Angle of deflection
Post by: motorollin on August 14, 2007, 03:41:00 PM
Oh I bet it's because it doesn't know which side it has collided with, only that it has collided. If that last collision had been with an object to the ball's right then a 45 degree deflection would have been correct. So I suppose I need to calculate which side it impacts on and whether the angle change would cause the ball to leave the playfield, and if it would then add 180 degrees to the angle to turn the ball round back in to the playfield.

Urgghhh...

--
moto
Title: Re: Angle of deflection
Post by: uncharted on August 14, 2007, 03:47:15 PM
If you're going for simple motion there is an easy shortcut you can take.  If you're doing something like Pong or breakout all you need to do is reverse the delta of horizontal or vertical component.

So if the ball hits the top make the ball start to move down, if it hits the left make it move right.  No need to calculate any angles.

I only say this because years ago I was making a pong type game with complex angle calculations, when someone pointed out that all I needed to do is the above  :oops:
Title: Re: Angle of deflection
Post by: motorollin on August 14, 2007, 03:53:46 PM
The problem is that the language I am using doesn't use "move the sprite y pixels up/down and/or x pixels left/right", it uses "set the sprite's angle of movement to a and its velocity to v". So I can't just tell it to move down. I have to tell it which angle to move in.

I think I've figured it out though. Karlos' rule of 180-angle works for the left and right sides. For the top and bottom, (180-angle)+180 seems to work. These are in hand-drawn tests I have just done. I'm going to reboot in to Windows now and test it in the game.

--
moto
Title: Re: Angle of deflection
Post by: motorollin on August 14, 2007, 04:07:43 PM
It works :-D I have four separate invisible ¨barriers¨ - one along each side of the playfield. Collision with the left or right barriers sets the angle of the object to 180-angle, and collision with the top or bottom barriers sets the angle of the object to (180-angle)+180. I might just add ¨+rand(10)¨ or similar to give some variation, otherwise the object eventually falls in to the same square movement.

Thanks guys!

--
moto