
source - https://docs.google.com/file/d/0B73Vvz7 ... sp=sharing
I could not make out what's going on there, can you explain or correct code please?Fred wrote:There is a demo called FPSFirstPerson.pb which show a basic FPS with collisions and physics
Code: Select all
#PlayerSpeed = 200
#CameraSpeed = 14
Global collision
Global joint = 66
Enumeration
#tex = 100
#mat
#BillboardGroup
#Billboard
#table
#leg1
#leg2
#leg3
#leg4
#tableMaterial
#football
#camera
#cameraBody ; the robot
EndEnumeration
Structure Vector3
x.f
y.f
z.f
EndStructure
Structure s_Key
Up.i
Down.i
Left.i
Right.i
Jump.i
EndStructure
Structure s_Entity
EntityBody.i
BodyOffsetY.f
elapsedTime.f
Fov.f
Key.s_Key
MainNode.i
CameraNode.i
HandNode.i
ForwardNode.i
StrafeNode.i
EndStructure
Macro GetNodePosition(Position, Node)
Position\x = NodeX(Node)
Position\y = NodeY(Node)
Position\z = NodeZ(Node)
EndMacro
Macro SubVector3(V, V1, V2)
V\x = V1\x - V2\x
V\y = V1\y - V2\y
V\z = V1\z - V2\z
EndMacro
;-Declare
Declare HandleEntity(*Entity.s_Entity)
Define Robot.s_Entity
Define.f KeyX, KeyY, MouseX, MouseY
If InitEngine3D()
ExamineDesktops()
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Parse3DScripts()
InitSprite()
InitKeyboard()
InitMouse()
InitSound()
DeskWidth=DesktopWidth(0)
DeskHeight=DesktopHeight(0)
If LoadFont(0, "Arial", 30,#PB_Font_Bold)
SetGadgetFont(#PB_Default, FontID(0))
EndIf
OpenWindow(0, 0, 0, DeskWidth, DeskHeight, "Room _ press space to fly, arrows to move, mouse to rotate ", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, DeskWidth, DeskHeight, 0, 0, 0)
LoadSound(collision,"britneyspears3.wav")
LoadMesh(0, "room_obj.mesh")
CreateEntity(0,MeshID(0), #PB_Material_None,0,-35,0)
RotateEntity(0, 0,-17,0)
ScaleEntity(0,16000,11000,16000)
;ScaleEntity(0,35000,20000,35000)
EntityPhysicBody(0, #PB_Entity_StaticBody )
CreateSphere(#football, 50)
CreateMaterial(34, LoadTexture(34, "football.jpg"))
CreateEntity(#football,MeshID(#football), MaterialID(34),0,1000,0)
EntityPhysicBody(#football, #PB_Entity_SphereBody , 5,2,3)
PointJoint(joint, EntityID(#football), 0, 10, 0 , EntityID(0), 0, 1000, 0)
;plane textured with squares
CreateMaterial(3, LoadTexture(3, "MRAMOR6X6.jpg"))
MaterialCullingMode(3, #PB_Material_NoCulling )
CreatePlane(1,7000,7000,1,1,8,8)
CreateEntity(1,MeshID(1),MaterialID(3),0,0,0)
EntityPhysicBody(1, #PB_Entity_StaticBody,1,0.3,1)
; Camera
CreateCamera(#camera, 0, 0, 100, 100)
CameraBackColor(#camera, RGB(177,191,245))
;MoveCamera(0,0, 100, 300,#PB_Absolute)
;MoveCamera(#camera,-226.81, 334.93, 3474.49,#PB_Absolute)
;MoveCamera(#camera,-226.81, 334.93, 0,#PB_Absolute)
CameraLookAt(#camera, 0, 0, 0)
; Light
CreateLight(0, RGB(230,230,230), 0, 2000, 0) ; general light
SetLightColor(0, #PB_Light_SpecularColor, RGB(255,255,255))
CreateLight(1, RGB(230,230,230), 0, 200, 0) ; light in the position of the sprite
SetLightColor(1, #PB_Light_SpecularColor, RGB(255,255,255))
WorldShadows(#PB_Shadow_Additive , 6000, RGB(200,200,200))
LoadTexture(#tex , "Lensflare5.jpg") ; for the sprite
CreateMaterial(#mat, TextureID(#tex))
SetMaterialColor(#mat, #PB_Material_AmbientColor, RGB(1,1,1))
SetMaterialColor(#mat, #PB_Material_DiffuseColor, RGB(1,1,1))
DisableMaterialLighting(#mat, 1)
MaterialBlendingMode(#mat, #PB_Material_Add )
MaterialDepthWrite(#mat, Disable)
CreateBillboardGroup(#BillboardGroup, MaterialID(#mat), 100,100, 0, 0, 0)
AddBillboard(#Billboard, #BillboardGroup, 0, 200, 0)
; table and its 4 Legs
CreateMaterial(#tableMaterial, LoadTexture(#tex , "Wood.jpg"))
CreateCube(#table, 10)
CreateEntity(#table,MeshID(#table), MaterialID(#tableMaterial),0,35,0)
EntityPhysicBody(#table, #PB_Entity_ConvexHullBody , 100,1,1)
ScaleEntity(#table,10,1,20)
CopyEntity(#table, #leg1)
ScaleEntity(#leg1, 0.3,1,0.05)
RotateEntity(#leg1, 0,0,90)
MoveEntity(#leg1, 40, 16,90)
EntityPhysicBody(#leg1, #PB_Entity_ConvexHullBody , 4,0,100)
CopyEntity(#leg1, #leg2)
RotateEntity(#leg2, 0,0,90)
MoveEntity(#leg2, -40, 16,90)
EntityPhysicBody(#leg2, #PB_Entity_ConvexHullBody , 4,0,100)
CopyEntity(#leg1, #leg3)
RotateEntity(#leg3, 0,0,90)
MoveEntity(#leg3, -40, 16,-90)
EntityPhysicBody(#leg3, #PB_Entity_ConvexHullBody , 4,0,100)
CopyEntity(#leg1, #leg4)
RotateEntity(#leg4, 0,0,90)
MoveEntity(#leg4, 40, 16,-90)
EntityPhysicBody(#leg4, #PB_Entity_ConvexHullBody , 4,0,100)
;- Robot Body
LoadMesh (#cameraBody, "robot.mesh")
CreateEntity(#cameraBody, MeshID(#cameraBody), #PB_Material_None, -227, 335, 3474)
EntityPhysicBody(#cameraBody, #PB_Entity_ConvexHullBody , 0.45, 0, 0) ; robot
With Robot
\Fov = 50
\EntityBody = #cameraBody
\BodyOffsetY = 43
\Key\Down = #PB_Key_Down ;Down
\Key\Left = #PB_Key_Left ;Left
\Key\Right = #PB_Key_Right;Right
\Key\Up = #PB_Key_Up ;Up
\Key\Jump = #PB_Key_Space
\MainNode = CreateNode(#PB_Any) ; Entity position
\CameraNode = CreateNode(#PB_Any, 0, 140, 50) ; Camera position
\HandNode = CreateNode(#PB_Any, 0, 80, 0) ; Hand position
\ForwardNode= CreateNode(#PB_Any, 0, 0, -1) ; Direction normalized
\StrafeNode = CreateNode(#PB_Any, -1, 0, 0) ; Direction normalized
AttachNodeObject(\MainNode , NodeID(\CameraNode))
AttachNodeObject(\MainNode , NodeID(\HandNode))
AttachNodeObject(\MainNode , NodeID(\ForwardNode))
AttachNodeObject(\MainNode , NodeID(\StrafeNode))
;RotateNode(\HandNode, 5, 0, 0, #PB_Relative)
EndWith
; Camera
CreateCamera(#camera, 0, 0, 100, 100)
CameraBackColor(#camera, RGB(177,191,245))
CameraRange(#camera, 0.08, 5000)
AttachNodeObject(Robot\CameraNode, CameraID(#camera))
CameraLookAt(#camera, NodeX(Robot\ForwardNode) * 100, NodeY(Robot\CameraNode), NodeZ(Robot\ForwardNode) * 100)
;WorldDebug(#PB_World_DebugBody)
HideEntity(#cameraBody, 1)
Repeat
;Screen3DEvents()
If Engine3DFrameRate(#PB_Engine3D_Current)
Robot\elapsedTime = 40/Engine3DFrameRate(#PB_Engine3D_Current)
;Robot\elapsedTime = 40/75
EndIf
HandleEntity(@Robot)
RenderWorld(60)
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
Else
MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
FreeSound(collision)
End
Procedure HandleEntity(*Entity.s_Entity)
Protected.Vector3 Forward, Strafe, PosMain, PosDir, PosStrafe
Protected.f Speed, Speed2, SpeedShoot, x, y
Protected.f MouseX, MouseY
Static Jump.f, MemJump.i, Rot.Vector3, Trans.Vector3, Clic
With *Entity
GetNodePosition(PosMain, \MainNode)
GetNodePosition(PosDir, \ForwardNode)
GetNodePosition(PosStrafe, \StrafeNode)
SubVector3(Forward, PosDir, PosMain)
SubVector3(Strafe, PosStrafe, PosMain)
Speed = #PlayerSpeed * \elapsedTime
Speed2 = Speed * 0.5
SpeedShoot = Speed * 10
If ExamineMouse()
MouseX = -(MouseDeltaX()/200) * \Fov * \elapsedTime
MouseY = -(MouseDeltaY()/200) * \Fov * \elapsedTime
Rot\z = 0
Rot\y + MouseX
If NodePitch(\CameraNode) < 80 And MouseY > 0
Rot\x + MouseY
ElseIf NodePitch(\CameraNode) > -80 And MouseY < 0
Rot\x + MouseY
EndIf
If MouseButton(#PB_MouseButton_Right)
If \Fov > 20
\Fov - 2 * \elapsedTime
EndIf
Else
If \Fov < 50
\Fov + 2 * \elapsedTime
EndIf
EndIf
EndIf
If ExamineKeyboard()
If KeyboardPushed(\Key\Jump); And OnGround(*Entity)
Jump = 30
MemJump = 1
EndIf
Rot\x * 0.30
Rot\y * 0.30
Rot\z * 0.30
Trans\x * 0.80
Trans\y = Jump
Trans\z * 0.80
If KeyboardPushed(\Key\Up)
Trans\x = Forward\x * Speed
Trans\z = Forward\z * Speed
ElseIf KeyboardPushed(\Key\Down)
Trans\x = Forward\x * -Speed2
Trans\z = Forward\z * -Speed2
EndIf
If KeyboardPushed(\Key\Left)
Trans\x = Strafe\x * Speed
Trans\z = Strafe\z * Speed
ElseIf KeyboardPushed(\Key\Right)
Trans\x = Strafe\x * -Speed
Trans\z = Strafe\z * -Speed
EndIf
MoveEntity(\EntityBody, Trans\x, Trans\y, Trans\z)
re = RayCollide(-406, 0.35, 1603, 324, 0.35, 1555)
If re > 0
PlaySound(collision)
FreeJoint(joint)
ApplyEntityImpulse(#football, 0, -10, 0, 0,0,0)
EndIf
EndIf
If KeyboardReleased(#PB_Key_Z) ; for knowledge of position only and not necessary
Debug EntityX(\EntityBody)
Debug EntityY(\EntityBody)
Debug EntityZ(\EntityBody)
EndIf
Jump=-100
CameraFOV(#camera, \Fov)
RotateEntity(\EntityBody, 0, Rot\y, 0, #PB_Relative)
RotateNode(\CameraNode , Rot\x, 0, 0, #PB_Relative)
RotateNode(\HandNode , Rot\x, 0, 0, #PB_Relative)
MoveNode(\MainNode, EntityX(\EntityBody), EntityY(\EntityBody) - \BodyOffsetY, EntityZ(\EntityBody), #PB_Absolute)
RotateNode(\MainNode, 0, EntityYaw(\EntityBody), 0)
EndWith
EndProcedure
Code: Select all
ExamineDesktops()
Global sizeW.i = DesktopWidth(0)
Global sizeH.i = DesktopHeight(0)
Global exit = #False
Global camX.d = 0
Global camY.d = 0
Global playerX.d = 400
Global playerY.d = 50
Global playerZ.d = 500
Global playerSp.d = 2.5
Declare Test()
If InitEngine3D() = 0 Or InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 Or UsePNGImageDecoder() = 0 Or UseJPEGImageDecoder() = 0
exit = #True
EndIf
OpenScreen (sizeW, sizeH, 32, "Game3D")
SetFrameRate(60)
Global cCamera.i = CreateCamera (#PB_Any, 0, 0, 100, 100)
MoveCamera(cCamera, 0, playerY, 0, #PB_Absolute)
crMechPlayer.i = CreateCube (#PB_Any, 50)
Global crEntPlayer.i = CreateEntity(#PB_Any, MeshID(crMechPlayer), #PB_Material_None, 0, 200, 0)
EntityPhysicBody(crEntPlayer, #PB_Entity_BoxBody, 5, 0, 0)
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)
AmbientColor(RGB(255, 255, 255))
CreateTexture(0,32,32)
StartDrawing(TextureOutput(0))
Box(0,0,16,16,RGB(255,0,0))
Box(16,16,32,32,RGB(0,120,255))
StopDrawing()
CreateTexture(1,32,32)
StartDrawing(TextureOutput(1))
Box(0,0,16,16,RGB(0,255,0))
Box(16,16,32,32,RGB(0,255,255))
StopDrawing()
CreateTexture(2,32,32)
StartDrawing(TextureOutput(2))
Box(0,0,16,16,RGB(255,0,0))
Box(16,16,32,32,RGB(255,125,0))
StopDrawing()
CreateMaterial(0, TextureID(0))
CreateMaterial(1, TextureID(1))
CreateMaterial(2, TextureID(2))
CreatePlane(0, 12800, 12800, 40, 40, 50, 50)
CreateEntity(0, MeshID(0), MaterialID(0))
EntityPhysicBody(0, #PB_Entity_StaticBody, 0)
CreateCube (1, 50)
CreateEntity(1, MeshID(1), MaterialID(1), 20, 200, 300)
EntityPhysicBody(1, #PB_Entity_BoxBody, 1)
CreateCube (2, 50)
CreateEntity(2, MeshID(2), MaterialID(1), 20, 300, 300)
EntityPhysicBody(2, #PB_Entity_BoxBody, 1)
CreateCube (3, 50)
CreateEntity(3, MeshID(3), MaterialID(2), 20, 30, 500)
EntityPhysicBody(3, #PB_Entity_BoxBody)
Repeat
ExamineKeyboard()
Test()
FlipBuffers()
RenderWorld(60)
Until exit = #True
End
Procedure Test()
If KeyboardPushed (#PB_Key_W)
playerZ + Cos(Radian(CameraYaw(cCamera) + 180)) * playerSp
playerX + Sin(Radian(CameraYaw(cCamera) + 180)) * playerSp
MoveEntity(crEntPlayer, EntityX(crEntPlayer), EntityY(crEntPlayer), EntityZ(crEntPlayer) + 5, #PB_Absolute)
EndIf
If KeyboardPushed (#PB_Key_S)
playerZ - Cos(Radian(CameraYaw(cCamera) + 180)) * playerSp
playerX - Sin(Radian(CameraYaw(cCamera) + 180)) * playerSp
EndIf
If KeyboardPushed (#PB_Key_A)
playerZ + Cos(Radian(CameraYaw(cCamera) - 90)) * playerSp
playerX + Sin(Radian(CameraYaw(cCamera) - 90)) * playerSp
EndIf
If KeyboardPushed (#PB_Key_D)
playerZ + Cos(Radian(CameraYaw(cCamera) + 90)) * playerSp
playerX + Sin(Radian(CameraYaw(cCamera) + 90)) * playerSp
EndIf
If KeyboardPushed (#PB_Key_Space)
playerY + 10
EndIf
If KeyboardPushed (#PB_Key_LeftShift)
playerSp = 5
Else
playerSp = 2.5
EndIf
If KeyboardPushed (#PB_Key_Escape)
exit = #True
EndIf
If ExamineMouse()
camX = -MouseDeltaX() * 1 * 0.05
camY = -MouseDeltaY() * 1 * 0.05
EndIf
MoveCamera(cCamera, EntityX(crEntPlayer), EntityY(crEntPlayer) + 150, EntityZ(crEntPlayer) - 300, #PB_Absolute)
CameraLookAt(cCamera, EntityX(crEntPlayer), EntityY(crEntPlayer), EntityZ(crEntPlayer))
RotateCamera(cCamera, camY, 0, 0, #PB_Relative)
EndProcedure
Code: Select all
ExamineDesktops()
Global sizeW.i = DesktopWidth(0)
Global sizeH.i = DesktopHeight(0)
Global exit = #False
Global camX.d = 0
Global camY.d = 0
Global playerX.d = 400
Global playerY.d = 50
Global playerZ.d = 500
Global playerSp.d = 2.5
Declare Test()
Declare CameraFollow()
If InitEngine3D(0) = 0 Or InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 ;Or UsePNGImageDecoder() = 0 Or UseJPEGImageDecoder() = 0
exit = #True
EndIf
OpenScreen (sizeW, sizeH, 32, "Game3D")
SetFrameRate(60)
Global cCamera.i = CreateCamera (#PB_Any, 0, 0, 100, 100)
MoveCamera(cCamera, 0, playerY, 0, #PB_Absolute)
crMechPlayer.i = CreateCube (#PB_Any, 1)
Global crEntPlayer.i = CreateEntity(#PB_Any, MeshID(crMechPlayer), #PB_Material_None, 0, 20, 0)
EntityPhysicBody(crEntPlayer, #PB_Entity_BoxBody, 1, 0, 0)
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)
AmbientColor(RGB(255, 255, 255))
CreateTexture(0,32,32)
StartDrawing(TextureOutput(0))
Box(0,0,16,16,RGB(255,0,0))
Box(16,16,32,32,RGB(0,120,255))
StopDrawing()
CreateTexture(1,32,32)
StartDrawing(TextureOutput(1))
Box(0,0,16,16,RGB(0,255,0))
Box(16,16,32,32,RGB(0,255,255))
StopDrawing()
CreateTexture(2,32,32)
StartDrawing(TextureOutput(2))
Box(0,0,16,16,RGB(255,0,0))
Box(16,16,32,32,RGB(255,125,0))
StopDrawing()
CreateMaterial(0, TextureID(0))
CreateMaterial(1, TextureID(1))
CreateMaterial(2, TextureID(2))
CreatePlane(0, 128, 128, 40, 40, 50, 50)
CreateEntity(0, MeshID(0), MaterialID(0))
EntityPhysicBody(0, #PB_Entity_StaticBody, 0)
CreateCube (1, 1)
CreateEntity(1, MeshID(1), MaterialID(1), 20, 20, 30)
EntityPhysicBody(1, #PB_Entity_BoxBody, 1)
CreateCube (2, 1)
CreateEntity(2, MeshID(2), MaterialID(1), 20, 30, 30)
EntityPhysicBody(2, #PB_Entity_BoxBody, 1)
CreateCube (3, 1)
CreateEntity(3, MeshID(3), MaterialID(2), 20, 30, 500)
EntityPhysicBody(3, #PB_Entity_StaticBody)
WorldDebug(#PB_World_DebugBody)
Repeat
If ExamineKeyboard()
Test()
EndIf
CameraFollow()
FlipBuffers()
RenderWorld(60)
Until exit = #True
End
Procedure Test()
playerSp = 0
If KeyboardPushed (#PB_Key_Up)
playerSp = 5
ElseIf KeyboardPushed (#PB_Key_Down)
playerSp = -5
EndIf
If KeyboardPushed (#PB_Key_Left)
RotateEntity(crEntPlayer, 0, 1, 0, #PB_Relative)
ElseIf KeyboardPushed (#PB_Key_Right)
RotateEntity(crEntPlayer, 0, -1, 0, #PB_Relative)
EndIf
If KeyboardPushed (#PB_Key_Space)
playerY + 10
EndIf
If KeyboardPushed (#PB_Key_Escape)
exit = #True
EndIf
If ExamineMouse()
camX = -MouseDeltaX() * 1 * 0.05
camY = -MouseDeltaY() * 1 * 0.05
EndIf
MoveEntity(crEntPlayer, 0, -9, playerSp, #PB_Local)
EndProcedure
Procedure CameraFollow()
Distance = 10
Angle.f = EntityYaw(crEntPlayer)-180
x.f = EntityX(crEntPlayer)+Sin(Radian(Angle))*Distance
z.f = EntityZ(crEntPlayer)+Cos(Radian(Angle))*Distance
MoveCamera(cCamera, x, EntityY(crEntPlayer) + 5, z, #PB_Absolute)
CameraLookAt(cCamera, EntityX(crEntPlayer), EntityY(crEntPlayer), EntityZ(crEntPlayer))
EndProcedure