Hello guys,
This code is if you want to have a cutscene camera or simply have the camera move along a path, this code could be modified for character pathfinding too. I'll try to explain how everything works too:
This is the main code - it comes with comments explaining what each set of commands are for:
MAIN PROJECT FILE
`%ProjectTitle%
`======================
`©%Author%
`======================
`Main Source File
cls
sync on
sync rate 60
`Make the scene and player
play_obj = 1
make object box play_obj, 100,100,100
make matrix 1,1000,1000,70,70
`Create the Camera Nodes and set their object numbers as an array
node_count = 4
dim object(node_count)
object(1) = create_node(910,110,910)
object(2) = create_node(900,120,420)
object(3) = create_node(920,120,320)
object(4) = create_node(-0,300,-300)
`Set up scene values
node = 1
loop# = 0
stop# = 0
speed# = 10
`Make the Camera collider
cam = MakeCameraCollider()
position object cam, 910,110,910
do
`Choose the AIM target for the CAMERA
target# = play_obj
`CHECK the collision between a camera and NODE
if NodeCollision(cam, object(node)) = 1
`IF THE CURRENT COLLIDING NODE IS THE HIGHEST IN NUMBER, THEN STOP IT, IF LOOP IS TOGGLED 'OFF' AND RESET NODE COUNT
if node = node_count and loop# = 0 then stop# = 1 : node = 1
`IF THE CURRENT COLLIDING NODE IS THE HIGHEST IN NUMBER, AND THE LOOP IS TOGGLED 'ON' THEN RESET NODE COUNT TO LOOP
if node = node_count and loop# = 1 then node = 0
`text 0,10,"COLLIDE"
`MOVE TO THE NEXT NODE
inc node, 1
endif
`IF THE CAMERA IS NOT STOPPED, THEN UPDATE ITS MOVEMENT
if stop# = 0 then UpdateCameraCollider(cam, target#, object(node), speed#)
`TOGGLE LOOP 'OFF'
if spacekey() = 1 then loop# = 0
`TOGGLE LOOP 'ON'
if returnkey() = 1 then loop# = 1 : stop# = 0
`HAVE SOME TEXT
text 0,20,"Hello World, it's a good day isn't it?"
`MOVE PLAYER IN A CIRCLE
`move object target#,10
`turn object left target#,3
sync
loop
If you press 'return' you'll turn on 'looping'
If you press 'space' you'll turn off 'looping'
FUNCTIONS
For the individual functions used:
create_Node(x,y,z)
function create_Node(x,y,z)
node = FreeObject()
make object sphere node, 10
position object node,x,y,z
hide object node
endfunction node
x,y,z are just for the coordinates for where you want to create it. Because I'm using 'FREEOBJECT', that means DBP gives you an object number rather than you defining one. If find it tidier. So to make this code work, you need to write something like this:
object = create_node(0,100,500)
the object will be created and the chosen object number will be stored to 'object', you'll notice I made the variable into an array in the main code above. It's best you use arrays as you'll be able to switch nodes much more easily.
The node object is essentially a sphere object created so that the camera may target it and moves itself to that position.
In the main code above, you will find you can just keep adding the line 'Create_node(x,y,z)' as many times as you like (and update the array size) that you will just have more and more nodes added to the scene without changing anything in the loop. So it makes it easy in changing your camera path.
MakeCameraCollider()
function MakeCameraCollider()
collider = FreeObject()
make object sphere collider, 10
hide object collider
endfunction collider
This works in the same way, only that you should only make the one, the camera collider is an object that the camera attaches itself to - so the object will manipulate the camera's position.
Try: cam = MakeCameraCollider()
'cam' will be your object number, which will be needed when it comes to collision with the node object.
UpdateCameraCollider(cam, target, node, speed)
function UpdateCameraCollider(o, target, node, speed#)
position camera object position x(o),object position y(o),object position z(o)
move object o,speed#
point object o, object position x(node), object position y(node), object position z(node)
point camera object position x(target), object position y(target), object position z(target)
endfunction
'cam' (or 'o' in the function) is your camera object. With this function the camera will stay in the same position as its collider. The target is the object at which the camera will point itself (a target can be a node if you wanted it to be), 'node' will specify the node the camera will move to and 'speed' will be how quickly it gets there.
nodeCollision(cam, node)
function nodeCollision(cam, node)
x=0
if object collision (cam, node) = 1 then x = 1
endfunction x
It works exactly like 'object collision(obj1, obj2)', but I think having a function labelled for node collision, it'll just make it easier to refer back to. The first object should be your camera and the second a 'node'. So:
if nodeCollision( cam, object(1)) = 1 then end
This would essentially be; if the camera collides with node '1' then the game will end.
Finally; this function is not mine and my apologies for forgetting who made it originally, but it's the 'FREE OBJECT' function:
free_object()
`FREE OBJECT - PICK AN OBJECT THAT IS FREE
function FreeObject()
repeat
inc i
if object exist(i)=0 then found=1
until found
endfunction i
You won't need to use this function, but my functions use it - essentially it checks what object numbers are free and returns it as a value - so: object = FreeObject() means that 'object' is the variable that will carry the 'spare' object variable.