Page 1 of 1

Problem with TransformMesh using Physics

Posted: Sat May 02, 2020 3:07 pm
by Psychophanta
Hi.
The code says it all.
When turning entity without touching the mes, all is ok, but when transform the mesh , it does not transform its bounding shape.

Code: Select all

InitEngine3D():InitSprite():InitKeyboard()
RX.u=1280:RY.u=720
OpenWindow(0,0,0,RX,RY,"",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,RX,RY,1,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home+"examples/3D/Data/Textures",#PB_3DArchive_FileSystem)
CreateLight(0,$EEEEEE,4,4,2,#PB_Light_Point):SetLightColor(0,#PB_Light_DiffuseColor,$EEEEEE):MoveLight(0,4,4,2,#PB_Absolute):AmbientColor($FFFFFF)
LoadTexture(0,"wood.jpg"):CreateMaterial(0,TextureID(0))
CreateCube(0,10):TransformMesh(0,0,0,0,2,0.01,2,0,0,0,0):UpdateMeshBoundingBox(0)
CreateEntity(0,MeshID(0),MaterialID(0),0,-0.05,0)
CreateEntityBody(0,#PB_Entity_StaticBody,1.0,0.8,0.3)
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,3,5)
Cameralookat(0,0,1,0)
LoadTexture(1,"RustySteel.jpg")
CreateMaterial(1,TextureID(1))
WorldDebug(#PB_World_DebugBody)
Repeat
  ExamineKeyboard()
  WindowEvent()
  If KeyboardReleased(#PB_Key_Space):way.b!1
    If IsEntity(1):FreeEntityBody(1):FreeEntity(1):EndIf
    If IsMesh(1):FreeMesh(1):EndIf
    CreateCone(1,0.2,1,16,8)
    If way
      UpdateMeshBoundingBox(1)
      CreateEntity(1,MeshID(1),MaterialID(1),0,0.5,0)
      RotateEntity(1,0,0,180)
    Else
      ; This way does not work!!: ?
      TransformMesh(1,0,0,0,1,1,1,0,0,180):UpdateMeshBoundingBox(1)
      CreateEntity(1,MeshID(1),MaterialID(1),0,0.5,0)
    EndIf
    CreateEntityBody(1,#PB_Entity_ConeBody,1,0.4,0.2):SetEntityAttribute(1,#PB_Entity_LinearSleeping,0.01):SetEntityAttribute(1,#PB_Entity_AngularSleeping,2.2)
  EndIf
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
EDITED to minimize code amount.

Re: Problem with TransformMesh using Physics

Posted: Sat May 02, 2020 3:46 pm
by DK_PETER
Nice find.
The UpdateMeshBoundingBox() should recalculate the boundingBox to the fit the shape of the object.
It doesn't....

Re: Problem with TransformMesh using Physics

Posted: Sat May 02, 2020 5:26 pm
by Psychophanta
DK_PETER wrote:Nice find.
The UpdateMeshBoundingBox() should recalculate the boundingBox to the fit the shape of the object.
It doesn't....
Indeed, that's the reason of the post.
May be a bug !

Re: Problem with TransformMesh using Physics

Posted: Wed May 20, 2020 12:19 pm
by T4r4ntul4
I want to share my 2 cents here, and maybe iam toally wrong:

I dont think theres a bug at all, what i see is that youre the first time rotate the entity, and the second time only the mesh.
With both times you use the command: UpdateMeshBoundingBox, but thats doing nothing, even with the first time. Clearly not needed at all.

What i think happens is, that you must rotate at all times only the boundingbox and not the mesh itself (its like a parent -> child), that way all works fine, try the following code:

in the docs it says by: UpdateMeshBoundingBox(): " If a mesh has been manually modified"
And thats what youre not doing. So my guess your using a command at the wrong place.

Code: Select all

Enumeration
  #BoundingBox = 10 ; Entity
  #ConeObject ; mesh
EndEnumeration


InitEngine3D():InitSprite():InitKeyboard()
RX.u=1280:RY.u=720
OpenWindow(0,0,0,RX,RY,"",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,RX,RY,1,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home+"examples/3D/Data/Textures",#PB_3DArchive_FileSystem)
CreateLight(0,$EEEEEE,4,4,2,#PB_Light_Point):SetLightColor(0,#PB_Light_DiffuseColor,$EEEEEE):MoveLight(0,4,4,2,#PB_Absolute):AmbientColor($FFFFFF)
LoadTexture(0,"wood.jpg"):CreateMaterial(0,TextureID(0))
CreateCube(0,10):TransformMesh(0,0,0,0,2,0.01,2,0,0,0,0):;UpdateMeshBoundingBox(0)
CreateEntity(0,MeshID(0),MaterialID(0),0,-0.05,0)
CreateEntityBody(0,#PB_Entity_StaticBody,1.0,0.8,0.3)
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,3,5)
CameraLookAt(0,0,1,0)
LoadTexture(1,"RustySteel.jpg")
CreateMaterial(1,TextureID(1))
WorldDebug(#PB_World_DebugBody)




Repeat
  ExamineKeyboard()
  WindowEvent()
  If KeyboardReleased(#PB_Key_Space)
    
    way.b!1
    
    If IsEntity(#BoundingBox)
      FreeEntityBody(#BoundingBox)
      FreeEntity(#BoundingBox)
    EndIf
    
    If IsMesh(#ConeObject)
      FreeMesh(#ConeObject)
    EndIf
    
    CreateCone(#ConeObject,0.2,1,16,8)

    
    If way
      CreateEntity(#BoundingBox,MeshID(#ConeObject),MaterialID(1),0,0.5,0)
      RotateEntity(#BoundingBox,0,0,150)
    Else
      CreateEntity(#BoundingBox,MeshID(#ConeObject),MaterialID(1),0,0.5,0)
      RotateEntity(#BoundingBox,0,0,210)
    EndIf
    
    CreateEntityBody(#BoundingBox,#PB_Entity_ConeBody,1,0.4,0.2)
    SetEntityAttribute(#BoundingBox,#PB_Entity_LinearSleeping,0.01)
    SetEntityAttribute(#BoundingBox,#PB_Entity_AngularSleeping,2.2)
    
    
    
  EndIf
  
  RenderWorld()
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape)

Re: Problem with TransformMesh using Physics

Posted: Thu Jun 18, 2020 10:28 pm
by Psychophanta
The point is that the wanted thing to do is to rotate the mesh in one of the both cases, and leave it untouched in the other case, to see the difference.

What you do is to perform the SAME in both cases, what is nonsense.

My tip is a demonstration about the fact that there seems not to exist a clean and direct way to just update the bounding box after altering the mesh.
So that, the UpdateMeshBoundingBox() is failing in its task.

Re: Problem with TransformMesh using Physics

Posted: Tue Jul 07, 2020 4:42 am
by wayne gray
First a bounding box is not a collision entity.
Perhaps working with entity commands accomplishes the goal.

Code: Select all

InitEngine3D():InitSprite():InitKeyboard()
RX.u=1280:RY.u=720
OpenWindow(0,0,0,RX,RY,"",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,RX,RY,1,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home+"examples/3D/Data/Textures",#PB_3DArchive_FileSystem)
CreateLight(0,$EEEEEE,4,4,2,#PB_Light_Point):SetLightColor(0,#PB_Light_DiffuseColor,$EEEEEE):MoveLight(0,4,4,2,#PB_Absolute):AmbientColor($FFFFFF)
LoadTexture(0,"wood.jpg"):CreateMaterial(0,TextureID(0))
CreateCube(0,10):TransformMesh(0,0,0,0,2,0.01,2,0,0,0,0):UpdateMeshBoundingBox(0)
CreateEntity(0,MeshID(0),MaterialID(0),0,-0.05,0)
CreateEntityBody(0,#PB_Entity_StaticBody,1.0,0.8,0.3)
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,3,5)
CameraLookAt(0,0,1,0)
LoadTexture(1,"RustySteel.jpg")
CreateMaterial(1,TextureID(1))
WorldDebug(#PB_World_DebugBody)
Repeat
  ExamineKeyboard()
  WindowEvent()
  If KeyboardReleased(#PB_Key_Space):way.b!1
    If IsEntity(1):FreeEntityBody(1):FreeEntity(1):EndIf
    If IsMesh(1):FreeMesh(1):EndIf
    
    CreateCone(1,0.2,1,16,8)
    CreateEntity(1,MeshID(1),MaterialID(1),0,0.5,0)
    
       
    If way
      RotateEntity(1,0,0,180)
     
    Else
      ; This way works ! 
      MoveEntity(1,1,0,0)
      RotateEntity(1,0,0,180)
            
    EndIf
     CreateEntityBody(1,#PB_Entity_ConeBody,1,0.4,0.2):SetEntityAttribute(1,#PB_Entity_LinearSleeping,0.01):SetEntityAttribute(1,#PB_Entity_AngularSleeping,2.2)
  EndIf
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)

Re: Problem with TransformMesh using Physics

Posted: Tue Jul 07, 2020 9:57 am
by wayne gray
lets take your code and instead of 180 rotation do 90.
Look at the results.
What I see is the transformed mesh, with updated bounding box,
and a collision body calculated based on new bounding box.

Code: Select all

InitEngine3D():InitSprite():InitKeyboard()
RX.u=1280:RY.u=720
OpenWindow(0,0,0,RX,RY,"",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,RX,RY,1,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home+"examples/3D/Data/Textures",#PB_3DArchive_FileSystem)
CreateLight(0,$EEEEEE,4,4,2,#PB_Light_Point):SetLightColor(0,#PB_Light_DiffuseColor,$EEEEEE):MoveLight(0,4,4,2,#PB_Absolute):AmbientColor($FFFFFF)
LoadTexture(0,"wood.jpg"):CreateMaterial(0,TextureID(0))
CreateCube(0,10):TransformMesh(0,0,0,0,2,0.01,2,0,0,0,0):UpdateMeshBoundingBox(0)
CreateEntity(0,MeshID(0),MaterialID(0),0,-0.05,0)
CreateEntityBody(0,#PB_Entity_StaticBody,1.0,0.8,0.3)
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,3,5)
CameraLookAt(0,0,1,0)
LoadTexture(1,"RustySteel.jpg")
CreateMaterial(1,TextureID(1))
WorldDebug(#PB_World_DebugBody)
Repeat
  ExamineKeyboard()
  WindowEvent()
  If KeyboardReleased(#PB_Key_Space):way.b!1
    If IsEntity(1):FreeEntityBody(1):FreeEntity(1):EndIf
    If IsMesh(1):FreeMesh(1):EndIf
    CreateCone(1,0.2,1,16,8)
    If way
      UpdateMeshBoundingBox(1)
      CreateEntity(1,MeshID(1),MaterialID(1),0,0.5,0)
      RotateEntity(1,0,0,180)
    Else
      ; This way does not work!!: ?
      TransformMesh(1,0,0,0,1,1,1,0,0,90):UpdateMeshBoundingBox(1)
      CreateEntity(1,MeshID(1),MaterialID(1),0,0.5,0)
    EndIf
    CreateEntityBody(1,#PB_Entity_ConeBody,1,0.4,0.2):SetEntityAttribute(1,#PB_Entity_LinearSleeping,0.01):SetEntityAttribute(1,#PB_Entity_AngularSleeping,2.2)
  EndIf
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)

Re: Problem with TransformMesh using Physics

Posted: Tue Jul 07, 2020 1:04 pm
by wayne gray
My final thoughts, once you transform the primitive using convex hull be better option.

Code: Select all

InitEngine3D():InitSprite():InitKeyboard()
RX.u=1280:RY.u=720
Define min_x.f, min_y.f, min_z.f
Define max_x.f, max_y.f, max_z.f
OpenWindow(0,0,0,RX,RY,"",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,RX,RY,1,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home+"examples/3D/Data/Textures",#PB_3DArchive_FileSystem)
CreateLight(0,$EEEEEE,4,4,2,#PB_Light_Point):SetLightColor(0,#PB_Light_DiffuseColor,$EEEEEE):MoveLight(0,4,4,2,#PB_Absolute):AmbientColor($FFFFFF)
LoadTexture(0,"wood.jpg"):CreateMaterial(0,TextureID(0))
CreateCube(0,10):TransformMesh(0,0,0,0,2,0.01,2,0,0,0,0):UpdateMeshBoundingBox(0)
CreateEntity(0,MeshID(0),MaterialID(0),0,-0.05,0)
CreateEntityBody(0,#PB_Entity_StaticBody,1.0,0.8,0.3)
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,3,5)
CameraLookAt(0,0,1,0)
LoadTexture(1,"RustySteel.jpg")
CreateMaterial(1,TextureID(1))
WorldDebug(#PB_World_DebugBody)
Repeat
  ExamineKeyboard()
  WindowEvent()
  If KeyboardReleased(#PB_Key_Space):way.b!1
    If IsEntity(1):FreeEntityBody(1):FreeEntity(1):EndIf
    If IsMesh(1):FreeMesh(1):EndIf
    CreateCone(1,0.2,1,16,8)
    If way
      UpdateMeshBoundingBox(1)
      CreateEntity(1,MeshID(1),MaterialID(1),0,0.5,0)
      RotateEntity(1,0,0,180)

    Else
      ; This way does not work!!: ?
      TransformMesh(1,0,0,0,1,1,1,0,0,180):UpdateMeshBoundingBox(1)
      CreateEntity(1,MeshID(1),MaterialID(1),0,0.5,0)
    EndIf
    CreateEntityBody(1,#PB_Entity_ConvexHullBody,1,0.4,0.2):SetEntityAttribute(1,#PB_Entity_LinearSleeping,0.01):SetEntityAttribute(1,#PB_Entity_AngularSleeping,2.2)
  EndIf
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)

Re: Problem with TransformMesh using Physics

Posted: Wed Jul 08, 2020 12:47 pm
by wayne gray
All is working correctly.
You transformed the mesh 180 and that resulted in the same bounding box.
You got the collision cone based on the bounding box.
You got exactly what you asked for.

Re: Problem with TransformMesh using Physics

Posted: Sat Jul 11, 2020 11:43 am
by DK_PETER
@wayne Grey

Code: Select all

Syntax: UpdateMeshBoundingBox(#Mesh)

Code: Select all

Description:
Update the bounding box of the mesh. If a mesh has been manually modified, its bounding box has to be recalculated, especially if the mesh is used for collisions. The bounding box is the smallest box which can contain the whole mesh. 
In the last example you're using #PB_Entity_ConvexHullBody, which isn't a problem.
ConvexHullBody is doing more calculations to conform the 'shape' and is slower than basic colliding boxes.

Wether you're transforming a cone from a basic form, streching it or rotating in any direction, the
UpdateMeshBoundingBox(#Mesh) should conform to the new shape as it is still a cone object.

Try this example: 'push' the cone using up and down arrow in each instance.
Again: Using ConvexHullBody will suffice just fine on machines today and works flawlessly
but it shouldn't be necessary to use convexhullbody for a simple cone object no matter the size or rotation.

Code: Select all

InitEngine3D():InitSprite():InitKeyboard()
RX.u=1280:RY.u=720
OpenWindow(0,0,0,RX,RY,"Space, key up, key down, escape",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,RX,RY,1,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home+"examples/3D/Data/Textures",#PB_3DArchive_FileSystem)
CreateLight(0,$EEEEEE,4,4,2,#PB_Light_Point):SetLightColor(0,#PB_Light_DiffuseColor,$EEEEEE):MoveLight(0,4,4,2,#PB_Absolute):AmbientColor($FFFFFF)
LoadTexture(0,"wood.jpg"):CreateMaterial(0,TextureID(0))
CreateCube(0,1):TransformMesh(0,0,0,0,2,0.01,2,0,0,0,0):UpdateMeshBoundingBox(0)
CreateEntity(0,MeshID(0),MaterialID(0),0,-0.05,0)
CreateEntityBody(0,#PB_Entity_StaticBody,1.0,0.8,0.3)
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,3,3)
CameraLookAt(0,0,0,0)
LoadTexture(1,"RustySteel.jpg")
CreateMaterial(1,TextureID(1))
WorldDebug(#PB_World_DebugBody)
Repeat
  ExamineKeyboard()
  WindowEvent()
  If KeyboardReleased(#PB_Key_Space):way.b!1
    If IsEntity(1):FreeEntityBody(1):FreeEntity(1):EndIf
    If IsMesh(1):FreeMesh(1):EndIf
    CreateCone(1,0.2,1,16,8)
    If way
      UpdateMeshBoundingBox(1)
      CreateEntity(1,MeshID(1), MaterialID(1),0,0.5,0)
      RotateEntity(1,0,0,180)
    Else
      TransformMesh(1,0,0,0,1,1,1,0,0,90):UpdateMeshBoundingBox(1)
      UpdateMeshBoundingBox(1)
      CreateEntity(1,MeshID(1),MaterialID(1),0,0.5,0)
    EndIf
    CreateEntityBody(1,#PB_Entity_ConeBody,1,0.4,0.2):SetEntityAttribute(1,#PB_Entity_LinearSleeping,0.01):SetEntityAttribute(1,#PB_Entity_AngularSleeping,2.2)
    
  EndIf
  If KeyboardReleased(#PB_Key_Up)
    ApplyEntityImpulse(1, 0, 0, -0.5) 
  ElseIf KeyboardReleased(#PB_Key_Down)
    ApplyEntityImpulse(1, 0, 0, 0.5) 
  EndIf
  
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)