Page 1 of 1

Question on physics with AttachEntityObject

Posted: Thu Feb 16, 2017 6:09 pm
by Samuel
Hello everyone,

I was fiddling with PB's physic system a little bit and came across a problem.
Does anyone know if it's possible to attach an entity to another entities' skeleton and then have that first entities' physics update accordingly.

In the example I put together below. I created a Sinbad entity and then attached a cube to his right calf bone. I'd like to have the physics to be able to work on this cube as Sinbad moves around.
Sadly, It doesn't seem to work. When I use WorldDebug(#PB_World_DebugBody) I can see that the cubes' physics do exist but they just don't follow Sinbad around.

Does anyone know how to make this work if at all?

I'm using PB 5.50.

Code: Select all

;Use Up and Down arrow keys to move Sinbad.

EnableExplicit


InitEngine3D()
InitSprite()
InitKeyboard()


Structure Vector3
  X.d
  Y.d
  Z.d
EndStructure

Structure CharacterData
  Handle.i
  Node.i
  Position.Vector3
EndStructure
Global.CharacterData Sinbad


Define.i CTR
Define.i Entity
Define.i Event
Define.i WinH = 720
Define.i WinW = 1280


Declare SinbadOnGround()


If OpenWindow(0, 0, 0, WinW, WinH, "3D", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, WinW, WinH)
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models/", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/Sinbad", #PB_3DArchive_Zip)
  Parse3DScripts()
  
  CreateCamera(0, 0, 0, 100, 100)
  CameraBackColor(0, RGB(40,60,80))
  MoveCamera(0, 0, 5, 60)
  CameraLookAt(0, 0, 0, 0)
  
  CreateLight(0, RGB(200,200,200), 50, 50, 50)
  
  LoadTexture(0, "grass.jpg")
  CreateMaterial(0, TextureID(0))
  
  LoadTexture(1, "Dirt.jpg")
  CreateMaterial(1, TextureID(1))
  
  CreatePlane(0, 100, 100, 5, 5, 10, 10)
  CreateEntity(0, MeshID(0), MaterialID(0))
  CreateEntityBody(0, #PB_Entity_StaticBody)
  
  CreateCube(1, 10)
  Entity = CreateEntity(#PB_Any, MeshID(1), MaterialID(1), 0, 0, -10)
  CreateEntityBody(Entity, #PB_Entity_StaticBody)
  
  With Sinbad
    LoadMesh(2, "Sinbad.mesh")
    \Handle = CreateEntity(#PB_Any, MeshID(2), #PB_Material_None, 0, 5, 0)
    ScaleEntity(\Handle, 0.5, 0.5, 0.5)
    
    CreateCube(3, 2)
    CreateEntity(3, MeshID(3), #PB_Material_None, 0, 5, 0)
    AttachEntityObject(\Handle, "Calf.R", EntityID(3))
    CreateEntityBody(3, #PB_Entity_BoxBody)
    
    RayCollide(EntityX(\Handle), EntityY(\Handle), EntityZ(\Handle), EntityX(\Handle), EntityY(\Handle)-10,  EntityZ(\Handle)) 
    
    \Position\X = 0
    \Position\Y = PickY() + 2.45
    \Position\Z = 0
    
    MoveEntity(\Handle, \Position\X, \Position\Y, \Position\Z, #PB_Absolute)
    StartEntityAnimation(\Handle, "RunBase")
  EndWith
    
  WorldDebug(#PB_World_DebugBody)
  
  Repeat
    Event = WindowEvent()
    
    ExamineKeyboard()
    
    If KeyboardPushed(#PB_Key_Up)
      With Sinbad
        \Position\Z + 0.1
        MoveEntity(\Handle, \Position\X, \Position\Y, \Position\Z, #PB_Absolute)
      EndWith
    EndIf
    
    If KeyboardPushed(#PB_Key_Down)
      With Sinbad
        \Position\Z - 0.1
        MoveEntity(\Handle, \Position\X, \Position\Y, \Position\Z, #PB_Absolute)
      EndWith
    EndIf
    
    If KeyboardReleased(#PB_Key_Escape)
      End
    EndIf
    
    SinbadOnGround()
    
    CameraFollow(0, EntityID(Sinbad\Handle), 45, 5, 10, 1, 1)
    
    RenderWorld()
    FlipBuffers()
    
  Until Event = #PB_Event_CloseWindow
EndIf
End

Procedure SinbadOnGround()
  
  With Sinbad
    RayCollide(EntityX(\Handle), EntityY(\Handle), EntityZ(\Handle), EntityX(\Handle), EntityY(\Handle)-10,  EntityZ(\Handle)) 
    
    \Position\Y = PickY() + 2.45
    MoveEntity(\Handle, \Position\X, \Position\Y, \Position\Z, #PB_Absolute)
  EndWith
  
EndProcedure

Re: Question on physics with AttachEntityObject

Posted: Sat Feb 18, 2017 9:41 am
by DK_PETER
Hey Samuel.
You can't (afaik) make bone attachments and retain their individual physics properties.
You can attach physics (AddSubEntity) objects to Sinbad.

Edit:
I know this probably isn't what you want/need, but here's an example for AddSubEntity:
Try shifting between #PB_Entity_ConvexHullBody and #PB_Entity_CompoundBody.

Hope you find a viable solution to your problem.

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()

Structure _Object
  id.i
  ms.i
  ma.i
  tx.i
EndStructure

Global Dim small._Object(2)
Global Main._Object, floor._Object

AntialiasingMode(#PB_AntialiasingMode_x6)
OpenWindow(0, 0, 0, 800, 600, "Crash 3D", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, -18, 30)

CreateLight(0, $FFFFFF, 0, -20, 10)

floor\ms = CreatePlane(#PB_Any, 10, 10, 1, 1, 1, 1)
floor\tx = CreateTexture(#PB_Any, 100, 100, "Tmp")
StartDrawing(TextureOutput(floor\tx))
DrawingMode(#PB_2DDrawing_Gradient)
FrontColor(RGB(Random(255,50), Random(255,50), Random(255,50)))
BackColor(RGB(Random(200,100), Random(200,100), Random(200,100)))
BoxedGradient(0, 0, 100, 100)
Box(0, 0, 100, 100)
StopDrawing()
floor\ma = CreateMaterial(#PB_Any, TextureID(floor\tx))
MaterialCullingMode(floor\ma, #PB_Material_NoCulling)
floor\id = CreateEntity(#PB_Any, MeshID(floor\ms), MaterialID(floor\ma), 0, -3, -10)
RotateEntity(floor\id, 45, 0, 0)
ScaleEntity(floor\id, 10, 10, 10)
CreateEntityBody(floor\id, #PB_Entity_StaticBody)


Main\ms = CreateSphere(#PB_Any, 1)
Main\tx = CreateTexture(#PB_Any, 1024, 1024, "Face")
StartDrawing(TextureOutput(Main\tx))
Box(0, 0, 1024, 1024, $FFFFFFF)
Ellipse(512, 768, 80, 40, $333333)
Ellipse(512, 748, 80, 40, $FFFFFF)
Circle(450, 512, 40, $777777)
Circle(574, 512, 40, $777777)
Circle(450, 512, 10, $FFFFFF)
Circle(574, 512, 10, $FFFFFF)
Circle(450, 512, 2, $111111)
Circle(574, 512, 2, $111111)

Circle(512, 620, 20, $555555)
StopDrawing()
Main\ma = CreateMaterial(#PB_Any, TextureID(Main\tx))
MaterialFilteringMode(Main\ma,  #PB_Material_Anisotropic, 8)
Main\id = CreateEntity(#PB_Any, MeshID(Main\ms), MaterialID(Main\ma), 0, 0, -10)
RotateEntity(Main\id, 0, 180, 0)

With small(0)
  \ms = CreateSphere(#PB_Any, 0.3)
  \tx = CreateTexture(#PB_Any, 256, 256, "Ear_1")
  StartDrawing(TextureOutput(\tx))
  Box(0, 0, 256, 256, $FFFFFF)
  Box(0, 0, 128, 256, $555555)
  StopDrawing()
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \id = CreateEntity(#PB_Any, MeshID(\ms),MaterialID(\ma), -0.8, 0.90, 0) 
  RotateEntity(\id, 0, 75, 0)
EndWith

With small(1)
  \ms = CreateSphere(#PB_Any, 0.3)
  \tx = CreateTexture(#PB_Any, 256, 256, "Ear_2")
  StartDrawing(TextureOutput(\tx))
  Box(0, 0, 256, 256, $FFFFFF)
  Box(0, 0, 128, 256, $555555)
  StopDrawing()
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \id = CreateEntity(#PB_Any, MeshID(\ms),MaterialID(\ma), 0.8, 0.90, 0) 
  RotateEntity(\id, 0, 115, 0)
EndWith

;Attaching...
AddSubEntity(Main\id, small(0)\id, #PB_Entity_SphereBody)
AddSubEntity(Main\id, small(1)\id, #PB_Entity_SphereBody)
;CreateEntityBody(Main\id, #PB_Entity_CompoundBody)   ;Try this
CreateEntityBody(Main\id, #PB_Entity_ConvexHullBody)  ;instead of this to see the difference.

Repeat
  
  Repeat
    ev = WindowEvent()
  Until ev = 0
  
  RenderWorld()
  
  FlipBuffers()
  
  ExamineKeyboard()
  
Until KeyboardPushed(#PB_Key_Escape)

Re: Question on physics with AttachEntityObject

Posted: Mon Feb 20, 2017 5:49 pm
by Samuel
Yes it looks like what I'm after isn't possible. Your example doesn't really help me in this case, but thanks anyways.