Rebounding Trig

Blitz3D Forums/Blitz3D Beginners Area/Rebounding Trig

_PJ_(Posted 2015) [#1]
Hi everyone, yet again, my utter failure at trigonometry strikes again!

This should be an extremely simple solution, but I just can't get it to work correctly.

Basically I am dealing with an object rebounding from perfect straight walls aligned horizontally and vertically.
There's no consideration for elasticity or kinetic energies etc. The entity should simply rebound at the correct incident angle on encountering the boundary.

It's calculating this angle that I am struggling with (though it really shouldn't be so difficult I'm sure!)

So far this is what I have, and it fails miserably!

Function UpdateBoundaries(B.Ball)
	Local X#=EntityX(B\Entity,True)
	Local Z#=EntityZ(B\Entity,True)
	Local Y#=EntityYaw(B\Entity,True)
	
	Local a#=Y+181
	Local r#=180-(Abs(a) Mod 90)
	
	
	If (Z<(0-Size))
		If (a>=270)
			TurnEntity B\Entity,0,360-r,0,True
		Else
			If (a<=90)
				TurnEntity B\Entity,0,180-r,0,True
			End If
		End If
	End If
		
End Function	


If anyone can help with a really succinct solution, I'd be most grateful! Thank you!

__ edit
fixed the code tags just because.


Floyd(Posted 2015) [#2]
It seems more natural to do this without angles, using the motion vector (dx,dy,dz).

At any moment this gives the "increment". Postion x becomes x+dx etc.

If a ball hits ceiling or floor then replace dy = -dy.

If it hits left or right wall then dx = -dx.

If it hits front or back then dz = -dz.


_PJ_(Posted 2015) [#3]
Thanks Floyd. Of course the vector method is really way simpler and makes a lot more sense!

Much appreciated, you always seem to respond so quickly when I need help ! ;)


Matty(Posted 2015) [#4]
If you want to include walls on angles then look up the reflection vector calculation - most good lighting textbooks will have it and it is standard in many shader languages. There should be plenty of vector examples online for calculating the reflection vector.

It is used in calculating specular lighting frequently so that could be another place to start.