Feeds:
Posts
Comments

Posts Tagged ‘reflection’

Making things bounce

Local vx#,vy# ‘V, the velocity of the object before it hits the wall
Local wallangle# ‘the angle of the wall
‘now work out the normal vector to the wall – it’s just at right angles
Local nx#,ny#
nx = Cos(wallangle + 90)
ny = Sin(wallangle + 90)
‘p is the projection of V onto the normal
Local px#,py#
Local dotproduct# = vx*nx+vy*ny
px = dotproduct*nx
py = dotproduct*ny
‘the velocity after hitting the wall is V – 2p, so just subtract 2*p from V
vx = vx – 2*px
vy = vx – 2*py
‘done!

Andres asked in this post:

i’m having problems with a formula for my early stage game.

i want to calculate how the object bounces (the angle) from the wall.

i know the angle of the wall, object’s moving angle, the point where object and wall collide. If possible then the function or formula would work if the object comes from the other side of the wall too.

Countless other people have asked this as well. It’s one of those things that comes up very often but is quite tricky to work out for yourself.

So, to start with, we’ve got something hitting a wall. We know its velocity before it hits the wall and we want to know what its velocity becomes after it hits the wall. reflection1

When an object bounces off a wall, it travels at the same angle from the wall as it went in at, but the other way round. I stated that in terms of angles, but that’s tricky to get into code because, while people always know to write an angle as 90° anti-clockwise instead of 270° clockwise, and how to do stuff like reflections properly, it isn’t so simple when you’re just dealing with numbers in a computer. For this example, there are four choices of escape angle that you could produce if you’re not careful, but it’s hard to say which one is the correct one.

So instead, consider V as a cartesian vector – that is, an x-component and a y-component. We could also think of it as having a component moving parallel to the wall, and one moving perpendicular to it.

reflection2

In the above image, p is the component of the vector which is perpendicular to the wall. If V didn’t bounce off the wall but instead kept travelling as it was before, travelling backwards along p from the end of V would get you back on the line:

reflection3

And if you move back in the direction of p one more time, you’ll end up pointing exactly where you want to be: the angle of escape is the same as the angle of entry.

reflection4So that’s the theory – we need to work out V and p, and the object’s velocity after hitting the wall will be V-2p.

V is easy – you might already have it in vector form, or if not, you can use V_x = V\cos \theta and V_y = V\sin \theta. p is the projection of V onto the wall’s normal vector. (I’m planning on writing a post about the various vector operations later, but this one takes just a couple of lines of code to work out and considerably more writing to explain, so I won’t.)

And the great thing about this method is that you don’t need to check which side of the wall the object is hitting, or which way round the normal is pointing – it all works out correctly anyway.

Local vx#,vy#		'V, the velocity of the object before it hits the wall
Local wallangle#	'the angle of the wall

'now work out the normal vector to the wall - it's just at right angles
Local nx#,ny#
nx = Cos(wallangle + 90)
ny = Sin(wallangle + 90)

'p is the projection of V onto the normal
Local px#,py#
Local dotproduct# = vx*nx+vy*ny
px = dotproduct*nx
py = dotproduct*ny

'the velocity after hitting the wall is V - 2p, so just subtract 2*p from V
vx = vx - 2*px
vy = vx - 2*py

'done!

I hope that was helpful.

Read Full Post »