Welcome, Guest. Please login or register.

Author Topic: Angle of deflection  (Read 1094 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Angle of deflection
« on: August 14, 2007, 02:36:52 PM »


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
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Angle of deflection
« Reply #1 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).
int p; // A
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Angle of deflection
« Reply #2 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
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Angle of deflection
« Reply #3 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
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline uncharted

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 1520
    • Show only replies by uncharted
Re: Angle of deflection
« Reply #4 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:
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Angle of deflection
« Reply #5 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
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Angle of deflection
« Reply #6 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
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10