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?
|