It's certainly not necessary to use NG for something as simple as this
Stop-collision
` Example 1.
` Move a ball around a randomly generated arena
` Object numbers from 10 to 99 will not allow you to move through them
` All other object numbers will
sync on
sync rate 30
autocam off
hide mouse
` Grab a black image for the matrix 'floor'
get image 1, 0, 0, 1, 1
` Make a simple flat floor
make matrix 1, 100, 100, 1, 1
prepare matrix texture 1, 1, 1, 1
update matrix 1
position matrix 1, -50.0, -1.0, -50.0
` Ball to move around
make object sphere 1, 2.0
CreateWalls()
SaveXPos as float
SaveZPos as float
` Main loop
repeat
SaveXPos = object position x(1)
SaveZPos = object position z(1)
text 0, 0, "Current X : " + str$( SaveXPos )
text 0, 15, "Current Z : " + str$( SaveZPos )
` Rotate the ball using the mouse
rotate object 1, 0.0, wrapvalue( object angle y(1) + mousemovex() ), 0.0
` Allow the ball to be moved
if upkey() = 1 then move object 1, 0.25
if downkey() = 1 then move object 1, -0.25
if leftkey() = 1 then move object left 1, 0.25
if rightkey() = 1 then move object right 1, 0.25
` If a wall collision occurred, move the ball back to the saved position
` Scan for collision between the ball (object 1) and walls (objects 10 to 99)
CollisionObject = 0
for Obj = 10 to 99
if object exist(Obj) = 1
if object collision(1, Obj) = 1
CollisionObject = Obj
exit
endif
endif
next Obj
` Alternate method - detect collision with *all* objects
` CollisionObject = object collision( 1, 0 )
if CollisionObject > 0
text 0, 30, "Collision with object " + str$( CollisionObject ) + " detected"
if CollisionObject >= 10 and CollisionObject < 100
position object 1, SaveXPos, 0.0, SaveZPos
endif
endif
` Position and rotate the camera to look at the ball (simple method)
position camera object position x(1), object position y(1), object position z(1)
rotate camera 35.0, object angle y(1), 0.0
move camera -50.0
sync
until lower$( inkey$() ) = "q"
end
function CreateWalls()
local o as integer
` Walls around the floor edges
make object box 10, 1.0, 2.0, 100.0
position object 10, -50.0, 0.0, 0.0
make object box 11, 1.0, 2.0, 100.0
position object 11, 50.0, 0.0, 0.0
make object box 12, 101.0, 2.0, 1.0
position object 12, 0.0, 0.0, -50.0
make object box 13, 101.0, 2.0, 1.0
position object 13, 0.0, 0.0, 50.0
for o = 20 to 59
make object box o, 10.0, 2.0, 1.0
if rnd(1) = 0 then rotate object o, 0.0, 90.0, 0.0
position object o, rnd(90)-45.0, 0.0, rnd(90)-45.0
next o
endfunction
Sliding Collision:
` Example 2.
` Move a ball around a randomly generated arena
` This uses the built-in DBPro sliding collision
` Object numbers from 10 to 99 will not allow you to move through them
` All other object numbers will
sync on
sync rate 30
autocam off
hide mouse
` Grab a black image for the matrix 'floor'
get image 1, 0, 0, 1, 1
` Make a simple flat floor
make matrix 1, 100, 100, 1, 1
prepare matrix texture 1, 1, 1, 1
update matrix 1
position matrix 1, -50.0, -1.0, -50.0
` Ball to move around
make object sphere 1, 2.0
` Create a collision box around this ball
` - I've used 0.8 as the radius instead of 1.0 to make it easier to get through gaps and narrow corridors
set object radius 1, 0.8
CreateWalls()
` No longer need to keep track of the previous position
` Main loop
repeat
` No storing of the previous position
text 0, 0, "Current Position : " + PrettyNumber(object position x(1)) + "/" + PrettyNumber(object position z(1))
` Rotate the ball using the mouse
rotate object 1, 0.0, wrapvalue( object angle y(1) + mousemovex() ), 0.0
` Allow the ball to be moved
if upkey() = 1 then move object 1, 0.25
if downkey() = 1 then move object 1, -0.25
if leftkey() = 1 then move object left 1, 0.25
if rightkey() = 1 then move object right 1, 0.25
` If a wall collision occurred, move the ball back to the saved position
` Scan for collision between the ball (object 1) and walls (objects 10 to 99)
CollisionObject = 0
for Obj = 10 to 99
if object exist(Obj) = 1
if object collision(1, Obj) = 1
CollisionObject = Obj
exit
endif
endif
next Obj
` Alternate method - detect collision with *all* objects
` CollisionObject = object collision( 1, 0 )
if CollisionObject <> 0
text 0, 30, "Collision with object " + str$( CollisionObject ) + " detected"
` Display the collision details
text 0, 45, "Collision coordinated : " + PrettyNumber( get object collision x() ) + "/" + PrettyNumber( get object collision z() )
if CollisionObject >= 10 and CollisionObject < 100
` Adjust the object position using the sliding collision data
position object 1, object position x(1) - get object collision x(), 0.0, object position z(1) - get object collision z()
endif
endif
` Position and rotate the camera to look at the ball (simple method)
position camera object position x(1), object position y(1), object position z(1)
rotate camera 35.0, object angle y(1), 0.0
move camera -50.0
sync
until lower$( inkey$() ) = "q"
end
function CreateWalls()
local o as integer
` Walls around the floor edges
make object box 10, 1.0, 2.0, 100.0
position object 10, -50.0, 0.0, 0.0
make object box 11, 1.0, 2.0, 100.0
position object 11, 50.0, 0.0, 0.0
make object box 12, 101.0, 2.0, 1.0
position object 12, 0.0, 0.0, -50.0
make object box 13, 101.0, 2.0, 1.0
position object 13, 0.0, 0.0, 50.0
for o = 20 to 59
make object box o, 10.0, 2.0, 1.0
if rnd(1) = 0 then rotate object o, 0.0, 90.0, 0.0
position object o, rnd(90)-45.0, 0.0, rnd(90)-45.0
next o
endfunction
function PrettyNumber(N as float)
local Sign as string
local S as string
local i as integer
if N < 0
N = 0.0 - N
Sign = "-"
else
Sign = "+"
endif
S = str$( N + 0.005 )
for i=1 to len( S )
if mid$(S, i) = "."
S = Sign + right$( "00" + left$( S, i+2 ), 5 )
exitfunction S
endif
next i
S = Sign + right$( "00" + S + ".00", 5 )
endfunction S
The sliding collision code needs a bit of work, but that would over-complicate the code and detract from what it is - a simple example.
*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins, source and the Interface library for Visual C++ 6, .NET and now for Dev-C++
http://www.matrix1.demon.co.uk