Ah, yeah, of course. Completely overlooked that. Also corrected a bug and added some other play stuff like weighted spheres( lighter color means lighter weight) and added friction settings, but apparently doesn't work as simple all together.
I'll really need to invest and do some reading on the whole physics thing for doesn't behave as I think it would, only thing is:
where, for docs are limited.
I would expect to be able to add weight to the player too as I've seen the ragdoll example, but somehow that didn't work here. Also I think should be possible to make objects so heavy that can't be moved or hardly, but I wonder how high mass would have to be set for such an object then for didn't succeed.
// Project: 3Dphysicscharacter
// Created: 2018-04-04
// By Blendman
// Added turning left/right, crouch/uncrouch, nose to player to indicate front , a pooltable like env to test physics and spheres with diff mass.
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "3Dphysicscharacter" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
Create3DPhysicsWorld()
SetAntialiasMode(1)
// create a ground
ground=CreateObjectBox(200,2,200)
Create3DPhysicsStaticBody(ground)
SetObjectColor(ground,100,150,50,255)
SetObject3DPhysicsRestitution( ground, 0.75 ) // lets the surface bounce off objects such as balls in this case.
friction = 100
rollingfriction=100
SetObject3DPhysicsFriction( ground, friction )
SetObject3DPhysicsRollingFriction( ground, friction )
hill = CreateObjectCone(20,80,20)
SetObjectPosition(hill, 0,10.0,0)
Create3DPhysicsStaticBody(hill)
SetObjectColor(hill,100,150,50,255)
SetObject3DPhysicsRestitution( hill, 0.75 ) // lets the surface bounce off objects such as balls in this case.
friction = 100
rollingfriction=100
SetObject3DPhysicsFriction( hill, friction )
SetObject3DPhysicsRollingFriction( hill, friction )
//Create walls.
wall_back=CreateObjectBox(200,10,10)
SetObjectPosition(wall_back, 0,5,95)
SetObjectColor(wall_back,100,150,50,255)
Create3DPhysicsStaticBody(wall_back)
SetObject3DPhysicsRestitution( wall_back, 0.75 ) // lets the surface bounce off objects such as balls in this case.
wall_front=CreateObjectBox(200,10,10)
SetObjectPosition(wall_front, 0,5,-95)
SetObjectColor(wall_front,100,150,50,255)
Create3DPhysicsStaticBody(wall_front)
SetObject3DPhysicsRestitution( wall_front, 0.75 ) // lets the surface bounce off objects such as balls in this case.
wall_left =CreateObjectBox( 10,10,200)
SetObjectPosition(wall_left, -95,5,0)
SetObjectColor(wall_left,100,150,50,255)
Create3DPhysicsStaticBody(wall_left)
SetObject3DPhysicsRestitution( wall_left, 0.75 ) // lets the surface bounce off objects such as balls in this case.
wall_right=CreateObjectBox(10,10,200)
SetObjectPosition(wall_right, 95,5,0)
SetObjectColor(wall_right,100,150,50,255)
Create3DPhysicsStaticBody(wall_right)
SetObject3DPhysicsRestitution( wall_right, 0.75 ) // lets the surface bounce off objects such as balls in this case.
// Create 6 crate on the ground
For i=0 to 5
mass=random(0,255)
n = 2+i
CreateObjectBox(n,10,10,10)
SetObjectPosition(n, random(0,80)-random(0,80),150,random(0,80)-random(0,80))
Create3DPhysicsDynamicBody(n)
//SetObjectColor(n,100,100,200,255)
SetObjectColor(n, mass,mass,mass,mass)
SetObject3DPhysicsMass( n, (255-mass)*100 ) // the lighter the color the lighter the object
friction=(255-mass)/10 // 0.8
rollingfriction= (255-mass)/10 // 0.7
SetObject3DPhysicsFriction( n, friction )
SetObject3DPhysicsRollingFriction( n, rollingfriction )
next
// Create 6 spheres on the ground
For i=6 to 12
n = 2+i
CreateObjectSphere(n,10,10,10)
SetObjectPosition(n, random(0,80)-random(0,80),150,random(0,80)-random(0,80))
Create3DPhysicsDynamicBody(n)
mass=random(0,255)
SetObjectColor(n, mass,mass,mass,mass)
//SetObjectTransparency(n, 1) //Random(0,1))
directionX=0
directionY=-1
directionZ=0
initialSpeed=1
friction=0// 0.8
rollingfriction=0 // 0.7
SetObject3DPhysicsMass( n, (255-mass)*100 ) // the lighter the color the lighter the object
SetObject3DPhysicsLinearVelocity( n, directionX, directionY, directionZ, initialSpeed )
SetObject3DPhysicsFriction( n, friction )
SetObject3DPhysicsRollingFriction( n, rollingfriction )
next
// Create the character
player = 15
n= player
//CreateObjectBox(n,10,20,10)
CreateObjectCapsule(n,10,20,10)
// Create3DPhysicsKinematicBody(n)
SetObjectColor(n,150,50,50,255)
x = 0
z = 0
SetObjectPosition(player, x, 100, z)
nose= CreateObjectSphere(4,1.2,12):SetObjectColor( nose,255,0,0,255) // //250,150,155,255=pink
SetObjectPosition( nose,0,1.2,5.5)
SetObjectCollisionMode(nose,0) //prevent self hiting
FixObjectToObject(nose,n)
// SetObject3DPhysicsMass( n, 40 ) // the lighter the color the lighter the object
// To create a character
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(n, 1, characterOffsetVec, objectOrientationVec, 0.50)
DeleteVector3( characterOffsetVec )
DeleteVector3( objectOrientationVec )
Set3DPhysicsCharacterControllerPosition(n, getobjectx(n), getobjecty(n), getobjectz(n))
// Debug3DPhysicsCharacterController(n,1)
Stand3DPhysicsCharacterController( n )
Set3DPhysicsCharacterControllerStepHeight( n,12.0 )
Set3DPhysicsCharacterControllerJumpSpeed( n, 7.0 )
//SetObjectCastShadow( n, 1 )
//SetObjectReceiveShadow( n, 1 )
WalkVelocity = 40
Angle=1
// Set camera
SetCameraPosition(1,0,120,-180)
SetCameraLookAt(1,0,0,0,0)
do
Print( ScreenFPS() )
// move player
Move3DPhysicsCharacterController( n, 0, WalkVelocity)
n = player
if GetRawKeyState(39) or GetRawKeyState(68) // right
x = x + 1
// SetObjectPosition(player, x, 10, z)
Move3DPhysicsCharacterController( n, 4, WalkVelocity)
endif
if GetRawKeyState(37) or GetRawKeyState(65) // left
x = x - 1
// SetObjectPosition(player,x, 10, z)
Move3DPhysicsCharacterController( n, 3, WalkVelocity)
endif
if GetRawKeyState(38) or GetRawKeyState(87) // up
z = z + 1
//SetObjectPosition(player, x, 10, z)
Move3DPhysicsCharacterController( n, 1, WalkVelocity)
endif
if GetRawKeyState(40) or GetRawKeyState(83) // down
z = z - 1
Move3DPhysicsCharacterController( n, 2, WalkVelocity)
//SetObjectPosition(player,x, 10, z)
endif
//Set3DPhysicsCharacterControllerPosition(n, getobjectx(n), getobjecty(n), getobjectz(n))
if GetRawKeyState(81) //q turn left
Angle = Angle - 5
Rotate3DPhysicsCharacterController( n, Angle)
endif
if GetRawKeyState(69) // e turn right
Angle = Angle + 5
Rotate3DPhysicsCharacterController( n, Angle)
endif
//jump
if GetRawKeyPressed(32) // space=jump
Jump3DPhysicsCharacterController(n)
Else
endif
If GetRawKeyState(67) // c=crouch
SetObjectScale(n,1.0,0.5,1.0)
Crouch3DPhysicsCharacterController(n)
Rotate3DPhysicsCharacterController( n, Angle) // required , for controller returns to original angle.
Else
Stand3DPhysicsCharacterController(n)
SetObjectScale(n,1.0,1.0,1.0)
Rotate3DPhysicsCharacterController( n, Angle)
EndIf
Step3DPhysicsWorld()
Sync()
loop
[EDIT] Also I've noticed the sphere's do not bounce as I expected they would. [/EDIT]