B3D Collisions & reflection

Blitz3D Forums/Blitz3D Programming/B3D Collisions & reflection

_PJ_(Posted 2010) [#1]
I only just realisefd the CollisionNX etc. commands should make things a lot easier for collisions. Previously, I was working on getting the collisiontriangle and figuring its planar vector and then the normal that way :D

Anywho, I've ended up with this function that should work for 2 dimensional rebounding (This all assumes the Y value (vertical height) is constant and irrelevant)

But the values I get out dont match hat I estimate they should be (I really suck at trig though... )

Overall I just need to know if this is correct, or at least on the right lines:


	DebugLog "Rebound"
	;Returns the Yaw angle of reflection for CollisionEntity after a collision
	nX=CollisionNX(CollidedEntity,CollisionIndex)
	nZ=CollisionNZ(CollidedEntity,CollisionIndex)
	DebugLog "Collision="+collisionIndex+"  :  "+collidedentity
	
	Yaw=((ASin(nx)+ACos(nz)*2*Pi)) ; THIS IS PARTICULARLY THE LINE I AM NOT SURE ABOUT!
	
	DebugLog "Old Yaw="+EntityYaw(CollidedEntity,True)

	DebugLog "New Yaw="+Yaw

	Return Yaw



Al;ternatively, if this is all a complete load of rubbish and there's better, easier, faster or more 'standard' ways to achieve this result, please let me know :D


Floyd(Posted 2010) [#2]
The angle for the Normal vector would be:

Yaw = ATan2( nz, nx )

If I understand what you want there is an old yaw value and a "reflected" new value:

NewYaw = 2*Yaw - OldYaw - 180

NewYaw may not be in the range of -180 to 180. That could be adjusted as follows:

NewYaw = NewYaw mod 360
If NewYaw > 180 then NewYaw = NewYaw - 360
if NewYaw <= -180 then NewYaw = NewYaw + 360

You will have to test this as I'm just making it up as I go. But it seems right.

EDIT: If anyone saw this post earlier I have made one change. Originally I imagined the Old and New entity movement vectors as both coming out of the collision point. Of course that gets the Old backward. It should be moving into the collision point. So I changed the Old one to point in the opposite direction, i.e. change by 180 degrees.

That is why there is now a 180 in NewYaw = 2*Yaw - OldYaw - 180.


_PJ_(Posted 2010) [#3]
That's great, Floyd, thanks.

This also helps me to understand ATan2() better, it's one of the trig functions I understood the least!

Rather than deal with rotating the bouncing entity itself in the function, I wanted to keep it simple as I am using pivots to control the movement of meshes whilst meshes need to face the right way, and of course, only the geometry of meshes can be used to collide as above.

Thanks too for the fix for the range. With that included it all appears to work as intended :)