Hello there.
Thanks for taking a look at my thread. I'm at a bit of a brick wall, but I'm sure you'll agree it's an interesting one.
I'm using acceleration and velocity vectors to move stuff around, and I'm trying to handle collisions using Sparky's DLL.
I basically understand vectors, and I've been using DBPro a while so I think I should be able to do this!
My problem is this, I use a ray (object's position, to the object's position + velocity) to check for collisions, and I then know the collision normal (if one exists!). I know the vector the object should be about to move on, and I know the normals.
I think that's all I should need to work out what should happen next... but I don't know how to fit it together.
I'm thinking I should resolve the velocity in the direction of the collision normal, then take the result of that away from the velocity. This should let me slide along the surface.
Am I right? Any ideas how to resolve a vector to find its component parralel to another?
Or, am I totally barking up the wrong tree here? Does anyone have any useful information for me or examples they can point me to?
There's tonnes of 2D resources out on the wide internet, but my mind boggles trying to convert it to 3D!
Code below!
Rem Project: Dark Basic Pro Project
Rem Created: Sunday, February 10, 2013
Rem ***** Main Source File *****
sync on
autocam off
`make a general flat floor
make object box 10,500,5,500
sc_setupObject 10,1,1
color object 10,RGB(0,128,18)
`now make a bunch of little hills to check the collision detection
for a=11 to 40
make object sphere a,20
color object a,RGB(78,192,0)
scale object a,rnd(200)+50,rnd(50)+50,rnd(200)+50
position object a,rnd(250)-rnd(250),0,rnd(250)-rnd(250)
sc_setupObject a,1,1
NEXT a
`make a sphere to stop the player getting too close to the camera
make object sphere 41,50
position object 41,0,100,0
sc_setupObject 41,1,1
position camera 0,200,0
position camera 0,0,0
`make the player
make object cone 1,20
x#=0
y#=200
z#=0
speed#=0
a#=0
b#=0
Acceleration=1
Add=2
Collision=3
Movement=4
a=make vector3(Acceleration)
a=make vector3 (Add)
a=make vector3 (Collision)
a=make vector3(Movement)
set vector3 Acceleration,0,0,0
set vector3 Movement,0,0,0
do
`clear the add vector
set vector3 Add,0,0,0
`add gravity
set vector3 Add,0,-0.4,0
add vector3 Acceleration,Acceleration,Add
set vector3 Add,0,0,0
`take inputs and add that to the player
if upkey()=1
ENDIF
if downkey()=1
ENDIF
add vector3 Acceleration,Acceleration,Add
set vector3 Add,0,0,0
`add all the accelerations that have happened to the movement vector, ready to check for collisions
add vector3 Movement,Acceleration,Movement
set vector3 Acceleration,0,0,0
`check for collisions
collide=sc_RayCastGroup(1,x#,y#,z#,x#+x vector3(Movement),y#+y vector3(Movement),z#+z vector3(Movement),0)
`set the collision vector to 0
set vector3 Collision,0,0,0
if collide>0
`set the collision unit vector to the normal of the collision
set vector3 Collision,sc_GetCollisionNormalX(),sc_GetCollisionNormalY(),sc_GetCollisionNormalZ()
`the dot product of the collision vector and the movement vector
dot#=Dot Product Vector3(Movement,Collision)
`we now have the angle between the vectors?
angle#=acos(dot#/(length vector3(Movement)*length vector3(Collision)))
`resolve vector
endif
`start moving the player
inc x#,x vector3(Movement)
inc y#,y vector3(Movement)
inc z#,z vector3(Movement)
position object 1,x#,y#,z#
point camera x#,y#,z#
`dump the player position to the screen
text 0,0,str$(x#)+" "+str$(y#)+" "+str$(z#)
sync
LOOP
Again, thanks for taking the time!