Hi,now I have a gun I looked into shooting bullets of to test spheres from it, here is the code I have so far:
function test_level()
//setsunactive(0)
//SetAmbientColor(0,0,0)
//setcamerarotation(1,0,180,0)
//setobjectrotation(player,0,180,0)
for n=1 to maxobjects
if object[n].group=7
f$="mov1.avi"
loadvideo(f$)
playvideotoimage(5000)
playvideo()
exit
endif
next n
//createpointlight(sun,0,1000,0,3000,255,0,255)
for n=1 to maxobjects
if object[n].id>0
createobjectphysics(object[n].id,object[n].pb,object[n].ps)
//setobject3dphysicsdamping(object[n].id,0.1,0.1)
//setobject3dphysicsfriction(object[n].id,0.01)
//setobject3dphysicsrollingfriction(object[n].id,0.01)
//SetObject3DPhysicsMass(object[n].id,0.5)
endif
if object[n].group=1
player=object[n].id
px#=object[n].x#
py#=object[n].y#
pz#=object[n].z#
Delete3DPhysicsBody(player)
Create_character_controller(player)
endif
if object[n].group=7
setobjectimage(object[n].id,5000,0)
endif
if light[n].lightID>0
setobjectvisible(light[n].lightobjectID,0)
endif
next n
addvirtualjoystick(1,100,600,90)
setobjectposition(curs,-1000000,-1000000,-1000000)
addvirtualbutton( 1, 94.7,93.1,9.77 )
addvirtualbutton( 2, 94.7,79.4,9.77 )
setcameraposition(1,getobjectx(player),getobjecty(player),getobjectz(player))
SetCameraRange(1,1,1000)
//setobjectposition(player,-1000,-1000000,-1000000)
SetAmbientColor( 0,0,0 )
setobjectvisible(myplane,0)
gun = createobjectbox(0.25,0.25,10)
SetObjectPosition( gun, -15, 0, -50.0 )
// FixObjectToObject( gun, player)
//setsunactive(1)
//SetSunColor(255,0,0)
//setsundirection(0,-2000,2000)
repeat
Step3DPhysicsWorld()
check_lights()
Move3DPhysicsCharacterController( player, 0, walkVel#)
if GetrawkeyPressed(KEY_V)
//To move dynamic physics bodies we need to apply velocity to the physics body.
ThrowBall( 300.0, 300.0)
endif
rem Old position
oldx#=GetCameraX(1)
oldy#=GetCameraY(1)-45.0
oldz#=GetCameraZ(1)
rem Move camera
if ( GetRawKeyState( 87 ) )
MoveCameraLocalZ( 1, 4 )
Move3DPhysicsCharacterController(player,1,walkvel#)
//print("Up")
endif
if ( GetRawKeyState( 83 ) )
MoveCameraLocalZ( 1, -4 )
Move3DPhysicsCharacterController(player,2,walkvel#)
// print("Down")
endif
if ( GetRawKeyState( 65 ) )
MoveCameraLocalX( 1, -4 )
Move3DPhysicsCharacterController(player,3,walkvel#)
// print("Left")
endif
if ( GetRawKeyState( 68 ) )
MoveCameraLocalX( 1, 4 )
Move3DPhysicsCharacterController(player,4,walkvel#)
// print("Right")
endif
if ( getvirtualbuttonstate(1) = 1 ) then MoveCameraLocalZ( 1, -4 )
if ( getvirtualbuttonstate(2) = 1 ) then MoveCameraLocalZ( 1, 4 )
if ( GetPointerPressed() )
startx# = GetPointerX()
starty# = GetPointerY()
angx# = GetCameraAngleX(1)
angy# = GetCameraAngleY(1)
pressed = 1
endif
if ( GetPointerState() = 1 )
fDiffX# = (GetPointerX() - startx#)
fDiffY# = (GetPointerY() - starty#)
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
Rotate3DPhysicsCharacterController(player,angy# + fDiffX#)
endif
if ( getrawjoystickexists(1) )
MoveCameraLocalZ(1,-getrawjoysticky(1)*4)
MoveCameraLocalX(1,getrawjoystickx(1)*4)
RotateCameraGlobalY(1,getrawjoystickrx(1)*4)
RotateCameraLocalX(1,getrawjoystickry(1)*4)
//RotateobjectGlobalY(player,GetCameraAngleX(1))
//Rotate3DPhysicsCharacterController(player,getcameraangleY(1))
endif `
rem Gravity on camera
SetCameraPosition(1,getobjectx(player),getobjecty(player),getobjectz(player))
SetObjectRotation(gun,GetCameraAngleX(1),GetCameraAngleY(1),GetCameraAngleZ(1))
// setobjectposition(gun,getobjectx(player),getobjecty(player),getobjectz(player))
rem New position
newx#=GetCameraX(1)
newy#=GetCameraY(1)-45
newz#=GetCameraZ(1)`
rem Adjust Y to match floor
pobj= ObjectSphereSlide(0,oldx#,oldy#,oldz#,newx#,newy#,newz#,19.0)
if pobj>0
newx#=GetObjectRayCastSlideX(0)
newy#=GetObjectRayCastSlideY(0)
newz#=GetObjectRayCastSlideZ(0)
//SetCameraPosition(1,newx#,newy#+45.0,newz#)
//setobjectposition(player,newx#,newy#,newz#)
objc=GetObjectRayCastHitID(0)
endif
if objc<>0
//Print("hit object "+str(objc))
endif
posx# = sin(timer()*40) * 250 + 2000
SetPointLightPosition( 1, posx#, 565, -3600 )
//SetObjectPosition( 99, posx#, 565, -3600 )
rem Framerate prompt
// print("W A S D or joystick to move, click and drag to rotate")
fps=ScreenFPS() : Print("FPS: " + str(fps))
rem Update screen
if getrawkeypressed(KEY_E)
quit=1
endif
if getrawkeypressed(KEY_SPACE)
Jump3DPhysicsCharacterController(player)
endif
SetObjectPosition(gun,getcamerax(1),getcameray(1),getcameraz(1))
MoveObjectLocalZ(gun,6.1)
MoveObjectLocalx(gun,0.5)
moveobjectlocaly(gun,-0.4)
SetObjectRotation(gun,GetCameraAngleX(1),GetCameraAngleY(1),GetCameraAngleZ(1))
Sync()
until quit=1
if getobjectexists(gun)
deleteobject(gun)
endif
quit=0
SetAmbientColor( 50,50,50 )
deletevirtualjoystick(1)
setobjectvisible(myplane,0)
Level_cleanup()
setobjectvisible(player,1)
SetCameraRange(1,100,3000)
endfunction
function ThrowBall( initialSpeed as float, mass as float )
local directionX as float
local directionY as float
local directionZ as float
directionX = getcameraanglex(1)
directionY = getcameraangley(1)
directionZ = getcameraanglez(1)
throwBall as integer
throwBall = CreateObjectSphere( 8, 16, 32 )
SetObjectImage( throwBall,12, 0 )
//position bullet in front of gun
SetObjectPosition( throwBall, Getobjectx( gun ), Getobjecty( gun ), GetobjectZ( gun )+2 )
//3D Physics code
Create3DPhysicsDynamicBody( throwBall )
SetObjectShapeSphere( throwBall )
SetObject3DPhysicsMass( throwBall, mass )
SetObject3DPhysicslinearVelocity( throwBall,directionx,directiony, directionZ, initialSpeed )
SetObject3DPhysicsFriction( throwBall, 0.8 )
SetObject3DPhysicsRollingFriction( throwBall, 0.7 )
endfunction
As you see I have taken code directly from the 3d physics examples on this forum, yet on running and pressing the v key, the sphere simply shoot downward rather than in the direction of the camera! Anyone know why, if you need a video of what happens then I will post one!
Hail to the king, baby!