So my experiment is to create a very simple simulation of a 5 axis CNC machine.
Works great, however i have a problem with entity collision. I simply want to check if the tool, the toolholder (named head in the code) or the pinole collide with the workpiece and highlight the parts that collide in red.
Using EntityCollide to test the pinole against the workpiece works.
Testing the tool or toolholder against the workpiece fails, it allways returns no collision. Is that because tool and toolholder belong to a different node? And if so how do i test them for collision?
Here is the code:
Consider it as a learning project, nothing serious.
You need to change the path in the second line to the examples folder of purebasic, as it uses the GUI stuff from the examples.
Use mouse and arrow keys to move camera.
Use keys X,Y,Z,A,C to choose a axis to move.
Use keys + and - to move the chosen axis.
The workpiece is the small cube inside the machine.
Code: Select all
EnableExplicit
Global GUIPath.s = "Data\GUI"
Enumeration
#Mesh_Staender1
#Mesh_Staender2
#Mesh_Querbalken
#Mesh_Pynole
#Mesh_Workpiece
#Mesh_Head
#Mesh_Tool
EndEnumeration
Enumeration
#Entity_Staender1
#Entity_Staender2
#Entity_Querbalken
#Entity_Pynole
#Entity_Workpiece
#Entity_Head
#Entity_Tool
EndEnumeration
Enumeration
#Node_Machine
#Node_Head
EndEnumeration
Enumeration
#AxisX
#AxisY
#AxisZ
#AxisA
#AxisC
EndEnumeration
#CameraSpeed = 0.5
#AxisSpeed = 0.5
Enumeration
#AxisX
#AxisY
#AxisZ
#AxisA
#AxisC
#AxisText
#WireFrameText
#TransparentText
#ShadowText
EndEnumeration
Global KeyX.f
Global KeyY.f
Global MouseX.f
Global MouseY.f
Global Quit.i
Global WireFrameActive.i
Global TransparencyActive.i
Global ShadowsActive.i = #True
Global CollisionHappened.i
Global ZeroPosX.f = -4.0
Global ZeroPosY.f = 4.5
Global ZeroPosZ.f = 1.5
Global EndPosX.f = 4.5
Global EndPosY.f = 0.5
Global EndPosZ.f = 0.5
Global AxisPosX.f = ZeroPosX
Global AxisPosY.f = ZeroPosY
Global AxisPosZ.f = ZeroPosZ
Global AxisPosA.f = 0
Global AxisPosC.f = 0
Global ActiveAxis.i
Procedure WndEventHandler()
Protected Event.i
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
Quit = #True
EndSelect
Until Event = #False
EndProcedure
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
Add3DArchive(GUIPath, #PB_3DArchive_FileSystem)
AntialiasingMode(#PB_AntialiasingMode_x4)
OpenWindow(0, 0, 0, 800, 600, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
KeyboardMode(#PB_Keyboard_International)
WorldShadows(#PB_Shadow_Additive)
CreateTexture(0, 32, 32)
StartDrawing(TextureOutput(0))
Box(0, 0, 32, 32, #White)
StopDrawing()
CreateMaterial(0, TextureID(0))
SetMaterialColor(0, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 128))
CreateTexture(1, 32, 32)
StartDrawing(TextureOutput(1))
Box(0, 0, 32, 32, #Red)
StopDrawing()
CreateMaterial(1, TextureID(1))
CreateCube(#Mesh_Staender1, 1.0)
CreateCube(#Mesh_Staender2, 1.0)
CreateCube(#Mesh_Querbalken, 1.0)
CreateCube(#Mesh_Pynole, 1.0)
CreateCube(#Mesh_Workpiece, 0.5)
CreateSphere(#Mesh_Head, 0.25)
CreateCylinder(#Mesh_Tool, 0.05, 0.2)
CreateEntity(#Entity_Staender1, MeshID(#Mesh_Staender1), MaterialID(0), 0, 0, 0)
CreateEntity(#Entity_Staender2, MeshID(#Mesh_Staender2), MaterialID(0), 5, 0, 0)
CreateEntity(#Entity_Querbalken, MeshID(#Mesh_Querbalken), MaterialID(0), 2.5, 1, -4)
CreateEntity(#Entity_Pynole, MeshID(#Mesh_Pynole), MaterialID(0), 4.5, 1.5, -4.5)
CreateEntity(#Entity_Workpiece, MeshID(#Mesh_Workpiece), MaterialID(0), 2, -0.5, -3)
CreateEntity(#Entity_Head, MeshID(#Mesh_Head), MaterialID(0), 0, 0, 0)
CreateEntity(#Entity_Tool, MeshID(#Mesh_Tool), MaterialID(0), 0, -0.3, 0)
ScaleEntity(#Entity_Staender1, 0.5, 2, 10, #PB_Relative)
ScaleEntity(#Entity_Staender2, 0.5, 2, 10, #PB_Relative)
ScaleEntity(#Mesh_Querbalken, 5, 1, 1, #PB_Relative)
ScaleEntity(#Entity_Pynole, 0.5, 2, 0.5, #PB_Relative)
CreateNode(#Node_Head, 0, 0, 0)
AttachNodeObject(#Node_Head, EntityID(#Entity_Head))
AttachNodeObject(#Node_Head, EntityID(#Entity_Tool))
CreateNode(#Node_Machine, 0, 0, 0)
AttachNodeObject(#Node_Machine, EntityID(#Entity_Staender1))
AttachNodeObject(#Node_Machine, EntityID(#Entity_Staender2))
AttachNodeObject(#Node_Machine, EntityID(#Entity_Querbalken))
AttachNodeObject(#Node_Machine, EntityID(#Entity_Pynole))
AttachNodeObject(#Node_Machine, EntityID(#Entity_Workpiece))
AttachNodeObject(#Node_Machine, NodeID(#Node_Head))
EntityPhysicBody(#Entity_Staender1, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Staender2, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Querbalken, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Pynole, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Workpiece, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Head, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Tool, #PB_Entity_StaticBody)
EntityRenderMode(#Entity_Staender1, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Staender2, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Querbalken, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Pynole, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Workpiece, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Head, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Tool, #PB_Entity_CastShadow)
MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 10, 1, -10, #PB_Absolute)
CameraLookAt(0, 2, 0, 0)
CameraBackColor(0, RGB(19, 34, 49))
CreateLight(0, $FFFFFF, 1560, 900, -500)
AmbientColor($606060)
OpenWindow3D(0, 10, 10, 200, 215, "Info")
TextGadget3D(#AxisX, 5, 5, 190, 15, "")
TextGadget3D(#AxisY, 5, 25, 190, 15, "")
TextGadget3D(#AxisZ, 5, 45, 190, 15, "")
TextGadget3D(#AxisA, 5, 65, 190, 15, "")
TextGadget3D(#AxisC, 5, 85, 190, 15, "")
TextGadget3D(#AxisText, 5, 105, 190, 15, "")
TextGadget3D(#WireFrameText, 5, 125, 190, 15, "")
TextGadget3D(#TransparentText, 5, 145, 190, 15, "")
TextGadget3D(#ShadowText, 5, 165, 190, 15, "")
ShowGUI(128, #False)
;WorldDebug(#PB_World_DebugBody)
Repeat
WndEventHandler()
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
If KeyboardReleased(#PB_Key_W)
If WireFrameActive = #False
MaterialShadingMode(0, #PB_Material_Gouraud | #PB_Material_Wireframe)
WireFrameActive = #True
Else
MaterialShadingMode(0, #PB_Material_Gouraud | #PB_Material_Solid)
WireFrameActive = #False
EndIf
EndIf
If KeyboardReleased(#PB_Key_T)
If TransparencyActive = #False
MaterialBlendingMode(0, #PB_Material_Color)
TransparencyActive = #True
Else
MaterialBlendingMode(0, #False)
TransparencyActive = #False
EndIf
EndIf
If KeyboardReleased(#PB_Key_S)
If ShadowsActive = #False
WorldShadows(#PB_Shadow_Additive)
ShadowsActive = #True
Else
WorldShadows(#PB_Shadow_None)
ShadowsActive = #False
EndIf
EndIf
If KeyboardReleased(#PB_Key_X)
ActiveAxis = #AxisX
EndIf
If KeyboardReleased(#PB_Key_Y)
ActiveAxis = #AxisY
EndIf
If KeyboardReleased(#PB_Key_Z)
ActiveAxis = #AxisZ
EndIf
If KeyboardReleased(#PB_Key_A)
ActiveAxis = #AxisA
EndIf
If KeyboardReleased(#PB_Key_C)
ActiveAxis = #AxisC
EndIf
If KeyboardPushed(#PB_Key_Add)
Select ActiveAxis
Case #AxisX
AxisPosX = AxisPosX + (#AxisSpeed * 0.05)
If AxisPosX > EndPosX
AxisPosX = EndPosX
EndIf
MoveEntity(#Entity_Querbalken, 2.5, 1, AxisPosX, #PB_Absolute)
MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
Case #AxisY
AxisPosY = AxisPosY - (#AxisSpeed * 0.05)
If AxisPosY < EndPosY
AxisPosY = EndPosY
EndIf
MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
Case #AxisZ
AxisPosZ = AxisPosZ + (#AxisSpeed * 0.05)
If AxisPosZ > ZeroPosZ
AxisPosZ = ZeroPosZ
EndIf
MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
Case #AxisA
AxisPosA = AxisPosA + #AxisSpeed
If AxisPosA > 90
AxisPosA = 90
EndIf
RotateNode(#Node_Head, 0, AxisPosC, AxisPosA, #PB_Absolute)
Case #AxisC
AxisPosC = AxisPosC + #AxisSpeed
If AxisPosC > 180
AxisPosC = 180
EndIf
RotateNode(#Node_Head, 0, AxisPosC, AxisPosA, #PB_Absolute)
EndSelect
EndIf
If KeyboardPushed(#PB_Key_Subtract)
Select ActiveAxis
Case #AxisX
AxisPosX = AxisPosX - (#AxisSpeed * 0.05)
If AxisPosX < ZeroPosX
AxisPosX = ZeroPosX
EndIf
MoveEntity(#Entity_Querbalken, 2.5, 1, AxisPosX, #PB_Absolute)
MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
Case #AxisY
AxisPosY = AxisPosY + (#AxisSpeed * 0.05)
If AxisPosY > ZeroPosY
AxisPosY = ZeroPosY
EndIf
MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
Case #AxisZ
AxisPosZ = AxisPosZ - (#AxisSpeed * 0.05)
If AxisPosZ < EndPosZ
AxisPosZ = EndPosZ
EndIf
MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
Case #AxisA
AxisPosA = AxisPosA - #AxisSpeed
If AxisPosA < -90
AxisPosA = -90
EndIf
RotateNode(#Node_Head, 0, AxisPosC, AxisPosA, #PB_Absolute)
Case #AxisC
AxisPosC = AxisPosC - #AxisSpeed
If AxisPosC < 0
AxisPosC = 0
EndIf
RotateNode(#Node_Head, 0, AxisPosC, AxisPosA, #PB_Absolute)
EndSelect
EndIf
EndIf
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(0, KeyX, 0, KeyY)
CollisionHappened = #False
If EntityCollide(#Entity_Tool, #Entity_Workpiece) = #False
SetEntityMaterial(#Entity_Tool, MaterialID(0))
Else
CollisionHappened = #True
SetEntityMaterial(#Entity_Tool, MaterialID(1))
EndIf
If EntityCollide(#Entity_Head, #Entity_Workpiece) = #False
SetEntityMaterial(#Entity_Head, MaterialID(0))
Else
CollisionHappened = #True
SetEntityMaterial(#Entity_Head, MaterialID(1))
EndIf
If EntityCollide(#Entity_Pynole, #Entity_Workpiece) = #False
SetEntityMaterial(#Entity_Pynole, MaterialID(0))
Else
CollisionHappened = #True
SetEntityMaterial(#Entity_Pynole, MaterialID(1))
EndIf
If CollisionHappened = #True
SetEntityMaterial(#Entity_Workpiece, MaterialID(1))
Else
SetEntityMaterial(#Entity_Workpiece, MaterialID(0))
EndIf
SetGadgetText3D(#AxisX, "X-Axis: " + StrF((AxisPosX - ZeroPosX) * 1000, 3))
SetGadgetText3D(#AxisY, "Y-Axis: " + StrF(-1*((AxisPosY - ZeroPosY) * 1000), 3))
SetGadgetText3D(#AxisZ, "Z-Axis: " + StrF((AxisPosZ - ZeroPosZ) * 1000, 3))
SetGadgetText3D(#AxisA, "A-Axis: " + StrF(AxisPosA, 3))
SetGadgetText3D(#AxisC, "C-Axis: " + StrF(AxisPosC, 3))
Select ActiveAxis
Case #AxisX
SetGadgetText3D(#AxisText, "Chosen Axis: X")
Case #AxisY
SetGadgetText3D(#AxisText, "Chosen Axis: Y")
Case #AxisZ
SetGadgetText3D(#AxisText, "Chosen Axis: Z")
Case #AxisA
SetGadgetText3D(#AxisText, "Chosen Axis: A")
Case #AxisC
SetGadgetText3D(#AxisText, "Chosen Axis: C")
EndSelect
If WireFrameActive = #True
SetGadgetText3D(#WireFrameText, "Wireframe (W): yes")
Else
SetGadgetText3D(#WireFrameText, "Wireframe (W): no")
EndIf
If TransparencyActive = #True
SetGadgetText3D(#TransparentText, "Transparency (T): yes")
Else
SetGadgetText3D(#TransparentText, "Transparency (T): no")
EndIf
If ShadowsActive = #True
SetGadgetText3D(#ShadowText, "Shadows (S): yes")
Else
SetGadgetText3D(#ShadowText, "Shadows (S): no")
EndIf
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = #True
End

