[PB 5.20] How apply physic on AttachEntityObject ?
Posted: Mon Aug 19, 2013 11:49 am
[PB 5.20]
If you incline the ground, the ball through the wall. How can I do apply EntityPhysicBody() on AttachEntityObject ? (F2 Restart)
If you incline the ground, the ball through the wall. How can I do apply EntityPhysicBody() on AttachEntityObject ? (F2 Restart)
Code: Select all
EnableExplicit
Define.l Event
Define.f X, Y, Z
Global Window.i, Texture.i, Mesh.i, Material.i, Entity.i
Global Camera.i, Ground.i, Sphere.i
Global Wall1.i, Wall2.i, Wall3.i, Wall4.i
Global Node.i, Cube0, Cube1, Cube2, Cube3, Cube4
Global EntityMove.i
InitEngine3D()
InitKeyboard()
InitSprite()
ExamineDesktops()
Window = OpenWindow(#PB_Any, 0, 0, DesktopWidth(0), DesktopHeight(0), "")
OpenWindowedScreen(WindowID(Window), 0, 0,DesktopWidth(0), DesktopHeight(0))
;
; Physics
EnableWorldPhysics(#True)
;
;Light and shadow
AmbientColor(RGB(127, 127, 127))
CreateLight(#PB_Any,RGB(151, 251, 151), -10, 100, 20)
WorldShadows(#PB_Shadow_Additive)
;
; Camera
Camera = CreateCamera(#PB_Any,0,0,100,100)
CameraBackColor(Camera, RGB(173, 216, 230))
;
; Texture (Ground, Obstacle, Sphere)
Texture = CreateTexture(#PB_Any,512,512)
StartDrawing(TextureOutput(Texture))
Box(0,0,512,512,RGB(0, 0, 0))
Box(1,1,510,510,RGB(255, 216, 0))
StopDrawing()
;
; Material (Ground, Obstacle, Sphere)
Material = CreateMaterial(#PB_Any,TextureID(texture))
MaterialFilteringMode(Material, #PB_Material_Anisotropic, 6)
;
; Ground
Mesh = CreatePlane(#PB_Any, 100, 100, 10, 10, 10, 10)
Ground = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material))
EntityPhysicBody(Ground, #PB_Entity_StaticBody, 1, 0.5, 1)
;
; Obstacles
Mesh = CreateCube(#PB_Any, 1)
;Wall East
Wall1 = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 50, 2, 0)
ScaleEntity(Wall1, 1, 4, 100)
EntityPhysicBody(Wall1, #PB_Entity_StaticBody, 1, 1, 1)
AttachEntityObject(Ground, "", EntityID(Wall1))
;Wall West
Wall2 = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), -50, 2, 0)
ScaleEntity(Wall2, 1, 4, 100)
EntityPhysicBody(Wall2, #PB_Entity_StaticBody, 1, 1, 1)
AttachEntityObject(Ground, "", EntityID(Wall2))
;Wall South
Wall3 = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 0, 2, 50)
ScaleEntity(Wall3, 100, 4, 1)
EntityPhysicBody(Wall3, #PB_Entity_StaticBody, 1, 1, 1)
AttachEntityObject(Ground, "", EntityID(Wall3))
;Wall North
Wall4 = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 0, 2, -50)
ScaleEntity(Wall4, 100, 4, 1)
EntityPhysicBody(Wall4, #PB_Entity_StaticBody, 1, 1, 1)
AttachEntityObject(Ground, "", EntityID(Wall4))
;
; Création d'un node sur lequel seront attaché les entités.
; Le cube 0 sera au meme emplacement que le node.
Mesh = CreateCube(#PB_Any, 2)
Node = CreateNode(#PB_Any, 0, 0, 0)
Cube0 = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 0, 2, 0)
AttachNodeObject(Node, EntityID(Cube0))
Cube1 = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 4, 2, 0)
AttachNodeObject(Node, EntityID(Cube1))
Cube2 = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), -4, 2, 0)
AttachNodeObject(Node, EntityID(Cube2))
Cube3 = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 0, 2, 4)
AttachNodeObject(Node, EntityID(Cube3))
Cube4 = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 0, 2, -4)
AttachNodeObject(Node, EntityID(Cube4))
MoveNode(Node, 20, 0, 0, #PB_Absolute)
AttachEntityObject(Ground, "", NodeID(Node))
;
; Sphere
Mesh = CreateSphere(#PB_Any, 1.5)
Sphere = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 0, 2, 0)
EntityPhysicBody(Sphere, #PB_Entity_SphereBody, 1, 1, 9)
While #True
Event = WindowEvent()
If ExamineKeyboard()
EntityMove=#False
If Event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
Break
EndIf
If KeyboardPushed(#PB_Key_Left)
Z=0.3 : EntityMove=#True
ElseIf KeyboardPushed(#PB_Key_Right)
Z=-0.3 : EntityMove=#True
EndIf
If KeyboardPushed(#PB_Key_Up)
X=-0.3 : EntityMove=#True
ElseIf KeyboardPushed(#PB_Key_Down)
X=0.3 : EntityMove=#True
EndIf
If KeyboardPushed(#PB_Key_F2)
RotateEntity(Ground, 0, 0, 0, #PB_Absolute)
MoveEntity(Sphere, 0, 1, 0, #PB_Absolute)
EndIf
EndIf
If EntityMove = #True
ApplyEntityImpulse(Sphere, 0, 0.01, 0) ;(BugWare when the entity is fixed)
RotateEntity(Ground, X, 0, Z, #PB_Relative)
EndIf
RotateNode(Node, 0, 1, 0, #PB_Relative)
; Rotation de chacun des cubes
; Les deux premiers cubes étaient placés sur l'axe X
; Les deux suivant sur l'axe Z
RotateEntity(cube1, -0.5, 0, 0, #PB_Relative)
RotateEntity(cube2, 0.5, 0, 0, #PB_Relative)
RotateEntity(cube3, 0, 0, -0.5, #PB_Relative)
RotateEntity(cube4, 0, 0, 0.5, #PB_Relative)
MoveCamera(Camera, 0, 40 , 110, #PB_Absolute)
CameraLookAt(camera, 0, 0, 0)
; RenderWorld
RenderWorld(40)
FlipBuffers()
Wend