Gravity in OGRE3d

Advanced game related topics
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Gravity in OGRE3d

Post by Rook Zimbabwe »

Perhaps I should have said Camera Gravity? Has anyone figured out a simpler mayhem to apply gravity (and I suppose collision since they were integral when I werked with Blitz3D) to a camera entity?

and why does it seem that a camera entity is a different entity than a mesh entity?

is this something to be finished? :?
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Gravity in OGRE3d

Post by DarkDragon »

You need to attach the camera to an invisible entity with the nodes (see help file).
bye,
Daniel
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Gravity in OGRE3d

Post by Rook Zimbabwe »

well I read that and this is my attempt...

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Terrain
;   
;   modofied by Ralph W Dunn / Rook ZImbabwe
;    (c) 2003 - Fantaisie Software
;    modified 3 MARCH 2011
;
; ------------------------------------------------------------
;

version$ = "0.05a"

#CameraSpeed = 0.4

#Thing1 = 1
#CRETE = 2
#PLAYER = 3
#DUDE = 4

Global NewY ; feeble attempt at gravity

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  Add3DArchive("Data", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    AmbientColor(RGB(255,255,255))
    
    LoadMesh   (0 , "thing1.mesh") ; a block or cube... can be gotten from my basic MESH pack
    LoadTexture(0, "RUST1.jpg") ; something to make it look nice
    CreateMaterial(#CRETE, TextureID(0))
    CreateEntity(#Thing1, MeshID(0), MaterialID(#CRETE),10,10,10)
    CreateNode(#DUDE) ; ************************************ INITAIL NODE INIT
    
    ; throw down a floor for gravity to work on
    
    ey = 7
    ez = 7
    For cx = 0 To 10
      For cy = 0 To 10
        Result = CopyEntity(#Thing1, #PB_Any)
        EntityLocate(Result, ey, 0, ez)
        ez = ez + 7
      Next
      ey = ey + 7
      ez = 7
    Next
    
    ; make something to see the floor with
    
    CreateCamera(#PLAYER, 0, 0, 100, 150)
    CameraLocate(#PLAYER, 50, 50, 50)
    
    AttachNodeObject(#DUDE,  CameraID(#PLAYER), #PB_Node_Camera) ; should this not meld them together?
    ; ***** But when I try to move the node... it no move right!
    
    CreateWater(#PLAYER, 0,-6,0, 230,#PB_World_WaterMediumQuality)

    SkyDome("cloud_2.bmp",-10) ; my clouds texture...
    
    ;ReleaseMouse(1) ; not needed as we are using mouse as pointer
    
    EnableWorldPhysics(1)
    EnableWorldCollisions(1)

    ;WorldShadows(#PB_Shadow_Modulative) ; turned off... with water it gets freaky on baser computers
    
    Repeat
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_A)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_D)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_W)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_X)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
        
      EndIf
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)*#CameraSpeed ; dropped the /2 cause it was slowing me down
        MouseY = -(MouseDeltaY()/10)*#CameraSpeed
      EndIf
      
      ; to move the new node entity
      
      RotateNode(#DUDE, MouseY, MouseX, RollZ, #PB_Relative)
      MoveNode(#DUDE, KeyX, -CameraY(#PLAYER)+Height+14, KeyY)
            
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

you only need a CUBE mesh... and 2 textures (1 for skydome / 1 for cube) I used the cube out of my sample pack...

movement is wonky and not at all satisfying... gravity still does not seem to work... I think the node IS the focus of movement though... seems to be in orbit around the cube entity that I made... what am I doing wrong?
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Gravity in OGRE3d

Post by Rook Zimbabwe »

nope... still not gravitizing...
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Gravity in OGRE3d

Post by DarkDragon »

You don't have a physics body for your entity ...

I modified the SimpleCollision.pb example from http://www.purebasic.com/Ogre1.6.zip for you:

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Camera
;
;    (c) 2002 - Fantaisie Software
;
; ------------------------------------------------------------
;

#CameraSpeed = 10

#Camera = 0

Enumeration
	#Robot
EndEnumeration

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()

  Add3DArchive("Data\", #PB_3DArchive_FileSystem)
  Add3DArchive("Cube\", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()

   	EnableWorldPhysics(#True)
   	EnableWorldCollisions(#True)
   	
    ;AmbientColor(RGB(128,128,128))
    
   	;WorldShadows(#PB_Shadow_Additive)
   	

    CreateMaterial(0, LoadTexture(0, "r2skin.jpg"))
    CreateMaterial(1, LoadTexture(1, "Background.png"))
    
    LoadMesh(0, "Cube.mesh")
    LoadMesh(1, "Robot.mesh")
    
    CreateCamera(#Camera, 0, 0, 100, 100)  ; Front camera
    CameraLocate(#Camera, 0, 200, 300)
    CameraLookAt(#Camera, 0, 0, 0)
    
    ;CreateLight(0, RGB(255,255,255),  100.0, 100, 0)

		; The cube mesh is 100x100x100
    Scale = 100
    
		; create the floor
		;    
 		Floor = CreateEntity(#PB_Any, MeshID(0), MaterialID(1), 0, -Scale, 0)
 		ScaleEntity(Floor, 11, 1, 11)
 		EntityPhysicBody(Floor, #PB_Entity_StaticBody)
    
    For x = -5 To 5 Step 2
    	For z = -5 To 5 Step 2
    		Block = CreateEntity(#PB_Any, MeshID(0), MaterialID(0), x*Scale, 0, z*Scale)
    		EntityPhysicBody(Block, #PB_Entity_StaticBody)
    		SetEntityMass(Block, 10.0)
    	Next
   	Next
   	
   	CreateEntity(#Robot, MeshID(1), MaterialID(0), 0, -30, 0)
   	EntityPhysicBody(#Robot, #PB_Entity_SphereBody, #PB_Entity_AbsoluteBodyMove)
   	SetEntityMass(#Robot, 1.1)
   	
   	WorldGravity(98.1)
   	
   	AnimateEntity(#Robot, "Walk")
   	
   	OldRobotX.d = EntityX(#Robot)
   	OldRobotZ.d = EntityZ(#Robot)
   	
    Repeat
      Screen3DEvents()
      
      If ExamineKeyboard()
      
      	SpeedX = 0
      	SpeedZ = 0
        
        If KeyboardPushed(#PB_Key_Left)
          SpeedX = -10
        ElseIf KeyboardPushed(#PB_Key_Right)
          SpeedX = 10
        Else
        EndIf
                  
        If KeyboardPushed(#PB_Key_Up)
	      	SpeedZ = -10
        ElseIf KeyboardPushed(#PB_Key_Down)
	      	SpeedZ = 10
        EndIf


      	MoveEntity(#Robot, SpeedX*3, 0, SpeedZ*3)
      EndIf
      
      NewRobotX.d = EntityX(#Robot)
      NewRobotZ.d = EntityZ(#Robot)
      
      RobotSpeedX.d = NewRobotX - OldRobotX
      RobotSpeedZ.d = NewRobotZ - OldRobotZ
      
      OldRobotX = NewRobotX
      OldRobotZ = NewRobotZ
      
      RobotSpeedLength.d = RobotSpeedX * RobotSpeedX + RobotSpeedZ * RobotSpeedZ
      If RobotSpeedLength > 0.05
        RobotSpeedLength = 1.0 / Sqr(RobotSpeedLength)
        
        RobotSpeedX * RobotSpeedLength
        RobotSpeedZ * RobotSpeedLength
        
;         EntityLookAt(#Robot, EntityX(#Robot) + RobotSpeedX, EntityY(#Robot), EntityZ(#Robot) + RobotSpeedZ)
        
        CameraLocate(#Camera, EntityX(#Robot) - RobotSpeedX * 100.0, EntityY(#Robot) + 130.0, EntityZ(#Robot) - RobotSpeedZ * 100.0)
        CameraLookAt(#Robot, EntityX(#Robot), EntityY(#Robot) + 70.0, EntityZ(#Robot))
      EndIf
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
  
End
For a first person view just replace the lines containing

Code: Select all

        CameraLocate(#Camera, EntityX(#Robot) - RobotSpeedX * 100.0, EntityY(#Robot) + 130.0, EntityZ(#Robot) - RobotSpeedZ * 100.0)
        CameraLookAt(#Robot, EntityX(#Robot), EntityY(#Robot) + 70.0, EntityZ(#Robot)))
with

Code: Select all

        HideEntity(#Robot, 1)
        CameraLocate(#Camera, EntityX(#Robot), EntityY(#Robot) + 50.0, EntityZ(#Robot))
        CameraLookAt(#Robot, CameraX(#Camera) + RobotSpeedX, CameraY(#Camera), CameraZ(#Camera) + RobotSpeedZ
bye,
Daniel
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Gravity in OGRE3d

Post by Rook Zimbabwe »

Daniel to my rescue again... :D

I am parsing your example... it is very nice and I am understanding the mechanism in the included physics body more now... I tried to read something about it in the OGRE docs but my dyslexia started making the letters swim!

OK now my next step is to figure out diablo cam view and integrate random pathing and rooms just for fun :mrgreen:

(yeah like I can do that!) :wink:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply