First-person view with collisions in 3d

Everything related to 3D programming
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: First-person view with collisions in 3d

Post by Comtois »

i modified your code.
Read this, your mesh are too big.

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
Please correct my english
http://purebasic.developpez.com/
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: First-person view with collisions in 3d

Post by Samuel »

Thank you for the link, Comtois! Your code runs perfect. No problems at all.
I'll have to make sure all of my meshes, that use physics, are within the proper scale.
Post Reply