Angles Of Reflection (Nasty trig stuff)
Blitz3D Forums/Blitz3D Beginners Area/Angles Of Reflection (Nasty trig stuff)
| ||
It's always trigonometry where I get stuck and it's hard to avoid it in 3D!!! I have a 2-D game in a 3-D world. Very simple the player Mesh can move in X and Z coords only. Around my SQUARE arena I have a boundary wall. I would like my Mesh to 'bounce' off this wall realistically - i.e. If it hits perpendiclar (at 90degrees), it will recoil stright backwards. I know the angle of reflection = angle of incidence, but how can I code this? I looked at Cosines, because the Cosine of 90degrees is 0, but it doesn't fit for other angles.. (I haven't touched trig for years and it shows :((( ) Plus I need to convert EntityYaw(Mesh) to whatever to decipher exactly which wall it is hitting, whether(for want of better descriptions) North, South, East or West... If it's of any consequence, my walls are built from single-unit primitive cubes (made using CopyEntity from a Master) |
| ||
Look for the thread "physics for stupid people like me" in this forum, I explained everything, and there's no sine/cosine involved. |
| ||
A wee demo .. can't remember who did this ( maybe DarkEagle ? )Graphics3D 640,480,16,1 Const C_BALL = 1 Const C_CUBE = 2 Const BALLS = 50 Const SPEED# = .5 Global camera,cube Global light =CreateLight() Type ball Field entity Field xvel#,yvel#,zvel# End Type Collisions C_BALL, C_BALL,1,1 Collisions C_BALL, C_CUBE,2,1 MakeStuff() While Not KeyDown(1) UpdateBalls() UpdateWorld() RenderWorld() Flip Wend ;============================================== ;============================================== ;============================================== Function MakeStuff() ;cube cube = CreateCube() ScaleMesh cube,30,30,30 FlipMesh cube EntityColor cube,50,50,200 EntityType cube,C_CUBE UpdateNormals cube ;camera camera = CreateCamera() PositionEntity camera,0,60,0 PointEntity camera, cube ;template for balls temp = CreateSphere() EntityType temp,C_BALL UpdateNormals temp EntityShininess temp,1 HideEntity temp ;balls For l = 1 To BALLS b.ball = New ball b\xvel = Rnd(-SPEED,SPEED) b\yvel = Rnd(-SPEED,SPEED) b\zvel = Rnd(-SPEED,SPEED) b\entity = CopyEntity( temp ) PositionEntity b\entity,Rnd(-30,30),Rnd(-30,30), Rnd(-30,30) EntityColor b\entity,Rand(100,255),Rand(100,255),Rand(100,255) radius# = Rnd( 1,4 ) ScaleEntity b\entity,radius,radius,radius EntityRadius b\entity,radius Next End Function ;============================================== ;============================================== ;============================================== Function UpdateBalls() For b.ball = Each ball For i = 1 To CountCollisions(b\entity) HandleBounce(b,i) Next TranslateEntity b\entity,b\xvel,b\yvel,b\zvel Next End Function ;============================================== ;============================================== ;============================================== Function HandleBounce(b.ball,i) ; Get the normal of the surface collided with. Nx# = CollisionNX(b\entity, i) Ny# = CollisionNY(b\entity, i) Nz# = CollisionNZ(b\entity, i) Mesh = CollisionEntity (b\entity,i) c.ball = Object.ball ( EntityName( mesh ) ) ; Compute the dot product of the ball's motion vector and the normal of the surface collided with. VdotN# = b\xvel*Nx + b\yvel*Ny + b\zvel*Nz ; Calculate the normal force. NFx# = -2.0 * Nx# * VdotN NFy# = -2.0 * Ny# * VdotN NFz# = -2.0 * Nz# * VdotN ; Add the normal force to the motion vector. b\xvel = b\xvel + NFx b\yvel = b\yvel + NFy b\zvel = b\zvel + NFz End Function |
| ||
I recommend What sswift Says, as I started that Thread and it was really helpful. Regards, ERic |