reflecting missile

Blitz3D Forums/Blitz3D Programming/reflecting missile

ryan scott(Posted 2004) [#1]
i'm making a game where my missiles, which are facing a certain direction, need to reflect off vertical walls.

all the code i've seen for bouncing/reflecting seems to assume you are bouncing a ball, and your object isn't really facing a particular direction. you control with plain old x,y,z and dx,dy,dz. i'm not doing that because they are missiles. they face forward and always need to.

i'm trying to adapt the code i've found in these forums but i'm stuck.

I am detecting the Normal of the surface i'm colliding with, that seems to work. but since i don't control my object with dx,dy,dz, i'm computing dx,dy,dz each loop, and trying to align my missile to the resulting vector. no luck. i am unsure what i'm doing, obviously..


		; before moving, get our last position so we can figure out dx,dy,dz after we move it.		
		b\lastx#=EntityX(b\model)
		b\lasty#=EntityY(b\model)
		b\lastz#=EntityZ(b\model)
		
		MoveEntity b\Model,0,0,b\speed
				
		; we need to keep our dx,dy,dz or we can't do bounces
		b\dx#=EntityX(b\model)-b\lastx#
		b\dy#=EntityY(b\model)-b\lasty#
		b\dz#=EntityZ(b\model)-b\lastz#

				
		b\Life = b\Life - 1 ; (b\Life > 0)
		b\timealivesofar= b\timealivesofar+1
				
		;-----------------------------------COLLISIONS?
		If CountCollisions(b\Model) > 0
			For ID = 1 To numberofcollisiontypes
				entity = EntityCollided (b\Model,ID)
				If entity <> 0
					Select ID
						
						Case T_WALL
							
							nx# = CollisionNX#(b\model,1)
							ny# = CollisionNY#(b\model,1)
							nz# = CollisionNZ#(b\model,1)
							
	 	 
							;Compute the dot product of the entity's motion vector And the normal of the surface collided with. 				
							VdotN# = (b\dx# * Nx# + b\dy# * Ny# + b\dz# * 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 direction vector. 
							dx# = b\dx# + NFx#/2
							dy# = b\dy# + NFy#/2	;Force /2 assumes balls equal in mass
							dz# = b\dz# + NFz#/2
														
							AlignToVector b\model,dx#,dy#,dz#,3,1						


i've tried various combinations of this, and this is the closest i've gotten to a reflection, hmm actually this works better:

AlignToVector b\model,nx#,ny#,nz#,3,1

but it's just reflecting in the direction of the normal (although not always exactly... i have no idea what's wrong)

can someone point me in the right direction?


ryan scott(Posted 2004) [#2]
by the way in case i didn't make it clear the walls are vertical. it's basically a 2d game, top down view, although played on a 3d playfield.


Stevie G(Posted 2004) [#3]
So basically you want the missile to point in the direction it's travelling. Align to vector on the z-axis, based on the new x,y,z velocity should work - as you're doing. What kind of results do you get? Sometimes the align to vector doesn't always do what you expect so until someone more familiar with the command comes along...

You create a global pivot, parent it to the missile, position it at dx,dy,dz then use point entity. This will work.

Stevie


ryan scott(Posted 2004) [#4]
when it hits the wall, it aligns with the wall and runs parallel down its edge.

i am unclear how to do what you say regarding making the global pivot.

is it possible that the dot product isn't working right because my dx,dy,dz are not a vector since they are not brought to length of 1 (not like i understand what i'm talking about here)


Stevie G(Posted 2004) [#5]
By halfing the normal force you're not going to get a differing mass effect so this may be your problem.


ryan scott(Posted 2004) [#6]
hey that worked!

thanks


Stevie G(Posted 2004) [#7]
If you need something which allows you to build masses into the equation so that they work correctly - I'm pretty sure there is something in the code archives here or @ blitzcoder (which is obviously down atm)

I use a similar system but with my own collisions as the in-build ones are not good in some situations.