You could either just nudge vx and vy around when bouncing. For example for a horizontal bounce:
set vx to -self.vx
add random(-1,1) to vy
A vertical bounce can be done similarly.
Or you can actually nedge the angle. To do that takes two steps. First calculate speed and angle from vx and vy, then convert it back to vx and vy from that after nudging the angle.
ang = angle(0,0,ball.vx,ball,vy)
speed = distance(0,0,ball.vx,ball,vy)
vx = speed * cos(ang+random(-2,2))
vx = speed * sin(ang+random(-2,2))
For changing the angle depending on where on the paddle was hit you could just add the horizontal distance between the ball and paddle when hitting the top of the paddle (event 16). So basically:
add (ball.x-paddle.x) to vx
You could multiply it by 0.5 or something to make it more subtle.
For actual round objects you'd need something better for contact normal detection.
This may work, you may want to add 1 or so to the radius check.
- Code: Select all
local number NormalAngle=0
For each roundWall
system compare: distance(roundWall.x,roundWall.y,ball.x,ball.y) <= ball.radius+roundWall.radius
--- set normalAngle to angle(roundWall.x,roundWall.y,ball.x,ball.y)
Basically to bounce you need to know the normal of the surface hit and then you can bounce using either the math event 19 in your capx or with the normal angle using that formula from granpa:
set angleOfMotion to 2*normalAngle-ball.AngleOfMotion+180