problem with collision and moveentity

Advanced game related topics
alan_solis
User
User
Posts: 12
Joined: Thu Oct 23, 2008 5:19 pm
Location: Mexico

problem with collision and moveentity

Post by alan_solis »

Hi
I was playing with the example code SimpleCollision.pb and changed the
code to every time the robot is moved; plays the Walk animation and the Idle
animation when the robot stops.
It seems to work fine, but when I changed
the part of the collision code, to send the robot backwards when hit a cube,
It work when I call MessageRequester before, but if I comment out the
line MessageRequester("Collision Detected", "Collision in Z"), or the X one,
the MoveEntity don’t send the robot backward.

Somebody can help to correct this?
I’m still learning Purebasic, Ogre and English :D

By the way I’m using 4.3 beta 3 and a ogre1.6 example
Thanks

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"))
    CreateMaterial(2, LoadTexture(2, "BumpyMetal.jpg"))
    
    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(2), x*Scale, 0, z*Scale)
    		EntityPhysicBody(Block, #PB_Entity_BoxBody)
    		SetEntityMass(Block, 10)
    	Next
   	Next
   	
   	CreateEntity(#Robot, MeshID(1), MaterialID(0), 0, -30, 0)
   	EntityPhysicBody(#Robot, #PB_Entity_SphereBody, #PB_Entity_AbsoluteBodyMove)
   	;SetEntityFriction(#Robot, 0)
   	SetEntityMass(#Robot, 0.01)
   	AnimateEntity(#Robot, "Idle")
   	LastAnimMode.s="Idle"
   	
   	EnableWorldCollisions(1)
   	  	 
    Repeat
      Screen3DEvents()
      
      If ExamineKeyboard()
      
      	SpeedX = 0
      	SpeedZ = 0
        
        If KeyboardPushed(#PB_Key_Left)
          RotateEntity(#Robot, 0, 180, 0)
          SpeedX = -10
        ElseIf KeyboardPushed(#PB_Key_Right)
          RotateEntity(#Robot, 0, 0, 0)
          SpeedX = 10      
        ElseIf KeyboardPushed(#PB_Key_Up)
          RotateEntity(#Robot, 0, 90, 0)
	      	SpeedZ = -10
        ElseIf KeyboardPushed(#PB_Key_Down)
          RotateEntity(#Robot, 0, 270, 0)
	      	SpeedZ = 10
        EndIf
     	  
     	  MoveEntity(#Robot, SpeedX*3, 0, SpeedZ*3)
     	  
     	  If SpeedX=0 And SpeedZ=0
     	    ;idle state
     	    AnimateEntity(#Robot, "Idle")
     	    SetEntityAnimationTime(#Robot, AnimIdle.f)
     	    LastAnimMode.s="Idle"
        Else
          ;walking state
          AnimateEntity(#Robot, "Walk")
          SetEntityAnimationTime(#Robot, AnimWalk.f)
          LastAnimMode.s="Walk"
        EndIf

      EndIf
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)*#CameraSpeed/2
        MouseY = -(MouseDeltaY()/10)*#CameraSpeed/2
      EndIf
      
      
      RotateCamera(0, MouseY, MouseX, RollZ, #PB_Relative)
      ;MoveCamera  (0, KeyX, 0, KeyY)
      
      If ExamineWorldCollisions()
        
      	While NextWorldCollision()

      		If FirstWorldCollisionEntity()=#Robot Or SecondWorldCollisionEntity()=#Robot
      		   
      		  ;robot hit a cube
      		  If SpeedX<>0
      		    MessageRequester("Collision Detected", "Collision in X")
      		    If SpeedX<0   
      	        MoveEntity(#Robot, 100, 0, 0)		    
      			  Else
      			    MoveEntity(#Robot, -100, 0, 0)
      			  EndIf  
      			ElseIf SpeedZ<>0
      			  MessageRequester("Collision Detected", "Collision in Z")
      			  If SpeedZ<0
      			    MoveEntity(#Robot, 0, 0, 100)
      			  Else
      			    MoveEntity(#Robot, 0, 0, -100)
      			  EndIf  
      			EndIf
      		EndIf
      		
      	Wend
      EndIf
      
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
      
      If LastAnimMode.s="Walk"
        AnimWalk.f = GetEntityAnimationTime(#Robot)
      Else
        AnimIdle.f = GetEntityAnimationTime(#Robot)
      EndIf
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
  
End
Lykaestria
User
User
Posts: 76
Joined: Wed Sep 17, 2008 3:10 am
Location: New Zealand

Post by Lykaestria »

I hope this code helps... oh, and welcome to PureBASIC :)

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"))
    CreateMaterial(2, LoadTexture(2, "BumpyMetal.jpg"))
   
    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(2), x*Scale, 0, z*Scale)
          EntityPhysicBody(Block, #PB_Entity_BoxBody)
          SetEntityMass(Block, 10)
       Next
      Next
      
      CreateEntity(#Robot, MeshID(1), MaterialID(0), 0, -30, 0)
      EntityPhysicBody(#Robot, #PB_Entity_SphereBody, #PB_Entity_AbsoluteBodyMove)
      ;SetEntityFriction(#Robot, 0)
      SetEntityMass(#Robot, 0.01)
      AnimateEntity(#Robot, "Idle")
      LastAnimMode.s="Idle"
      
      EnableWorldCollisions(1)
           
    Repeat
      Screen3DEvents()
     
      If ExamineKeyboard()
     
         SpeedX = 0
         SpeedZ = 0
       
        If KeyboardPushed(#PB_Key_Left)
          RotateEntity(#Robot, 0, 180, 0)
          SpeedX = -10
        ElseIf KeyboardPushed(#PB_Key_Right)
          RotateEntity(#Robot, 0, 0, 0)
          SpeedX = 10     
        ElseIf KeyboardPushed(#PB_Key_Up)
          RotateEntity(#Robot, 0, 90, 0)
            SpeedZ = -10
        ElseIf KeyboardPushed(#PB_Key_Down)
          RotateEntity(#Robot, 0, 270, 0)
            SpeedZ = 10
        EndIf
         
          MoveEntity(#Robot, SpeedX*3, 0, SpeedZ*3)
         
          If SpeedX=0 And SpeedZ=0
            ;idle state
            AnimateEntity(#Robot, "Idle")
            SetEntityAnimationTime(#Robot, AnimIdle.f)
            LastAnimMode.s="Idle"
        Else
          ;walking state
          AnimateEntity(#Robot, "Walk")
          SetEntityAnimationTime(#Robot, AnimWalk.f)
          LastAnimMode.s="Walk"
        EndIf

      EndIf
     
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)*#CameraSpeed/2
        MouseY = -(MouseDeltaY()/10)*#CameraSpeed/2
      EndIf
     
     
      RotateCamera(0, MouseY, MouseX, RollZ, #PB_Relative)
      ;MoveCamera  (0, KeyX, 0, KeyY)
     
      If ExamineWorldCollisions()
       
         While NextWorldCollision()

            If FirstWorldCollisionEntity()=#Robot Or SecondWorldCollisionEntity()=#Robot
               
              ;robot hit a cube

                HideEntity(#Robot, 1)
                If SpeedX<0   
                  MoveEntity(#Robot, 700, 0, 0)         
                ElseIf SpeedX>0
                  MoveEntity(#Robot, -700, 0, 0)
                EndIf 

                If SpeedZ<0
                  MoveEntity(#Robot, 0, 0, 700)
                ElseIf SpeedZ>0
                  MoveEntity(#Robot, 0, 0, -700)
                EndIf
                HideEntity(#Robot, 0)
            EndIf
            
         Wend
      EndIf
     
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
     
      If LastAnimMode.s="Walk"
        AnimWalk.f = GetEntityAnimationTime(#Robot)
      Else
        AnimIdle.f = GetEntityAnimationTime(#Robot)
      EndIf
     
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
   
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
 
End 
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

CreateMaterial(1, LoadTexture(1, "Background.png"))
CreateMaterial(2, LoadTexture(2, "BumpyMetal.jpg"))
Where are those files?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
alan_solis
User
User
Posts: 12
Joined: Thu Oct 23, 2008 5:19 pm
Location: Mexico

Post by alan_solis »

Thank Lykaestria, works great!

Psychophanta:
Background.png is in the Ogre1.6\Data
and i got BumpyMetal.jpg from the media that comes with
the c/c++ demos (ogre3d site).
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

alan_solis wrote:Psychophanta:
Background.png is in the Ogre1.6\Data
and i got BumpyMetal.jpg from the media that comes with
the c/c++ demos (ogre3d site).
Thanks
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply