Hello, I have an issue with first person camera, when I´m using Newton Dynamics for a car physics.
I want camera to rotate with car. It´s working a moment, but after while, camera isn´t rotating with car and after that it´s again working and again not working and again and again.
I tried everything what I can (with some other methods of camera rotating) without any success.
Here´s the code:
sync on
autocam off
set ambient light 80
cosi as float : cosi = 0
camY as float
REM Steering Wheel Stuff
jy as float : jy=Joystick Y()
jx as float : jx=Joystick X()
MaxSpeed# as float : MaxSpeed# = 3000
Rem Size of vehicle Výška předku auta nad zemí (původně 8)
sizeA=8
Rem Weight of Vehicle Váha auta (když je malá, tak auto nezatáčí)
Weight#=300
Rem Max Steeringangle
MaxSteeringAngle#=45
Dim wheels(4)
Rem Wheelmass (nechal jsem to na 15)
mass# = 50
Rem Wheelwidth (původně 3)
width# = 1
Rem Wheelradius Zdvih zadku auta (původně 6)
radius# = 4
Rem Shocks (tlumení, původně 150.0)
Damping#=150.0
Rem Spring (původně 150.0)
Spring#=150.0
Rem Length of suspension
Length#=10.0
Rem Forward Torque (točivý moment/rychlost dopředu, původně 35 000)
FTorque#=150000
Rem Reverse Torque (točivý moment/rychlost dozadu, původně 15 000)
RTorque#=40000
Rem Maximum Brakeforce
MaxBrake#=7000
Rem this are values for linear and angular damping on the Vehicle Chassis
Rem This makes the vehicle behave more realistic
Lindamp#=0.2
Angdamp#=0.8
Rem Size of boxes in wall
boxsize#=6
Rem Weight of boxes in Wall (původně 0.1)
boxmass#=0.1
Rem Max boxes in wall X direction
maxx= 8
Rem Max boxes in wall Y direction
maxy= 8
dim bbox(maxx*maxy+maxx)
NDB_NewtonCreate
` Reduce the exactness of the solver model to linear mode for stacked boxes
NDB_NewtonSetSolverModel 2
NDB_SetVector 1, -1000.0, -500.0, -1000.0
NDB_SetVector 2, 1000.0, 500.0, 1000.0
NDB_NewtonSetWorldSize
NDB_SetVector 1,0,-98.0,0
NDB_SetStandardGravity
`NDB_Newtonsetminimumframerate 80
Rem Make world
make object box 1,1500,4,1500
color object 1, RGB(20,50,20)
col = NDB_NewtonCreateTreeCollision(1)
Room = NDB_NewtonCreateBody(col)
NDB_BodySetDBProData Room, 1
NDB_NewtonBodySetDestructorCallback Room
NDB_NewtonReleaseCollision col
NDB_BuildMatrix 0,0,0,0,-20,0
position object 1,0,-20,0
NDB_NewtonBodySetMatrix 1
Rem Make Vehicle body
Col=NDB_NewtonCreatebox(30,13,15)
BoxA=Ndb_NewtonCreateBody(Col)
NDB_NewtonReleaseCollision Col
NDB_BodySetGravity BoxA,1
NDB_BuildMatrix 0,cosi,0,30,-4,-40
NDB_NewtonBodySetMatrix BoxA
// make object box 2,sizeA*3,sizeA/2,sizeA
Load Object "car.x", 2
Scale Object 2, 1, 1, 1
NDB_BodySetDBProData BoxA,2
NDB_CalculateMIBoxSolid Weight#,30,13,15
NDB_NewtonBodySetMassMatrix BoxA,Weight#
NDB_NewtonBodySetLinearDamping BoxA,Lindamp#
REM tohle by mohlo být otáčení auta
// NDB_SetVector 1,Angdamp#,Angdamp#,Angdamp#
NDB_SetVector 1,Angdamp#,0,Angdamp#
NDB_NewtonBodySetAngularDamping BoxA
Rem Make wheels
for t = 0 to 1
// make object box 3+t,radius#,radius#,Radius#/1.5
Load Object "wheel_front_left.x", 3+t
Scale Object 3+t, 1, 1, 1
// color object 3+t,rgb(0,20,35)
set object ambience 3+t,80
next t
for t = 2 to 3
// make object box 3+t,radius#*1.5,radius#*1.5,Radius#/1.5
Load Object "wheel_back_left.x", 3+t
Scale Object 3+t, 1, 1, 1
// color object 3+t,rgb(0,20,35)
set object ambience 3+t,80
next t
NDB_SetVector 1,0,1,0
Body = NDB_NewtonConstraintCreateVehicle( BoxA )
REM Pozice Kol
Predni_kola as float : Predni_kola = -12
Zadni_kola as float : Zadni_kola = 20
Levy_bok as float : Levy_bok = -6
Pravy_bok as float : Pravy_bok = 13
`NDB_BuildMatrix 0,0,0,-Sizea*1.8,-Sizea/2,-Sizea
NDB_BuildMatrix 0,0,0,Predni_kola,-Sizea*0.66,Levy_bok
NDB_SetVector 1,0,0,1
Wheels(1) = NDB_NewtonVehicleAddTire( Body,mass#/2, width#, radius#/2, Damping#,Spring#,Length# )
NDB_VehicleTireSetDBProData Body, wheels(1), 3
`NDB_BuildMatrix 0,0,0,-Sizea*1.8,-Sizea/2,Sizea
NDB_BuildMatrix 0,0,0,Predni_kola,-Sizea*0.66,Pravy_bok
NDB_SetVector 1,0,0,1
Wheels(2) = NDB_NewtonVehicleAddTire( Body,mass#/2, width#, radius#/2, Damping#,Spring#,Length# )
NDB_VehicleTireSetDBProData Body, wheels(2), 4
`NDB_BuildMatrix 0,0,0,Sizea/1.5,-Sizea/2,Sizea původně 1.5
NDB_BuildMatrix 0,0,0,Zadni_kola,-Sizea*0.66,Levy_bok
NDB_SetVector 1,0,0,1
Wheels(3) = NDB_NewtonVehicleAddTire( Body,mass#, width#, radius#, Damping#,Spring#,Length# )
NDB_VehicleTireSetDBProData Body, Wheels(3), 5
//
`NDB_BuildMatrix 0,0,0,Sizea/1.5,-Sizea/2,-Sizea Sizea/1.5
NDB_BuildMatrix 0,0,0,Zadni_kola,-Sizea*0.66,Pravy_bok
NDB_SetVector 1,0,0,1
Wheels(4) = NDB_NewtonVehicleAddTire(Body, mass#, width#, radius#, Damping#,Spring#,Length# )
NDB_VehicleTireSetDBProData Body, Wheels(4), 6
Rem set wheel max brakes
For t = 1 to 4
NDB_VehicleTireSetMaxBrake Body, Wheels(t), Maxbrake#
next t
Rem setting auto freeze to zero means even when at rest the car will respond to controls
NDB_NewtonBodySetAutoFreeze BoxA,0
Rem Generate Materials
Rem Without materials you cant change the friction elasticity and softness of
Rem different Rigid bodies in your system.
Rem First we creates some identification variables to store the materials in
Rem Defaultmat (Which is the default material always present in Newton
Rem BoxMat which becomes the material of each bbox
Rem CarMat which is to be the material of the Car object
DefaultMat = NDB_NewtonMaterialGetDefaultGroupID()
BoxMat = NDB_NewtonMaterialCreateGroupID()
CarMat = NDB_NewtonMaterialCreateGroupID()
Rem Here we set the Static and the Dynamic friction between the Defaultmaterial
Rem And the bbox material.
Rem The Floor in our case will have the defaultmaterial because we dont specify
Rem any material for that object. = Gets the defaultgroupid automatically.
NDB_NewtonMaterialSetDefaultFriction DefaultMat, BoxMat, .95, 0.85
Rem Here the elasticity between the materials is specified
NDB_NewtonMAterialSetDefaultElasticity DefaultMat, BoxMat, 0.5
Rem Here the softness between the materials is specified
NDB_NewtonMaterialSetDefaultSoftness DefaultMat, BoxMat, 0.5
Rem Here we specify how the boxes will interact with eachother !
NDB_NewtonMaterialSetDefaultFriction BoxMat, BoxMat, 0.5,0.5
Rem Here the elasticity between the boxes is specified
NDB_NewtonMAterialSetDefaultElasticity BoxMat, BoxMat, 0.2
Rem Here the softness between the boxes is specified
NDB_NewtonMaterialSetDefaultSoftness BoxMat, BoxMat, 0.9
Rem Here we specify the friction between the floor and the Car object
NDB_NewtonMaterialSetDefaultFriction DefaultMat, CarMat, 0.95, 0.95
Rem Here the elasticity between the floor and the fps object
NDB_NewtonMAterialSetDefaultElasticity DefaultMat, CarMat, 0.1
Rem Here the softness between the floor and the fps object
NDB_NewtonMaterialSetDefaultSoftness DefaultMat, CarMat, 0.2
Rem Than we set the material CarMat to the Car object !
NDB_NewtonBodySetMaterialGroupID Body, CarMat
Rem Now we Specify which objects will affect eachother with the stated material parameters
Rem Floor against bbox
NDB_NewtonMaterialSetCollisionCallback DefaultMat, BoxMat
Rem Floor against fps object
NDB_NewtonMaterialSetCollisionCallback DefaultMat, CarMat
Rem Boxes against boxes
NDB_NewtonMaterialSetCollisionCallback BoxMat, BoxMat
Rem Fps objct against boxes , thats all !
NDB_NewtonMaterialSetCollisionCallback CarMat, BoxMat
Rem Create Boxes, both DBPro Object and Newton Object
for y = 0 to maxy
for x=1 to maxx
obj=x+(y*maxx)+1000
objarr=x+(y*maxx)
Rem DBPro object
h#=RND(2)+1
make object box obj,boxsize#,boxsize#/h#,boxsize#
color object obj,RGB(100,100,x*2.5)
set object ambience obj, 80
Rem Newton Collision object same size as DBPro Object
col=NDB_NewtonCreateBox(boxsize#,boxsize#/h#,boxsize#)
Rem Here bbox becomes a newton collision object
bbox(objarr) = NDB_NewtonCreateBody(col)
Rem Connect Newton and DBPro object This means that the callback system will
Rem Automaticall update position of the graphical shape (The box)
NDB_BodySetDBProData bbox(objarr), obj
NDB_CalculateMIBoxSolid boxmass#,boxsize#,boxsize#,boxsize#
Rem Set Mass of NewtonObject
NDB_NewtonBodySetMassMatrix bbox(objarr),boxmass#
Rem Position Objects
Rem First three Floats in the Buildmatrix represents the Global rotation
Rem Next three Floats in the Buildmatrix represents the Global position
NDB_BuildMatrix 0.0, 0.0, 0.0, -50+x*boxsize#*1.3,10+y*boxsize#*2, 0.0
Rem This command reads the matrix created above and rotates and position the object
Rem bbox(obj) regarding to the content of the matrix
NDB_NewtonBodySetMatrix bbox(objarr)
Rem Release collision to free up memory, Newton object stays active !
NDB_NewtonReleaseCollision col
Rem Make sure the Gravity in the Newton World affects each bbox
NDB_BodySetGravity bbox(objarr),1
Rem We need to increase the dampener default since it is slow low
NDB_NewtonBodySetLinearDamping bbox(objarr),0.1
NDB_SetVector 0.1, 0.1, 0.1
NDB_NewtonBodySetAngularDamping bbox(objarr)
NDB_SetVector 0.1, 0.1, 0.1, 0.2
NDB_NewtonBodySetFreezeTreshold bbox(objarr), 1
Rem Make sure the Box objects get the correct body
NDB_NewtonBodySetMaterialGroupID bbox(objarr), boxMat
next x
next y
set ambient light 60
Speed=make vector3(SpeedVec)
thirdperson as float : thirdperson = 0
viewwait as float : wievwait = 0
camY = 0
Do
// If keystate(17) and keystate(32) and steerangle#= 30 and Speed# > 1
// inc camY, 0.12
// EndIf
//
// If keystate(17) and keystate(30) and steerangle#= -30 and Speed# > 1
// dec camY, 0.12
// EndIf
//
// If keystate(17) and keystate(32) and steerangle#= 30 and Speed# > 50
// inc camY, 0.14
// EndIf
//
// If keystate(17) and keystate(30) and steerangle#= -30 and Speed# > 50
// dec camY, 0.14
// EndIf
camY = Object Angle Y(2)-90
REM update joystick
jy=Joystick Y()
jx=Joystick X()
position camera object position x(2),object position y(2)+4.5,object position z(2)
rotate camera 0,camY,0
If thirdperson = 1
pitch camera down 20
move camera -70
EndIf
If returnkey() = 1 and thirdperson = 0 and viewwait = 0
thirdperson = 1
viewwait = 30
EndIf
If returnkey() = 1 and thirdperson = 1 and viewwait = 0
thirdperson = 0
viewwait = 30
EndIf
If viewwait > 0
dec viewwait, 1
EndIf
Sec#=NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate Sec#
Rem Next command is needed for the next version of the wrapper: 1.32+
NDB_NewtonUpdateTires Body
text 10,10,"Time since last update"+str$(Sec#)
text 10,20,"Maximum Speed :"+str$(MaxSpeed#)
text 10,30,"Screen FPS :"+str$(screen fps())
text 10,40,"jy :"+str$(jy)
text 10,50,"jx :"+str$(jx)
text 10,60,"cosi :"+str$(cosi)
text 10,70,"camera angle y :"+str$(camY)
text 10,80,"steer angle :"+str$(steerangle#)
text 10,90,"speed :"+str$(speed#)
NDB_NewtonBodyGetVelocity BoxA
SET VECTOR3 SpeedVec, NDB_GetVector_X(),NDB_GetVector_Y(), NDB_GetVector_Z()
Speed#=Length vector3(SpeedVec)
if Speed#>MaxSpeed# then MaxSpeed#=Speed#
// if inkey$()="r"
// wait 200
// NDB_BuildMatrix 0,0,0,30,-4,-40
// NDB_NewtonBodySetMatrix BoxA
// NDB_SetVector 0.0,0.0,0.0
// NDB_NewtonBodySetVelocity BoxA
// NDB_NewtonBodySetOmega BoxA
// MaxSpeed#=0
//
// NDB_NewtonVehicleReset Body
// endif
NDB_NewtonBodyGetOmega BoxA
VecX#=NDB_GetVector_X (1)
VecY#=NDB_GetVector_Y (1)
VecZ#=NDB_GetVector_Z (1)
if VecX# >1 then VecX#=1
if VecY# >1 then VecY#=1
if VecZ# >1 then VecZ#=1
NDB_SetVector 1,VecX#,VecY#,VecZ#
NDB_NewtonBodySetOmega BoxA
Rem CONTROLS FOR MOVING THE VEHICLE
ACCEL = 0
Rem if the user pressed "W" key, set positive torque for the tires
if keystate(17) or jy<-600
ACCEL = 1
Torque# = FTorque#
endif
Rem if the user pressed "S" key, set negative torque for the tires.
if keystate(31)
ACCEL = 1
Torque# = -RTorque#
endif
Rem steering basically works the same way!
STEERING = 0
if keystate(32) or jx>800
STEERING = 1
inc steerangle#, 0.7
if steerangle# > MaxSteeringAngle# then steerangle# = MaxSteeringAngle#
endif
if keystate(30) or jx<-800
STEERING = 1
dec steerangle#, 0.7
if steerangle# < -MaxSteeringAngle# then steerangle# = -MaxSteeringAngle#
endif
if keystate(32) or jx>500
STEERING = 1
inc steerangle#, 0.3
if steerangle# > 30 then steerangle# = 30
endif
if keystate(30) or jx<-500
STEERING = 1
dec steerangle#, 0.3
if steerangle# < -30 then steerangle# = -30
endif
if STEERING = 0
steerangle# = steerangle# * 0.05
endif
Rem If the Shift key is pressed, we turn the brakes on!
if Shiftkey() then BRAKES = 1 else BRAKES = 0
Rem Loop through each wheel, and apply the torque, steering, and brakes!
for i=3 to 4
if ACCEL AND (120 > ABS(NDB_NewtonVehicleGetTireOmega(body, Wheels(i))))
NDB_VehicleTireSetTorque body, Wheels(i), torque#
else
if 5 < ABS(NDB_NewtonVehicleGetTireOmega(body, Wheels(i)))
NDB_VehicleTireSetTorque body, Wheels(i), -torque#*0.1
else
NDB_VehicleTireSetTorque body, Wheels(i), 0.0
endif
endif
if BRAKES then NDB_VehicleTireBrakesOn body, Wheels(i)
next i
for i = 1 to 2
NDB_VehicleTireSetSteeringAngle body, Wheels(i), steerangle#
if BRAKES then NDB_VehicleTireBrakesOn body, Wheels(i)
next i
Rem Press Ctrl key to show debug lines
if controlkey() then NDB_DebugDrawNewtonLines
Rem update the screen
If escapekey() = 1
NDB_NewtonDestroy
end
EndIf
Sync
Loop
if memblock exist(1) then end
if mesh exist(1) then end
In the download is a RAR with media and compiled code.
Controls are WASD, shift(handbrake) and enter(change of view).
the leader of Mouseking studio