I am making a fps game and i have newton in it. It works just fine except the ellipsoid im using for collision of camera keeps falling i tried to use the constraint upvector thing but still doesnt work. I am kinda copying the fps example but im tweaking it.
here is some code
rem *************************************************
rem create starting position for camera and collision
rem player initiation
rem _________________________________________________
sphere=ndb_newtoncreatesphere(25,60,25)
player=ndb_newtoncreatebody(sphere)
ndb_buildmatrix 0,0,0,0,5,-25
ndb_newtonbodysetmatrix player
rem mass of player
ndb_setvector 6,6,6
ndb_calculatemiboxsolid pmass#
ndb_newtonbodysetmassmatrix player,pmass#
rem unfreeze character
ndb_newtonbodysetautofreeze player,0
ndb_bodysetgravity player,1
rem make sure the character doesn't fall down
ndb_setvector 0,1,0
upvector = ndb_newtonconstraintcreateupvector(player)
rem make sure friction doesnt affect player
default= ndb_newtonmaterialgetdefaultgroupid()
playerid= ndb_newtonmaterialcreategroupid()
ndb_newtonmaterialsetdefaultfriction default,playerid,0.1,0.1
ndb_newtonmaterialsetdefaultelasticity default,playerid,0.1
ndb_newtonbodysetmaterialgroupid player,playerid
and here is the player movement
rem this is the movement subroutine
movement:
rem these are the movement variables
moving=0
degree=-15
degree2=-7
step#=20
rem position the rigid body and char.
set cursor 0,0
print scancode()
xend#=xbase#+cos(angle#)*lengthx#
yend#=sin(angle#)*lengthy#-ybase#
ndb_bodygetposition player
position camera cameranum,ndb_getvector_x(),ndb_getvector_y(),ndb_getvector_z()
rotate camera cameranum,cx#,cy#,0.0
rem movement variables
movex#=0
movez#=0
rem this is for moving the camera
if keystate(17) or keystate(31)
moving=1
dx#=sin(cy#)
dz#=cos(cy#)
inc movex#, dx# * (keystate(17)-keystate(31))
inc movez#, dz# * (keystate(17)-keystate(31))
endif
if keystate(32) or keystate(30)
dx#=sin(cy#+90)
dz#=cos(cy#+90)
inc movex#,dx# * (keystate(32)-keystate(30))
inc movez#,dz# * (keystate(32)-keystate(30))
endif
rem renormalize vector
length#=sqrt((movex#^2)+(movey#^2))
movex#=movex#/length#
movez#=movez#/length#
rem current velocity of player
ndb_newtonbodygetvelocity player
currentvelx#=ndb_getvector_x()
currentvely#=ndb_getvector_y()
currentvelz#=ndb_getvector_z()
rem our goal velocity
goalvelx#= movex# * (step# * ((shiftkey()+1)*2))
goalvelz#= movez# * (step# * ((shiftkey()+1)*2))
rem find our acceleration
accelx#=((goalvelx#-currentvelx#)/time#)
accelz#=((goalvelz#-currentvelz#)/time#)
rem limit our acceleration
if accelx# > 200 then accelx#=200
if accelx# < -200 then accelx#=-200
if accelz# > 200 then accelz#=200
if accelz# < -200 then accelz#=-200
rem
rem apply force to player
rem
ndb_bodygetposition player
rem 2nd vector
ndb_setvector 2,accelx#,0,accelz#
ndb_bodyaddforceglobal player
rem create swaying for more realism
rem this is the loop when swaying
rem gives us a little classic doom action :)
if moving = 1
if beginning=0
angle#=angle#+degree
endif
rem set limitations
if angle# <= -180
beginning = 1
endif
if beginning = 1
angle#=angle#-degree
endif
if angle# >= 15
beginning=0
endif
endif
rem if moving = 0 and angle is not at default set at default and make it a smooth transition
if moving = 0
beginning =0
if angle# > 3.141592
angle#=angle#-15
if angle# < 3.141592
angle#=3.141592
endif
endif
if angle# < 3.141592
angle#=angle#+15
if angle# > 3.141592
angle#=3.141592
endif
endif
endif
rem this part is jumping
accely#=0
if spacekey()=1 and jumping=0
jumping = 1
ndb_bodygetposition player
px#= ndb_getvector_x(): py#= ndb_getvector_y(): pz#= ndb_getvector_z()
ndb_setvector 1,px#,py#,pz#
ndb_setvector 2,px#,py#-6.05,pz#
dist#= ndb_newtonworldraycast()
if dist# < 10
accely#= ((pjumpspeed#-currentvely#)/time#)
rem add force to player
ndb_bodygetposition player
ndb_setvector 2,0,accely#,0
ndb_bodyaddforceglobal player
endif
endif
if spacekey()=0 then jumping=0
rem these pieces of code is for limiting to look up and down
inc cx#,mousemovey()*.25
inc cy#,mousemovex()*.25
if cx# <= -90
cx#=-90
endif
if cx# >= 90
cx#=90
endif
rem this is for switching guns
if delay=0
if keystate(51)=1
rx#=0
while rx# <> -90
if rx# > -90
rx#=rx#-.1
else
rx#=-90
endif
xrotate object guns(curgun),wrapvalue(rx#)
endwhile
curgun=curgun-1
while avguns(curgun)=0
curgun=curgun-1
if curgun < 1 then curgun = 9:curgun=curgun-1
endwhile
rx#=-90
while rx# <> 0
if rx# < 0
rx#=rx#+.1
else
rx#=0
endif
xrotate object guns(curgun),wrapvalue(rx#)
endwhile
delay=20
endif
endif
if delay=0
if keystate(52)=1
rx#=0
while rx# <> -90
if rx# > -90
rx#=rx#-.1
else
rx#=-90
endif
xrotate object guns(curgun),wrapvalue(rx#)
endwhile
curgun=curgun+1
while avguns(curgun)=0
curgun=curgun+1
if curgun > 9 then curgun = 1:curgun=curgun+1
endwhile
rx#=-90
while rx# <> 0
if rx# < 0
rx#=rx#+.1
else
rx#=0
endif
xrotate object guns(curgun),wrapvalue(rx#)
endwhile
delay=20
endif
endif
rem this shows and positions the current gun
if delay <> 0
dec delay
endif
if curgun=4 and delay<=0
delay=0
show object guns(4)
position object guns(4),xend#,yend#,20
else
hide object guns(4)
endif
if curgun=3 and delay<=0
delay=0
show object guns(3)
position object guns(3),xend#,yend#,37.5
else
hide object guns(3)
endif
return
i kinda mixed movement with switching guns
thanks,
flip998
"Wow, it's like a panda crapped a rainbow in my brain."
-Captain(Sealab 2021)