Catapult demo with a motor
Posted: Wed Sep 17, 2014 5:13 pm
when i have posted the inclined plane example
http://www.purebasic.fr/english/viewtop ... 36&t=54556 i was not aware to the function HingeJointMotorTarget http://purebasic.com/documentation/join ... arget.html
now i know about it from this recent thread http://purebasic.fr/english/viewtopic.php?f=36&t=60530 fortunately Realizimo have used several mechanics functions, also fortunately DK_PETER have posted a bug report here http://purebasic.fr/english/viewtopic.p ... tityobject to repair the AttachEntityObject() - in his words: so that to be able to attach an particle emitter to an entity without bones. the good new is that this is fixed in PB 5.31 beta 1.
naturally with all these goodies i will think of the Catapult which throw stones with fire like in the old wars.
in normal mode when the impulse (for the motor) = 20 it will throw stones nearby. but in super power mode when Impulse = 200 it will throw the stones to another remote land


keys to use:
mouse and arrows: to travel over the scene
Space key: to throw the stones.
V key : to recharge the weapon with another stone ( i have used the same stone)
P key: toggle power either 20 or 200
X/Z to move the Catapult right/Left
R to rotate the Catapult
Updated to PB 5.42
PS: i forgot to say that look here http://purebasic.fr/english/viewtopic.p ... 30#p453018 to see how this hinge joint are made
PS2: the flame i have used from the book PureBasic - A Beginner's Guide look here http://purebasic.fr/english/viewtopic.php?f=36&t=60479 but with using lens_flare5.jpg instead of flame.png since i want the code to be self contained.
http://www.purebasic.fr/english/viewtop ... 36&t=54556 i was not aware to the function HingeJointMotorTarget http://purebasic.com/documentation/join ... arget.html
now i know about it from this recent thread http://purebasic.fr/english/viewtopic.php?f=36&t=60530 fortunately Realizimo have used several mechanics functions, also fortunately DK_PETER have posted a bug report here http://purebasic.fr/english/viewtopic.p ... tityobject to repair the AttachEntityObject() - in his words: so that to be able to attach an particle emitter to an entity without bones. the good new is that this is fixed in PB 5.31 beta 1.
naturally with all these goodies i will think of the Catapult which throw stones with fire like in the old wars.
in normal mode when the impulse (for the motor) = 20 it will throw stones nearby. but in super power mode when Impulse = 200 it will throw the stones to another remote land


keys to use:
mouse and arrows: to travel over the scene
Space key: to throw the stones.
V key : to recharge the weapon with another stone ( i have used the same stone)
P key: toggle power either 20 or 200
X/Z to move the Catapult right/Left
R to rotate the Catapult
Updated to PB 5.42
Code: Select all
Enumeration
#TEXTURE = 200
#MATERIAL
#ENTITY
#CAMERA
#PARTICLE
#ground
#remoteLand
#Catapult
#arm
#stone
EndEnumeration
#CameraSpeed = 1
Global power = 1
Define.f KeyX, KeyY, MouseX, MouseY
InitEngine3D()
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/OPE/textures", #PB_3DArchive_FileSystem)
Add3DArchive("Data\", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0, 0, 0, DesktopW, DesktopH, "Space to throw the stone, X/Z move right/left, . R rotate, .. P toggle super power projectile,.. V recharge ")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
GetScriptMaterial(2, "Color/Red")
CreateMaterial(6, LoadTexture(2, "soil_wall.jpg"))
CreateMaterial(3, LoadTexture(3, "terrain_detail.jpg"))
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera,0,50,150, #PB_Absolute)
CameraLookAt(#Camera,0,0,0)
CreateLight(0, $FFFFFF, 1500, 800, 500)
AmbientColor(RGB(200,200,200))
SkyBox("desert07.jpg")
CreateCube(1, 1)
CreateEntity(#ground, MeshID(1), MaterialID(6), 0, -4, 0)
ScaleEntity( #ground, 300, 0.5, 300)
CreateEntity(#remoteLand, MeshID(1), MaterialID(3),700,-50,-100)
ScaleEntity( #remoteLand,1000, 10, 1000)
CreateEntity(#Catapult , MeshID(1), MaterialID(2))
ScaleEntity( #Catapult , 20 , 6 , 20 ) ;Catapults
CreateEntity(#arm, MeshID(1), MaterialID(6),5,7,10)
ScaleEntity( #arm,10,2,10)
CreateEntity(#stone, MeshID(1), MaterialID(2),2,10,1)
ScaleEntity( #stone,3,2,3)
CreateEntityBody(#ground, #PB_Entity_StaticBody,0,0,4)
CreateEntityBody(#remoteLand, #PB_Entity_StaticBody,0,0,4)
CreateEntityBody(#Catapult,#PB_Entity_BoxBody ,20, 1, 1)
CreateEntityBody(#arm, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(#stone, #PB_Entity_BoxBody,1, 0.1, 1)
;CreateEntityBody(#Entity, Type [, Mass [, Restitution, Friction]])
HingeJoint(1,EntityID(#Catapult),
10, 3, 0, ;position of the joint from the center of the #Catapult
0, 0, 1,
EntityID(#arm),
5, -1, 0, ;position of the joint from the center of the #arm
0, 0, 1)
SetEntityAttribute(#Catapult, #PB_Entity_LinearSleeping, 0)
SetEntityAttribute(#arm, #PB_Entity_LinearSleeping, 0)
SetEntityAttribute(#stone, #PB_Entity_LinearSleeping, 0)
LoadTexture(#TEXTURE, "lensflare5.jpg")
CreateMaterial(#MATERIAL, TextureID(#TEXTURE))
DisableMaterialLighting(#MATERIAL, 1)
MaterialBlendingMode(#MATERIAL, #PB_Material_Add)
CreateParticleEmitter(#PARTICLE, 2, 2, 0,#PB_Particle_Point,0,0,0)
ParticleSize(#PARTICLE, 5, 5)
ParticleMaterial(#PARTICLE, MaterialID(#MATERIAL))
ParticleEmissionRate(#PARTICLE, 50)
ParticleTimeToLive(#PARTICLE, 0.25, 0.25)
ParticleColorRange(#PARTICLE, RGB(255, 0, 0), RGB(255, 200, 0))
ParticleVelocity(#PARTICLE, 1, 2)
AttachEntityObject(#stone, "", ParticleEmitterID(#PARTICLE))
Impulse.f = 20
Repeat
Repeat
event = WindowEvent()
Until event = 0
If ExamineMouse()
MouseX = -MouseDeltaX()/20
MouseY = -MouseDeltaY()/20
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
EndIf
If KeyboardPushed(#PB_Key_X)
MoveEntity(#Catapult, 3,0,0)
ElseIf KeyboardPushed(#PB_Key_Z)
MoveEntity(#Catapult, -3,0,0)
ElseIf KeyboardPushed(#PB_Key_R)
RotateEntity(#Catapult, 0, -0.1, 0,#PB_Relative)
EndIf
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
EnableHingeJointAngularMotor(1, #True, 2, 5)
DisableEntityBody(#arm, 0)
HingeJointMotorTarget(1, 25, 1)
If KeyboardPushed(#PB_Key_Space) ; to lift the small box
;ApplyEntityImpulse(#arm, 0, 4, 0, 6,0,0) ; apply force up
;EnableHingeJointAngularMotor(#Joint, Enable, TargetVelocity, MaxMotorImpulse)
EnableHingeJointAngularMotor(1, #True, 3, impulse)
DisableEntityBody(#arm, 0)
HingeJointMotorTarget(1, 45, 0.02)
;HingeJointMotorTarget(#Joint, Angle, Velocity)
EndIf
If KeyboardPushed(#PB_Key_V)
DisableEntityBody(#stone, #True)
DisableParticleEmitter(#PARTICLE, #True)
MoveEntity(#stone, EntityX(#Catapult)+2,10,1, #PB_Absolute)
MoveParticleEmitter(#Particle, 0,0,0,#PB_Absolute)
DisableEntityBody(#stone, #False)
DisableParticleEmitter(#PARTICLE, #False)
EndIf
If KeyboardReleased(#PB_Key_P)
If power
Impulse = 200
power ! 1
Else
Impulse = 20
Power ! 1
EndIf
EndIf
RenderWorld()
FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
PS2: the flame i have used from the book PureBasic - A Beginner's Guide look here http://purebasic.fr/english/viewtopic.php?f=36&t=60479 but with using lens_flare5.jpg instead of flame.png since i want the code to be self contained.


