i updated it a bit to take into account three dimensions, i think the collision code is working fine, though my make-shift gravity and floor makes it a little too jumpy, though it is my first attempt at it.
+ Code SnippetSet window on
set window layout 1,1,1
sync on
sync rate 30
Rem Declare Constants
Dim VectorCar#(2) : Dim VectorObj#(2) : Dim VectorOld#(2) : Dim Vn#(2)
VX = 0 : VZ = 1 : VY=2
Rem Make the Player
Make object box 1, 50,50,50
color object 1, rgb(100,0,0)
position object 1,0,0,0
REM make rotated plains to collide with
Make object plain 2, 1000,100 : Position object 2, 0,0,500
Make object plain 3, 1000,100 : Position object 3, 0,0,1500
Make object plain 4, 1000,100 : Position object 4, 0,0,2500
Make object plain 5, 1000,100 : Position object 5, 0,0,3500
Rem YRotate the Plains as desired!!!
yrotate object 2,135
yrotate object 3,278
yrotate object 4,66
yrotate object 5,143
rem Xrotate!!!
xrotate object 2, 80
xrotate object 3, 10
xrotate object 4, 33.7
xrotate object 5, 20
x# = 0 : z# = 0 : y# = 0
a# = 0
Do
Rem Store Old values
oldx# = x# : oldz# = z# : oldy#= y#
Rem handle Player input
if leftkey() = 1 Then a# = Wrapvalue(a#-3)
if rightkey() = 1 Then a# = Wrapvalue(a#+3)
if Upkey() = 1 Then x# = NewXValue(x#,a#,10) : z# = NewZValue(z#,a#,10)
rem set floor
if y# < 0 then y#=0
Rem Position and rotate our object (car ahem)
Position object 1,x#,y#,z#
yrotate object 1, a#
Rem Position Our camera
position camera object position x(1),object position y(1)+50,object position z(1)
set camera to object orientation 1
move camera -100
point camera object position x(1),object position y(1),object position z(1)
rem gravity switch!!
if returnkey()=1
inc grav, 1
if grav>1 then grav=0
endif
Rem Check for collisions
ObjectHit = object collision(1,0)
if ObjectHit > 0
Gosub HandleCollisions
Else
rem gravity
if grav=0
y#= y# - 2.0
endif
EndIf
rem feedback
if grav=1
text 0, 0, "press return to turn off gravity"
else
text 0, 0, "press return to turn on gravity"
endif
sync
Loop
end
HandleCollisions:
Rem get the angle of the object we hit...
ObjectHitAngle# = object Angle y(ObjectHit)
hitangleX#= object angle x(ObjectHit)
Rem Calculate the length of our car vector which is at position x,z from origin oldx,oldz
Rem ` in other words this is the distance from the last position to the new position
vLength# = sqrt((x# - oldx#)^2 + (z# - oldz#)^2 + (y# - oldy#)^2)
if vLength# > 0
Rem define my two vector Objects for the car and the object normal
VX = 0 : VZ = 1 : VY=2
Dim VectorCar#(2) : Dim VectorObj#(2) : Dim VectorOld#(2)
Rem in other words, make out that the Old Pos was 0,0,0 and go from there
VectorCar#(VX) = x# - oldx#
VectorCar#(VY) = y# - oldy#
VectorCar#(VZ) = z# - oldz#
Rem Get the Object normal Vector
VectorObj#(VX) = NewXValue(oldx#,ObjectHitAngle#,vLength#) - oldx#
VectorObj#(VY) = NewYValue(oldy#,hitangleX#,vLength#) - oldy#
VectorObj#(VZ) = NewZValue(oldz#,ObjectHitAngle#,vLength#) - oldz#
Rem Get the scalar DotProduct And....
Rem ... Multiply it by the Object Vector (VectorObj) to
Rem get the component of the car vector that is perpendicular to the surface of the object.
Rem We create a new vector out of this multiplying the object Vector by the DotProduct Scalar
Dim Vn#(2)
Vn#(VX) = ((VectorCar#(VX) * VectorObj#(VX) + VectorCar#(VZ) * VectorObj#(VZ) + VectorCar#(VY) * VectorObj#(VY))/ vLength#^2) * VectorObj#(VX)
Vn#(VZ) = ((VectorCar#(VX) * VectorObj#(VX) + VectorCar#(VZ) * VectorObj#(VZ) + VectorCar#(VY) * VectorObj#(VY))/ vLength#^2) * VectorObj#(VZ)
Vn#(VY) = ((VectorCar#(VX) * VectorObj#(VX) + VectorCar#(VZ) * VectorObj#(VZ) + VectorCar#(VY) * VectorObj#(VY))/ vLength#^2) * VectorObj#(VY)
x# = x# - Vn#(VX)
y# = y# - Vn#(VY)
z# = z# - Vn#(VZ)
EndIF
Return