Problem with TransformMesh using Physics

Everything related to 3D programming
User avatar
Psychophanta
Addict
Addict
Posts: 4996
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Problem with TransformMesh using Physics

Post 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.
Last edited by Psychophanta on Sun May 03, 2020 3:36 pm, edited 2 times in total.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Problem with TransformMesh using Physics

Post by DK_PETER »

Nice find.
The UpdateMeshBoundingBox() should recalculate the boundingBox to the fit the shape of the object.
It doesn't....
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Psychophanta
Addict
Addict
Posts: 4996
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Problem with TransformMesh using Physics

Post 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 !
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
T4r4ntul4
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Mar 04, 2014 4:15 pm
Location: Netherlands

Re: Problem with TransformMesh using Physics

Post 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)
User avatar
Psychophanta
Addict
Addict
Posts: 4996
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Problem with TransformMesh using Physics

Post 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.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
wayne gray
New User
New User
Posts: 6
Joined: Thu Jun 04, 2020 8:40 pm

Re: Problem with TransformMesh using Physics

Post 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)
wayne gray
New User
New User
Posts: 6
Joined: Thu Jun 04, 2020 8:40 pm

Re: Problem with TransformMesh using Physics

Post 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)
wayne gray
New User
New User
Posts: 6
Joined: Thu Jun 04, 2020 8:40 pm

Re: Problem with TransformMesh using Physics

Post 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)
wayne gray
New User
New User
Posts: 6
Joined: Thu Jun 04, 2020 8:40 pm

Re: Problem with TransformMesh using Physics

Post 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.
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Problem with TransformMesh using Physics

Post 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)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Post Reply