41 lines
763 B
Ruby
41 lines
763 B
Ruby
# A = old position, c = center of ball, v = current moving vector
|
|
def reflection_vector(ax,ay,cx,cy,xv,yv)
|
|
# Bouncing of resulting point: R
|
|
rx = ax + xv
|
|
ry = ay + yv
|
|
ux = rx - cx
|
|
uy = ry - cy
|
|
|
|
r = (-ux*cx + ux*ax -uy*cy + uy*ay).to_f / ( ux * ux + uy * uy).to_f
|
|
|
|
fx = cx + r * ux
|
|
fy = cy + r * uy
|
|
|
|
afx = fx - ax
|
|
afy = fy - ay
|
|
|
|
gx = ax + 2 * afx
|
|
gy = ay + 2 * afy
|
|
|
|
rgx = gx - rx
|
|
rgy = gy - ry
|
|
|
|
return rgx,rgy
|
|
end
|
|
|
|
|
|
# x,y further away than r allows?
|
|
def bouncing?(x,y,cx,cy,r)
|
|
# d(cx,cy,x,y) > r?
|
|
d = Math.sqrt((x - cx)*(x-cx) + ( y - cy)*( y - cy))
|
|
|
|
d > r
|
|
end
|
|
|
|
# if the speed vector is higher than the radius, we are done (2x radius would be still okay actually)
|
|
def too_fast?(vx,vy,r)
|
|
d = Math.sqrt(vx*vx+vy*vy)
|
|
|
|
d > r
|
|
end
|