I got a good working player control. except that the gravity is way off, can any one help me?
would like it if the player could fall at 9.8g, which is the speed at which ever object falls at.
here is the code
` by sindore
`credit to Lukas W for some of the player control code
sync on : sync rate 60
autocam off
set camera range 1,1000
`set normalization on
hide mouse
position camera 0,0,-50:` temp delete position camera
ink rgb(0,0,0),0
` start physic
phy start
` set up player and contorls
gosub player_setup
`*********************************************************
`*********************************************************
`*********************************************************
`*** make a tempary land ***
land_test()
`*********************************************************
`*********************************************************
`*********************************************************
` start the loop
do
` this is a temp
if scancode()=2
gosub camera_rotation :`this is a temp camera comand, use this to rotate the camera around the player, to be delete at a later date
else
gosub camera_on_player
gosub physic_collision
endif
` control the player with arrow keys
player_controls()
`PHY SET RIGID BODY GRAVITY player.object_ID,9.8
phy set gravity 0.0,-9.8,0.0
` update physics
phy update
` end loop and update sync
sync
loop
`*********************************************************
`*********************************************************
`*********************************************************
` end physic
phy end
` end program
end
`*********************************************************
`*********************************************************
`*********************************************************
`*** make a tempary land ***
function land_test()
make object cube 1000,1
scale object 1000,5000,10,5000
make object cube 2,1
make mesh from object 1,2
delete object 2
for c = 1 to 4
add limb 1000,c,1
scale limb 1000,c,10,5000,10
next c
offset limb 1000,1,0.4,2.5,0.4
offset limb 1000,2,-0.4,2.5,-0.4
offset limb 1000,3,-0.4,2.5,0.4
offset limb 1000,4,0.4,2.5,-0.4
delete mesh 1
position object 1000,0,0,0
phy make rigid body static mesh 1000,"test_level_01.x"
phy load rigid body static mesh 1000,"test_level_01.x"
endfunction 1000
`*********************************************************
`*********************************************************
`*********************************************************
` set up player and contorls
` start of the gosub
player_setup:
` make player type data
type player_type :` this is the name of the type
` the number that player is given
object_ID as integer
` the meterial the player is made of
Material as integer
` player position
Xpos# as float
Ypos# as float
Zpos# as float
` player size
Xsize# as float
Ysize# as float
Zsize# as float
` player angles
Xang# as float
Yang# as float
Zang# as float
` move player xyz
moveX# as float
moveY# as float
moveZ# as float
` move dirction xyz
moveDirX# as float
moveDirY# as float
moveDirZ# as float
`
CurrentVelocityX# as float
CurrentVelocityY# as float
CurrentVelocityZ# as float
` jumping speed
jumpSpeed# as float
` the speed at whitch the player looks
LookSpeed# as float
` the weight / mass
Mass# as float
` speed at whitch the player moves
Speed# as float
`
energy# as float
`The gun bobbing values
gunBobSpeed# as float
gunBobMove# as float
gunBobHeight# as float
gunBobAng# as float
gunTurnAng# as float
gunLookAng# as float
`
gunOffsetX# as float
gunOffsetY# as float
gunOffsetZ# as float
` mouse move for camera control
mouseMovementX# as float
mouseMovementY# as float
` test to see if player object exists
player_exist as boolean
` test to see if player physic exists
phy_play_exist as boolean
` to let you know if the player is jumping or not
IsJumping as boolean
endtype
global player as player_type :` aplie type values to player globals
`*** set values for player ***
` player object number
player.object_ID = 1
` position player at start position
player.Xpos# = 0.0
player.Ypos# = 5.0 :`this will need to edited dipending on where you wont the player to spawn
player.Zpos# = 0.0
` scale the player
player.Xsize# = 0.0
player.Ysize# = 9.0
player.Zsize# = 0.0
` player rotation
player.Xang# = 0.0
player.Yang# = 0.0
player.Zang# = 0.0
` the speed at which the play jumps
player.JumpSpeed# = 70.0
` the speed at whitch the player looks
player.LookSpeed# = 0.2
` weight of player
player.Mass# = 12.0
` speed at whitch the player moves
player.speed# = 25 :`120.0
`energy that the player has stored within him/her
`player.energy# = 0.5
`The gun bobbing values
player.gunBobSpeed# = 6.0 -2.0
player.gunBobMove# = 3.0 -2.75
player.gunBobHeight# = 1.5 -1.0
player.gunBobAng# = 0.0
player.gunTurnAng# = 0.0
player.gunLookAng# = 0.0
player.gunOffsetX# = 15.0-13
player.gunOffsetY# = -20.0+18.75
player.gunOffsetZ# = 20.0-16
`*********************************
`*********************************
`*** make player material ***
player.Material = 1
Phy Make Material player.Material, "Player 1"
Phy Set Material Dynamic Friction player.Material, 0.01
Phy Set Material Static Friction player.Material, 0.01
Phy Set Material Restitution player.Material, 0.01
Phy Build Material player.Material
`*** make the player man object ***
`make a player object
make object sphere player.object_ID,1
scale object player.object_ID,100,100+player.Ysize#,100
` make temp object and then delete it
make object cube 2,1
make mesh from object 1,2
delete object 2
` add gun limb
add limb 1,1,1
scale limb 1,1,100,100,200
`offset limb 1,1,0.1,0,0.1
delete mesh 1
`remend
`*** set exist settings ***
` test to see if player object exists
player.player_exist = object exist ( player.object_ID )
` test to see if player physic exists
player.phy_play_exist = Phy Get Rigid Body Exist( player.object_ID )
` end of make player object funtion
` setup player physic
player_stand()
return :`end of the gosub
`*********************************************************
`*********************************************************
`*********************************************************
function player_stand()
If player.phy_play_exist = 1
Phy Delete Rigid Body player.object_ID
EndIf
Phy Make Rigid Body Dynamic Capsule player.object_ID,player.Material
Phy Set Rigid Body Position player.object_ID, player.Xpos#, player.Ypos#, player.Zpos#
Phy Set Rigid Body Mass player.object_ID, player.Mass#
Phy Set Rigid Body Rotation player.object_ID, 0.0, player.Yang#, 0.0
phy set rigid body ccd player.object_ID, 1
`player.isStanding =1
endfunction
`*********************************************************
`*********************************************************
`*********************************************************
function player_controls()
`Store mouse movement
player.mouseMovementX# = mousemovex() / 2
player.mouseMovementY# = mousemovey() / 2
`Turn player
player.Yang# = WRAPVALUE(player.Yang# + player.mouseMovementX#*player.lookSpeed#)
player.Xang# = WRAPVALUE(player.Xang# + player.mouseMovementY#*player.lookSpeed#)
` find out the players position
player.Xpos# = object position x(player.object_ID)
player.Ypos# = object position y(player.object_ID)
player.Zpos# = object position z(player.object_ID)
` stops players mouse movement from going upside down
if player.Xang# >=100 and player.Xang# <=120 then player.Xang# = 100 :` (100,120,100) my old settings
if player.Xang# >=180 and player.Xang# <=280 then player.Xang# = 280 :` (180,280,280)
`Control movement using arrow keys
player.MoveX# = 0.00
player.MoveZ# = 0.00
IF UPKEY()+DOWNKEY()+LEFTKEY()+RIGHTKEY() > 0 or KeyState(17)+KeyState(31)+KeyState(30)+KeyState(32) > 0
IF UPKEY() = 1 or KeyState(17) = 1 :`W
INC player.moveX#,SIN(player.Yang#)*player.speed#
INC player.moveZ#,COS(player.Yang#)*player.speed#
ENDIF
IF DOWNKEY() = 1 or KeyState(31) = 1 :`S
DEC player.moveX#,SIN(player.Yang#)*player.speed#
DEC player.moveZ#,COS(player.Yang#)*player.speed#
ENDIF
IF RIGHTKEY() = 1 or KeyState(32) = 1 :`D
INC player.moveX#,COS(player.Yang#)*player.speed#
INC player.moveZ#,-SIN(player.Yang#)*player.speed#
ENDIF
IF LEFTKEY() = 1 or KeyState(30) = 1 :`A
DEC player.moveX#,COS(player.Yang#)*player.speed#
DEC player.moveZ#,-SIN(player.Yang#)*player.speed#
ENDIF
`Increase gun-bobbing angle to get gun bobbing
player.gunBobAng# = WRAPVALUE(player.gunBobAng#+player.gunBobSpeed#)
ELSE
`Otherwise slowly change the value to nothing to bring the gun to the centre again
player.gunBobAng# = CURVEANGLE(0,player.gunBobAng#,10)
ENDIF
`Control the gun swaying according to how much the player is turning
player.gunTurnAng# = CURVEANGLE(WRAPVALUE(player.mouseMovementX#),player.gunTurnAng#,10)
player.gunLookAng# = CURVEANGLE(WRAPVALUE(player.mouseMovementY#),player.gunLookAng#,10)
`Position the gun-holding limb and rotate it to give swaying effect
OFFSET LIMB player.object_ID,1,player.gunOffsetX#+SIN(player.gunBobAng#)*player.gunBobMove#,player.gunOffsetY#+ABS(COS(player.gunBobAng#))*player.gunBobHeight#,player.gunOffsetZ#
ROTATE LIMB player.object_ID,1,player.gunLookAng#,player.gunTurnAng#,0
`player.energy# = player.mass# + player.speed# ^ 2
`
Phy Add Rigid Body Force player.object_ID,player.moveX#,0.0,player.moveZ#,3 :`-player.energy# this code was for the y on this line
` update player postion rotation etc
Phy Set Rigid Body Rotation player.object_ID,player.Xang#,player.Yang#,0.0 :`player.Xang#,player.Zang#
` end this player controls function
endfunction
`*********************************************************
`*********************************************************
`*********************************************************
`*** position camera & rotate it to player object ***
camera_on_player:
` position camera at player location
position camera 0,object position x(player.object_ID),object position y(player.object_ID)+1.5,object position z(player.object_ID)
` rotate camera on player orientation
rotate camera 0,object angle x(player.object_ID),object angle y(player.object_ID),object angle z(player.object_ID)
return
`*********************************************************
`*********************************************************
`*********************************************************
physic_collision:
set cursor 0,0 :print "contact point x = " + str$( x# )
set cursor 0,10 :print "contact point y = " + str$( y# )
set cursor 0,20 :print "contact point z = " + str$( z# )
set cursor 0,30 :print "contact normal x = " + str$( dirX# )
set cursor 0,40 :print "contact normal y = " + str$( dirY# )
set cursor 0,50 :print "contact normal z = " + str$( dirZ# )
set cursor 0,60 :print "friction force x = " + str$( frictionX# )
set cursor 0,70 :print "friction force y = " + str$( frictionY# )
set cursor 0,80 :print "friction force z = " + str$( frictionZ# )
set cursor 0,90 :print "normal force x = " + str$( normalX# )
set cursor 0,100 :print "normal force x = " + str$( normalY# )
set cursor 0,110 :print "normal force x = " + str$( normalZ# )
if phy get collision ( 1000, player.object_ID )
`color object 3, rgb ( 255, 0, 0 )
for i = 1 to phy get collision count ( player.object_ID )
x# = phy get collision contact point x ( player.object_ID, i )
y# = phy get collision contact point y ( player.object_ID, i )
z# = phy get collision contact point z ( player.object_ID, i )
dirX# = phy get collision contact normal x ( player.object_ID, i )
dirY# = phy get collision contact normal y ( player.object_ID, i )
dirZ# = phy get collision contact normal z ( player.object_ID, i )
frictionX# = phy get collision friction force x ( player.object_ID, i )
frictionY# = phy get collision friction force y ( player.object_ID, i )
frictionZ# = phy get collision friction force z ( player.object_ID, i )
normalX# = phy get collision normal force x ( player.object_ID, i )
normalY# = phy get collision normal force y ( player.object_ID, i )
normalZ# = phy get collision normal force z ( player.object_ID, i )
inc ID
next i
endif
return
`*********************************************************
`*********************************************************
`*********************************************************
` camera controls temp
camera_rotation:
` camera rotates around bridge
c# = wrapvalue( c# + 0.5 )
x# = cos ( c# ) * 40.0 :`8.0 keep this as a smaller value
z# = sin ( c# ) * 50.0 :`10.0 kepp this as a bigger value
position camera x#, 4, z#
point camera 0, 4, 0
return
soul sucking devils, twisted body of the damed, slivering slim drips from every poor, sin licking at your ears, and the smell stinging your eyes, and if you don't like it, get out of my kitchen!