Hi
Thanks to StabInTheDarkSoftware, I have created a little code example for a platform game.
Tell me what do you think about this code. If you want to improve this example, please modify the code, and post it
.
Features ok :
- canons and bullet
- collect stars or money
- several surface with differents parameters : fire -> damage
Todolist :
- ennemies
- trap which appears
- ladder
- mecanism : door+keys
// Project: 3D platformer example
// Created: 2015-12-07
// code : based on an example from StabInTheDarkSoftware, modified by Blendman
// set window properties
SetWindowTitle( "3D platformer example" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
Create3DPhysicsWorld()
init()
a = 0
Collision = 0
TimeDamage = 0
TimeBullet = 0
playerTouch = 0
do
CharacterControllerInput(player,75.0)
Step3DPhysicsWorld()
//Position Camera after we step physics
setcameraposition(1,GetObjectWorldX(playerCam),GetObjectWorldY(playerCam),GetObjectWorldZ(playerCam))
setcameralookat(1,getobjectx(player),getobjecty(player),getobjectz(player),0)
inc a
Create3DPhysicsStaticBody(23)
SetObjectPosition(23,50+sin(a)*20,GetObjectY(23),0)
oldX = GetObjectX(player)
oldy = GetObjectY(player)
oldZ = GetObjectZ(player)
NewX = GetObjectX(23)
NewY = GetObjectY(23)
NewZ = GetObjectZ(23)
// id= ObjectRayCast( 23, oldx, oldy, oldz, oldx+15,oldy+15,oldz+15)
id = ObjectSphereCast( 0, oldx, oldy, oldz, oldx, oldy, oldz,15)
print("ID collision: "+str(id))
if id >=50 and id <=50+NbStars
Delete3DPhysicsBody(id)
Deleteobject(id)
inc Bonus
else
Select id
case 500 // bullet
if playerTouch =0
PlayerTouch = -1
life = life - 6
// if don't want the player move thank to the bullet, uncomment those 2 lines
// Delete3DPhysicsBody(500)
// DeleteObject(500)
endif
endcase
case 23 // moving platform
if collision = 0
collision = 1
startX = GetObjectX(player) - GetObjectX(23)
StartY = GetObjectY(player) - GetObjectY(23)
StartZ = GetObjectZ(player) - GetObjectZ(23)
endif
if GetObjectY(player)>=GetObjectY(23)+8 and GetObjectY(player)<=GetObjectY(23)+15
print("ok")
Set3DPhysicsCharacterControllerPosition(player,startX+GetObjectX(23),GetObjectY(player),StartZ+GetObjectZ(23))
endif
endcase
case 30 // damage surface
if timedamage<=0
timedamage = 60
life = life - 10
if life <=0
message("you are dead!")
life = 50
Set3DPhysicsCharacterControllerPosition(player,0,20,0)
endif
else
dec timedamage
endif
endcase
endselect
endif
if id = player
collision = 0
endif
if ObjectSphereCast(player,GetObjectX(24),GetObjectY(24),GetObjectZ(24),GetObjectX(24),GetObjectY(24),GetObjectZ(24),100)
// canon send bullet
if TimeBullet <= 0
if GetObjectExists(500)
Delete3DPhysicsBody(500)
DeleteObject(500)
endif
TimeBullet = 60
CreateObjectBox(500,5,5,5)
SetObjectPosition(500,GetObjectX(24),GetObjectY(24),GetObjectZ(24))
Create3DPhysicsKinematicBody(500)
SetObjectLookAt(500,oldX,oldY,OldZ,0)
else
dec TimeBullet
endif
endif
if GetObjectExists(500)
if TimeBullet <= 0 or playerTouch<=-10
playerTouch = 0
Delete3DPhysicsBody(500)
DeleteObject(500)
else
dec TimeBullet
if playerTouch <0 and playertouch>-10
dec playerTouch
endif
endif
MoveObjectLocalZ(500,2)
endif
Sync()
loop
// init
function init()
SetClearColor(120,120,120)
SetPrintSize(15)
global ground=200
global player=1
global playerCam=201
Global StartX,StartY,StartZ,oldX,oldy,oldZ,NewX,NewY,NewZ
global rotationInc,finalRotation as float
rotationInc = 1.0
Global Life,Bonus as integer
Life = 100
// Create some object
GroundW = 1000
GroundH = 1000
// just add some static cube
for i = 0 to 30
x# = random(0,GroundW/2) - random(0,GroundW/2)
y# = random(0,10) - random(0,10)
z# = random(0,GroundH/2) - random(0,GroundH/2)
make_my_object(i+201,x#,GetObjectY(Ground)+y#,z#,20,30,20,120,120,120,0)
next
// create stars (bonus to collect)
global NbStars = 20
For i = 0 to NbStars
x# = random(0,GroundW/2) - random(0,GroundW/2)
y# = 10
z# = random(0,GroundH/2) - random(0,GroundH/2)
make_my_object(i+50,x#,GetObjectY(Ground)+y#,z#,10,10,10,190,190,0,3)
Next
// create some moving platform
make_my_object(23,50,10,0,20,5,20,80,120,120,0)
// a canon
make_my_object(24,-50,10,-50,10,20,10,80,80,120,0)
// create damage surface
make_my_object(30,150,5,120,50,5,80,120,60,60,0)
// create the ground
make_my_object(ground,0,0,0,GroundW,5,GroundH,90,120,70,0)
// create the player
make_my_object(player,0,30,0,15,15,15,200,200,200,1)
// the cube fixed to the player, camera will use its position/rotation to have a third person camera
createobjectbox(PlayerCam,1,1,1)
SetObjectPosition(PlayerCam,0,100,-180)
FixObjectToObject(PlayerCam,Player)
setcameraposition(1,getobjectx(playerCam),getobjecty(playerCam),getobjectz(playerCam))
setcameralookat(1,getobjectx(player),getobjecty(player),getobjectz(player),0)
endfunction
function make_my_object(objid,x#,y#,z#,sx#,sy#,sz#,r,g,b,p)
createobjectbox(objid,sx#,sy#,sz#)
setobjectposition(objid,x#,y#,z#)
setobjectcolor(objid,r,g,b,255)
if p=0
createobjectphysics(objid,2,1)
elseif p=2
createobjectphysics(objid,2,2)
endif
if p=1
`createobjectphysics(objid,2,1)
createmycontroller(objid)
endif
endfunction
function createobjectphysics(myobj,myphyshape,myphybody)
select myphybody
case 1
Create3DPhysicsStaticBody(myobj)
endcase
case 2
Create3DPhysicsDynamicBody(myobj)
endcase
endselect
select myphyshape
case 1
SetObjectShapesphere(myobj)
endcase
case 2
SetObjectShapebox(myobj)
endcase
endselect
endfunction
function createmycontroller(objid)
characterOffsetVec = CreateVector3( 0.0, 0.0, 0.0 )
//This vector indicates the axis your model faces forward.
//For example if your model faces down the - Z axis use this vector.
objectOrientationVec = CreateVector3( 0.0, 0.0, 0.0 )
//The crouchScale parameter is 0.0 to 1.0, for examle 0.75 is 75% of the models standing height.
Create3DPhysicsCharacterController(objid, 1, characterOffsetVec, objectOrientationVec, 0.75 )
DeleteVector3( characterOffsetVec )
DeleteVector3( objectOrientationVec )
Set3DPhysicsCharacterControllerPosition(objid,getobjectx(player),getobjecty(player),getobjectz(player))
endfunction
// update
function CharacterControllerInput( objID as integer, walkVelocity as float)
if GetRawKeyPressed( 27 )
end
endif
move = 0
if GetKeyboardExists()
//Stops movement when no key is pressed.
Move3DPhysicsCharacterController( objID, 0, WalkVelocity)
//up key Forwards
if GetRawKeyState( 38 )
Move3DPhysicsCharacterController( objID, 1, WalkVelocity )
move = 1
endif
//down Backwards
if GetRawKeyState( 40 )
Move3DPhysicsCharacterController( objID, 2, WalkVelocity )
move = 1
endif
//Q Key For Rotate
if GetRawKeyState( 37 )
finalRotation = finalRotation - rotationInc
endif
//E Key For Rotate
if GetRawKeyState( 39 )
finalRotation = finalRotation + rotationInc
endif
// R Strafe Left
if GetRawKeyState( 82 )
Move3DPhysicsCharacterController( objID, 3, WalkVelocity )
move = 1
endif
// T Strafe Right
if GetRawKeyState( 84 )
Move3DPhysicsCharacterController( objID, 4, WalkVelocity )
move = 1
endif
//SpaceBar Jump
if GetRawKeyPressed( 32 )
Jump3DPhysicsCharacterController( objID )
endif
Rotate3DPhysicsCharacterController( objID, finalRotation )
endif
if move = 1
startX = GetObjectX(player) - GetObjectX(23)
StartY = GetObjectY(player) - GetObjectY(23)
StartZ = GetObjectZ(player) - GetObjectZ(23)
endif
Print(ScreenFPS())
print(str(oldX)+"/"+str(oldY)+"/"+str(oldZ)+" | "+str(NewX)+"/"+str(NewY)+"/"+str(NewZ))
print("Life: "+str(Life)+"/100")
print("Bonus: "+str(Bonus))
endfunction
A question :
We have to use "Create3DPhysicsStaticBody(object) " to update the position of the collision shape of each static objects (if we want that this object move for example) at each frame, or is there another technic to do that ?
[EDIT ]
add :
- canon+bullet
- lava surface
- bonus to collect
http://www.dracaena-studio.com