Posted: 26th Nov 2002 18:50
It would be great if someone clever could update this for all 3 dimensions:

+ Code Snippet
Set window on
set window layout 1,1,1
sync on

Rem Declare Constants
Dim VectorCar#(2) : Dim VectorObj#(2) : Dim VectorOld#(2) : Dim Vn#(2)
VX = 0 : VZ = 1

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




x# = 0 : z# = 0
a# = 0
Do
   Rem Store Old values
   oldx# = x# : oldz# = z#

   Rem hanlde 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 Position and rotate our object (car ahem)
   Position object 1,x#,0,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 Check for collisions
   ObjectHit = object collision(1,0)
   if ObjectHit > 0
      Print "HIITNG!! "; ObjectHit
      Gosub HandleCollisions
   EndIf

   sync
Loop

end

HandleCollisions:
   Rem get the angle of the object we hit...
   ObjectHitAngle# = object Angle y(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)

   if vLength# > 0

      Rem define my two vector Objects for the car and the object normal
      VX = 0 : VZ = 1
      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#(VZ)    = z# - oldz#

      Rem Get the Object normal Vector
      VectorObj#(VX) = NewXValue(oldx#,ObjectHitAngle#,vLength#) - oldX#
      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))/ vLength#^2) * VectorObj#(VX)
      Vn#(VZ) = ((VectorCar#(VX) * VectorObj#(VX) + VectorCar#(VZ) * VectorObj#(VZ))/ vLength#^2) * VectorObj#(VZ)


      print "x# = ";x#;" and z# = ";z#
      x# = x# - Vn#(VX)
      z# = z# - Vn#(VZ)


      `RemStart
      set cursor 0,0
      Print "oldx# = ";oldx#;" and oldz# = "; oldz#

      Print "ObjectHitAngle# = ";ObjectHitAngle#
      Print "Length of vector = "; vLength#
      Print "VectorCar#(";VectorCar#(VX);",";VectorCar#(VZ);")"
      Print "VectorObj#(";VectorObj#(VX);",";VectorObj#(VZ);")"
      Print "DotProduct ( Scalar value = cos(theta) )= ";(VectorCar#(0) * VectorObj#(0) + VectorCar#(1) * VectorObj#(1))/ vLength#^2
      Print ""
      Print "Explination of values:"
      Print "Values range from 1 to -1."
      Print "If the two input vectors are pointing in the same direction, then the return value will be 1."
      Print "If the two input vectors are pointing in opposite directions, then the return value will be -1."
      Print "If the two input vectors are at right angles, then the return value will be 0."
      print ""
      Print "Our New Vector Vn = ("; Vn#(VX);",";Vn#(VZ);")"
      Print "So we can find where we are supposed to be by TAKING AWAY the new vector (VN#) from VectorCar#"
      Print "(VectorCar# - Vn#)"
      print "or adding the opposite of vector Vn"
      Print "x = ";x#;" and z# = ";z#
      `RemEnd
   EndIF

Return
Posted: 26th Nov 2002 19:46
That is some great code! Though if you hit the planes directly on the ends you get stuck in side them.
Posted: 26th Nov 2002 20:02
Though if you hit the planes directly on the ends you get stuck in side them.

Maybe that's only with plains, not cubes etc?
Posted: 26th Nov 2002 20:24
Yeah, I've found it works REALLY great if you restrict the collision to one side only (ie the result of the Dot Product is 0 to 1 OR -1 to 0) and ignore the other collision, effectively saying "You can collided only with the defined object Normal, not the back of it" as it stops the "Getting your arse stuck in the wall" syndrom.

If anyone's interested I'll do a tutorial on building these plains from a 3ds/x object limbs. Works well for a path or race track.
Posted: 26th Nov 2002 20:56
have you ever thought about using the 3d math commands in DBpro, it might speed it up even more
Posted: 27th Nov 2002 0:30
"If anyone's interested I'll do a tutorial on building these plains from a 3ds/x object limbs", yes please, sounds cool. Hey, actually a tutorial on the 3dmaths commands (or just some examples) would be sweeeeet.

Cheers
Posted: 28th Nov 2002 5:17
Okies just as soon as time allows (tommorrow?). How about an oval race track? Always good for a laugh...
Posted: 28th Nov 2002 22:24
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 Snippet
Set 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
Posted: 28th Nov 2002 22:45
lol, i cant believe how stupid i was, here is the version of the code which has gravity working, its fun sliding down objects

+ Code Snippet
Set 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 gravity
   if grav=0
   y#= y# - 2.0
   endif

   Rem Check for collisions
   ObjectHit = object collision(1,0)
   if ObjectHit > 0
      Gosub HandleCollisions
   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
Posted: 29th Nov 2002 15:54
Hi there, I haven't tried the latest version (yet, but will do!) but when I tried the original code on my screen was just a spinning cube, no other objects.. perhaps I should re-try with patch 3 (haven't run it again since installing it).. may just be me. I can try again later,

Cheers
Posted: 29th Nov 2002 17:23
the first version didn't limit the frame rate so i added in sync rate 30 to slow it down
Posted: 30th Nov 2002 1:36
thanks!, I was looking for code like this
Posted: 6th Dec 2002 9:21
Tutorial added [url]www.actinic.hiway.co.uk[/url]
Posted: 6th Dec 2002 11:04
Nice tutorial, but i was more interested in your game
Posted: 17th Dec 2002 3:34
Oh great! After all that effort on the tutorial - lol

I'll upload some new screenies of the game just for you then