Reputation: 33
I am making a game with some bouncing elements IN a circle (I use pygame) , My elements have 2 attributes , one for the angle and one for the speed Here is how elements moves :
mvx = math.sin(self.angle) * self.speed
mvy = -math.cos(self.angle) * self.speed
self.x += mvx
self.y += mvy
My problem is this : I know the angle at the top (99.6°) , I have the collision point (x and y ) , but I'm unable to find the angle at the bottom(42.27°) Does someones can make a relation between the first angle and the second ?
Upvotes: 3
Views: 1699
Reputation: 210890
I recommend do calculate the reflection vector to the incident vector on the circular surface.
In the following formula N
is the normal vector of the circle, I
is the incident vector (the current direction vector of the bouncing ball) and R
is the reflection vector (outgoing direction vector of the bouncing ball):
R = I - 2.0 * dot(N, I) * N.
Use the pygame.math.Vector2
.
To calculate the normal vector, you' ve to know the "hit" point (dvx
, dvy
) and the center point of the circle (cptx
, cpty
):
circN = (pygame.math.Vector2(cptx - px, cpty - py)).normalize()
Calculate the reflection:
vecR = vecI - 2 * circN.dot(vecI) * circN
The new angle can be calculated by math.atan2(y, x)
:
self.angle = math.atan2(vecR[1], vecR[0])
Code listing:
import math
import pygame
px = [...] # x coordinate of the "hit" point on the circle
py = [...] # y coordinate of the "hit" point on the circle
cptx = [...] # x coordinate of the center point of the circle
cpty = [...] # y coordinate of the center point of the circle
circN = (pygame.math.Vector2(cptx - px, cpty - py)).normalize()
vecI = pygame.math.Vector2(math.cos(self.angle), math.sin(self.angle))
vecR = vecI - 2 * circN.dot(vecI) * circN
self.angle = math.pi + math.atan2(vecR[1], vecR[0])
Upvotes: 2
Reputation: 2522
The inner angles of a triangle need to sum up to 180°
. Also, the angle 99.96°
is supplementary to the triangle's angle next to it (calling it by A
), i.e. 99.96° + A = 180°
so A = 180° - 99.96°
. Calling of B = 42.27°
the bottom angle. And for the last angle C
, we can use that it is opposed by the vertex with the other angle that is equal to 2 * 28.85 = 57.7°
.
Then:
A + B + C = 180°
180° - 99.96° + 42.27° + 2 * 28.85° = 180°
180° - 99.96° + 42.27° + 2 * 28.85° = 180°
-99.96° + 42.27° + 2 * 28.85° = 0°
42.27° + 2 * 28.85° = 99.96°
B + C = Top angle
P.S.: I know that the values are not exactly equal, but it must be because of the decimal places rounding
Upvotes: 0