Page 1 of 1

Catapult demo with a motor

Posted: Wed Sep 17, 2014 5:13 pm
by applePi
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
Image
Image

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) 
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.

Re: Catapult demo with a motor

Posted: Thu Sep 18, 2014 9:03 am
by Bananenfreak
IMA at line 104
AttachEntityObject(#stone, "", ParticleEmitterID(#PARTICLE))

Re: Catapult demo with a motor

Posted: Thu Sep 18, 2014 9:35 am
by applePi
Hi Bananenfreak
the example works only in the new PB v5.31 beta 1 and above, since the attachment of the particle emitter to an entity without bones are just made recently. it allows attaching flame to entities the easy way.

Re: Catapult demo with a motor

Posted: Thu Sep 18, 2014 10:44 am
by DK_PETER
@applePi
VERY nice!! I can almost feel the urge to create a 'siege the castle' game. ;-)

Re: Catapult demo with a motor

Posted: Sat Sep 27, 2014 4:23 pm
by applePi
version 2 of the Catapult, at first i want to note that there is a great car demo moving on a terrain in purebasic v5.31 look here http://vimeo.com/106630420 . download the mp4 file.

the catapult will throw a stone on a flat terrain and make holes in it, rotate it by e/r then charge with C then fire with Space, you can make a trench in the ground like this:
Image

experimentally i know the EntityY(#stone) on the ground = -8.9 so i check when it is below -8.1 and then calling the hole making procedure. i don't know how to check the collision between the stone and the terrain ???. also when we Debug TerrainHeight(0, 32, 80) for this flat terrain we get 600. while the EntityY(stone) on the ground are -8.9 , how is this ????

just put the following picture flame.png in the same folder as the code
Image

Updated to PB 5.42

Code: Select all

Declare DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, height.f)

Enumeration
   #TEXTURE = 200
   #MATERIAL
   #ENTITY
   #CAMERA
   #PARTICLE
   #Catapult
  #arm
  #stone
  #limiter
  #limiter2
  
EndEnumeration

#CameraSpeed = 7
Global power = 1
Define.f KeyX, KeyY, MouseX, MouseY
  

InitEngine3D()

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/"       , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"         , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"           , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
    
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/E rotate Catapult, .. P toggle super power projectile,.. C recharge ")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)

GetScriptMaterial(2, "Color/Red")
CreateMaterial(6, LoadTexture(2, "soil_wall.jpg"))
GetScriptMaterial(4, "Color/Green")
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(#Catapult , MeshID(1), MaterialID(2))
ScaleEntity( #Catapult , 20 , 6 , 20 ) ;Catapults
CreateEntity(#arm, MeshID(1), MaterialID(6),5,7,10)
ScaleEntity( #arm,20,2,20)
CreateEntity(#stone, MeshID(1), MaterialID(2),0,10,1)
ScaleEntity( #stone,3,2,3)
CreateEntity(#limiter, MeshID(1), MaterialID(4),5,8,10)
ScaleEntity( #limiter,2,2,20)
CreateEntity(#limiter2, MeshID(1), MaterialID(4),-5,8,10)
ScaleEntity( #limiter2,2,2,20)


CreateEntityBody(#Catapult,#PB_Entity_BoxBody  ,60, 0.1, 1)
CreateEntityBody(#arm, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(#limiter, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(#limiter2, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(#stone, #PB_Entity_BoxBody,2, 0.1, 0.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)
;PointJoint(#Joint, EntityID, PivotX, PivotY, PivotZ [, EntityID2, PivotX2, PivotY2, PivotZ2])
PointJoint(2, EntityID(#arm), -3,1,2, EntityID(#limiter), 0,-1,2)
PointJoint(3, EntityID(#arm), -3,1,-2, EntityID(#limiter), 0,-1,-2)
PointJoint(4, EntityID(#arm), -9,1,2, EntityID(#limiter2), 0,-1,2)
PointJoint(5, EntityID(#arm), -9,1,-2, EntityID(#limiter2), 0,-1,-2)


SetEntityAttribute(#Catapult, #PB_Entity_LinearSleeping, 0)
SetEntityAttribute(#arm, #PB_Entity_LinearSleeping, 0)
SetEntityAttribute(#stone, #PB_Entity_LinearSleeping, 0)

LoadTexture(#TEXTURE, "flame.png")
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 = 100
   
SetupTerrains(LightID(0), 3000, #PB_Terrain_NormalMapping)
; initialize terrain 
CreateTerrain(0, 513, 12000, 600, 3, "TerrainHeight", "dat")
TerrainLocate(0, 400, -610, 700)
; set all texture will be use when terrrain will be constructed 
AddTerrainTexture(0,  0, 100, "dirt_grayrocky_diffusespecular.jpg",  "dirt_grayrocky_normalheight.jpg")
AddTerrainTexture(0,  1, 30, "grass_green-01_diffusespecular.jpg", "grass_green-01_normalheight.jpg")
AddTerrainTexture(0,  2, 200, "growth_weirdfungus-03_diffusespecular.jpg", "growth_weirdfungus-03_normalheight.jpg")
;- define terrains
DefineTerrainTile(0, 0, 0, "white.jpg", 0, 0)     
BuildTerrain(0)  
CreateTerrainBody(0, 2, 1) 
UpdateTerrain(0)
; SkyBox
SkyBox("desert07.jpg")

EnableWorldPhysics(1)
EnableWorldCollisions(1)

WorldShadows(#PB_Shadow_Modulative)

TerrainRenderMode(0, #PB_Terrain_LowLODShadows)

RotateEntity(#Catapult, 0, 45, 0,#PB_Relative)

Repeat
  Repeat
  event = WindowEvent()  
  Until event = 0
  If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
      If EntityY(#stone) < -8.1
      
        If flg = 0
          
          DoTerrainModify(0, 0, EntityX(#stone),EntityY(#stone),EntityZ(#stone), 0.00001, 550) 
          ParticleEmissionRate(#PARTICLE, 50)
          ParticleSize(#PARTICLE, 15, 15)
          ParticleTimeToLive(#PARTICLE, 0.95, 0.95)
          flg = 1
          EndIf
        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)  ; to move catapult right
        MoveEntity(#Catapult, 10,0,0)
        ApplyEntityImpulse(#Catapult, 10, -10, 0)

      ElseIf KeyboardPushed(#PB_Key_Z) ; to move left
        MoveEntity(#Catapult, -10,0,0)
        ApplyEntityImpulse(#Catapult, -10, -10, 0)
      ElseIf KeyboardPushed(#PB_Key_R)
        RotateEntity(#Catapult, 0, -0.3, 0,#PB_Relative)
      ElseIf KeyboardPushed(#PB_Key_E)
        RotateEntity(#Catapult, 0, 0.3, 0,#PB_Relative)
      EndIf
            
      DisableEntityBody(#Catapult, 0)
      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
       
       ;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_C) 
  flg = 0
  DisableEntityBody(#stone, #True)
  DisableParticleEmitter(#PARTICLE, #True)
  MoveEntity(#stone, EntityX(#Catapult)+0,EntityY(#Catapult)+10,EntityZ(#Catapult), #PB_Absolute)
  MoveParticleEmitter(#Particle, 0,0,0,#PB_Absolute)
  DisableEntityBody(#stone, #False)
  DisableParticleEmitter(#PARTICLE, #False)
  ParticleEmissionRate(#PARTICLE, 50)
  ParticleSize(#PARTICLE, 5, 5)
  ParticleTimeToLive(#PARTICLE, 0.25, 0.25)
EndIf 

If KeyboardReleased(#PB_Key_P) 
  
  If power
    Impulse = 200
    power ! 1
  Else
    Impulse = 100
    Power ! 1
  EndIf
  
EndIf

If KeyboardPushed(#PB_Key_M)
  Debug EntityX(#stone)
  Debug EntityY(#stone)
  Debug EntityZ(#stone)
  Debug TerrainHeight(0, 32, 80)

  Debug "---------------------"
EndIf
  
  RenderWorld()   
  FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)


; the following proc. is by Realizimo
Procedure DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, newheight.f)
  terrainSize.f = TerrainTileSize(0, tx, ty) - 1
  Pointx.f = TerrainTilePointX(0, tx, ty, wx, wy, wz) 
  Pointy.f = TerrainTilePointY(0, tx, ty, wx, wy, wz)   
  startx = (Pointx - mBrushSizeTerrainSpace) * terrainSize
  starty = (Pointy - mBrushSizeTerrainSpace) * terrainSize
  endx   = (Pointx + mBrushSizeTerrainSpace) * terrainSize
  endy   = (Pointy + mBrushSizeTerrainSpace) * terrainSize  
  

  For y = starty To endy
    For x = startx To endx  
      SetTerrainTileHeightAtPoint(0, tx, ty, x, y, newheight)      
    Next x
  Next y
  UpdateTerrain(0)
  CreateTerrainBody(0, 2, 1)
EndProcedure

Re: Catapult demo with a motor

Posted: Mon Sep 29, 2014 11:55 am
by Comtois
applePi wrote:experimentally i know the EntityY(#stone) on the ground = -8.9 so i check when it is below -8.1 and then calling the hole making procedure. i don't know how to check the collision between the stone and the terrain ???. also when we Debug TerrainHeight(0, 32, 80) for this flat terrain we get 600. while the EntityY(stone) on the ground are -8.9 , how is this ????
the ogre command says
Get the height data for a given world position


it seems that this command does not consider the position of the terrain

Code: Select all

TerrainLocate(0, 400, -610, 700)
add -610 at TerrainHeight(), may be this should be done in PB command.

Re: Catapult demo with a motor

Posted: Mon Sep 29, 2014 2:03 pm
by applePi
may be this should be done in PB command
thanks Comtois, yes a special PB command to detect a collision between a specific entity and the terrain will be more convenient. something like
Result = TerrainCollide(#Terrain, #Entity)
thats in parallel with the current command:
Result = EntityCollide(#Entity, #Entity2)

Re: Catapult demo with a motor

Posted: Mon Sep 29, 2014 7:31 pm
by Comtois
applePi wrote:
may be this should be done in PB command
thanks Comtois, yes a special PB command to detect a collision between a specific entity and the terrain will be more convenient. something like
Result = TerrainCollide(#Terrain, #Entity)
thats in parallel with the current command:
Result = EntityCollide(#Entity, #Entity2)
Well, i was thinking about that
add -610 at TerrainHeight()
But yes, it could be usefull to add 'Result = TerrainCollide(#Terrain, #Entity)'. I will give it a try.

While waiting, you can use ExamineCollision(), it should work with terrain for next beta.
http://www.purebasic.fr/english/viewtop ... =4&t=60597

Re: Catapult demo with a motor

Posted: Tue Sep 30, 2014 10:00 pm
by applePi
you can use ExamineCollision()
thanks Comtois , i have looked at the Bananenfreak code ,it seems the stone number returns from the FirstWorldCollisionEntity(), , so we then call the hole making at the stone place . and this is better than my first approach.
the procedure DoTerrainModify() needs more fine tunning.
i suggest to choose superpower (P key) and shoot the remote mountain in the terrain.
needs the flame.png picture above
updated to PB 5.42

Code: Select all

Global terrain, stone, flg
Declare DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, height.f)

Enumeration
   #TEXTURE = 200
   #MATERIAL
   #ENTITY
   #CAMERA
   #PARTICLE
   #Catapult
  #arm
  #stone
  #limiter
  #limiter2
  
EndEnumeration

#CameraSpeed = 7
Global power = 1, stone
Define.f KeyX, KeyY, MouseX, MouseY
  

InitEngine3D()

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/"       , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"         , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"           , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
    
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/E rotate Catapult, .. P toggle super power projectile,.. C recharge ")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)

GetScriptMaterial(2, "Color/Red")
CreateMaterial(6, LoadTexture(2, "soil_wall.jpg"))
GetScriptMaterial(4, "Color/Green")
CreateMaterial(3, LoadTexture(3, "terrain_detail.jpg"))


CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera,-60,100,200, #PB_Absolute)
CameraLookAt(#Camera,100,0,0)


CreateLight(0, $FFFFFF, 1500, 800, 500)
AmbientColor(RGB(200,200,200))

SkyBox("desert07.jpg")

CreateCube(1, 1)

CreateEntity(#Catapult , MeshID(1), MaterialID(2))
ScaleEntity( #Catapult , 20 , 6 , 20 ) ;Catapults
CreateEntity(#arm, MeshID(1), MaterialID(6),5,7,10)
ScaleEntity( #arm,20,2,20)
stone = CreateEntity(#PB_Any, MeshID(1), MaterialID(2),0,10,1)
;Debug stone
ScaleEntity( stone,3,2,3)
CreateEntity(#limiter, MeshID(1), MaterialID(4),5,8,10)
ScaleEntity( #limiter,2,2,20)
CreateEntity(#limiter2, MeshID(1), MaterialID(4),-5,8,10)
ScaleEntity( #limiter2,2,2,20)


CreateEntityBody(#Catapult,#PB_Entity_BoxBody  ,100, 0.1, 1)
CreateEntityBody(#arm, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(#limiter, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(#limiter2, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(stone, #PB_Entity_BoxBody,2, 0.1, 0.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)
;PointJoint(#Joint, EntityID, PivotX, PivotY, PivotZ [, EntityID2, PivotX2, PivotY2, PivotZ2])
PointJoint(2, EntityID(#arm), -3,1,2, EntityID(#limiter), 0,-1,2)
PointJoint(3, EntityID(#arm), -3,1,-2, EntityID(#limiter), 0,-1,-2)
PointJoint(4, EntityID(#arm), -9,1,2, EntityID(#limiter2), 0,-1,2)
PointJoint(5, EntityID(#arm), -9,1,-2, EntityID(#limiter2), 0,-1,-2)


SetEntityAttribute(#Catapult, #PB_Entity_LinearSleeping, 0)
SetEntityAttribute(#arm, #PB_Entity_LinearSleeping, 0)
SetEntityAttribute(stone, #PB_Entity_LinearSleeping, 0)

LoadTexture(#TEXTURE, "flame.png")
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 = 100
   
SetupTerrains(LightID(0), 3000, #PB_Terrain_NormalMapping)
; initialize terrain 
terrain = CreateTerrain(#PB_Any, 513, 12000, 600, 3, "TerrainHeight", "dat")

TerrainLocate(terrain, 100, -110, 700)
; set all texture will be use when terrrain will be constructed 
AddTerrainTexture(terrain,  0, 100, "dirt_grayrocky_diffusespecular.jpg",  "dirt_grayrocky_normalheight.jpg")
AddTerrainTexture(terrain,  1, 30, "grass_green-01_diffusespecular.jpg", "grass_green-01_normalheight.jpg")
AddTerrainTexture(terrain,  2, 200, "growth_weirdfungus-03_diffusespecular.jpg", "growth_weirdfungus-03_normalheight.jpg")
;- define terrains
DefineTerrainTile(terrain, 0, 0, "terrain.png", 0, 0)     
BuildTerrain(terrain)  
CreateTerrainBody(terrain, 2, 1) 
UpdateTerrain(terrain)
; SkyBox
SkyBox("desert07.jpg")

EnableWorldPhysics(1)
EnableWorldCollisions(1)

WorldShadows(#PB_Shadow_Modulative)

TerrainRenderMode(0, #PB_Terrain_LowLODShadows)

;RotateEntity(#Catapult, 0, 45, 0,#PB_Relative)

Repeat
   Repeat
  event = WindowEvent()  
  Until event = 0
  If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
        If flg = 0
          
          ExamineWorldCollisions(#True)
  
          While NextWorldCollision()
             first = FirstWorldCollisionEntity()
             sec = SecondWorldCollisionEntity()

             If first = stone
             flg = 1
             
             DoTerrainModify(0, 0, EntityX(stone),EntityY(stone),EntityZ(stone), 0.00001, 48) 
            
             EndIf
   
          Wend
          
          
        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)  ; to move catapult right
        MoveEntity(#Catapult, 10,0,0)
        ApplyEntityImpulse(#Catapult, 10, -10, 0)

      ElseIf KeyboardPushed(#PB_Key_Z) ; to move left
        MoveEntity(#Catapult, -10,0,0)
        ApplyEntityImpulse(#Catapult, -10, -10, 0)
      ElseIf KeyboardPushed(#PB_Key_R)
        RotateEntity(#Catapult, 0, -0.3, 0,#PB_Relative)
      ElseIf KeyboardPushed(#PB_Key_E)
        RotateEntity(#Catapult, 0, 0.3, 0,#PB_Relative)
      EndIf
            
      DisableEntityBody(#Catapult, 0)
      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
       
       ;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_C) 
  flg = 0
  DisableEntityBody(stone, #True)
  DisableParticleEmitter(#PARTICLE, #True)
  MoveEntity(stone, EntityX(#Catapult)+0,EntityY(#Catapult)+10,EntityZ(#Catapult), #PB_Absolute)
  MoveParticleEmitter(#Particle, 0,0,0,#PB_Absolute)
  DisableEntityBody(stone, #False)
  DisableParticleEmitter(#PARTICLE, #False)
  ParticleEmissionRate(#PARTICLE, 50)
  ParticleSize(#PARTICLE, 5, 5)
  ParticleTimeToLive(#PARTICLE, 0.25, 0.25)
EndIf 

If KeyboardReleased(#PB_Key_P) 
  
  If power
    Impulse = 700
    power ! 1
  Else
    Impulse = 100
    Power ! 1
  EndIf
  
EndIf

If KeyboardPushed(#PB_Key_M)
  Debug EntityX(stone)
  Debug EntityY(stone)
  Debug EntityZ(stone)
  Debug TerrainHeight(terrain, 32, 80)

  Debug "---------------------"
EndIf
  
  RenderWorld()   
  FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)

; the following proc. is by Realizimo
Procedure DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, newheight.f)
  terrainSize.f = TerrainTileSize(terrain, tx, ty) - 1
  Pointx.f = TerrainTilePointX(terrain, tx, ty, wx, wy, wz) 
  Pointy.f = TerrainTilePointY(terrain, tx, ty, wx, wy, wz)   
  startx = (Pointx - mBrushSizeTerrainSpace) * terrainSize
  starty = (Pointy - mBrushSizeTerrainSpace) * terrainSize
  endx   = (Pointx + mBrushSizeTerrainSpace) * terrainSize
  endy   = (Pointy + mBrushSizeTerrainSpace) * terrainSize  
  

  For y = starty To endy
    For x = startx To endx  
      SetTerrainTileHeightAtPoint(terrain, tx, ty, x, y, newheight)      
    Next x
  Next y
  UpdateTerrain(terrain)
  CreateTerrainBody(terrain, 2, 1)
EndProcedure



Re: Catapult demo with a motor

Posted: Wed Oct 01, 2014 7:10 am
by Bananenfreak
Nice, applePi! I like it :)

What about some physicsstuff? If WorldCollision() triggers, you´ll get the impulsevalues. You could use them for variable "terraindamage".

Code: Select all

WorldCollisionAppliedImpulse()
      imp\x = GetX()
      imp\y = GetY()
      imp\z = GetZ()
      imp_len = Pow(Pow(imp\x, 2) + Pow(imp\y, 2) + Pow(imp\z, 2), 1/3)
Now you only need the height of terrain at the specific Point and rest is your Thing ^^
Would be nice if you add this (Real physics :shock: :D )

Re: Catapult demo with a motor

Posted: Wed Oct 01, 2014 9:49 am
by applePi
thanks Bananenfreak, your suggestion about WorldCollisionAppliedImpulse() seems great and have many possible fun things. it is up to the user to imagine how to use it, and here i imagine that when the average Impulse are big enough the projectile will make a wider terrain damage. so try here to prepare the super power mode (P key) and shoot the remote mountain and it will destroy half the mountain like this picture:
Image

but not always we get positive impulse, i got sometimes 0 value , seems depends where the stone landed. and the squared hole because the procedure DoTerrainModify() needs more mathematical stuff to care about this, in fact i need to study the terrain more by beginning with a one Tile terrain.
edit:look my thread "terrain experiments" for how to make perfect circular holes.

note that the SecondWorldCollisionEntity() does not return the terrain number.
(P key to prepare super projectile then Space key to shoot)

PS: in one case only the Catapult have made a hole under itself, this is may be because the stone make a collision with the Catapult body !!. this is why i have suggested the function TerrainCollide(#Terrain, #Entity)
needs the flame.png picture above
Updated to PB 5.42
Edit2: corrected the formula for the addition of 3 perpendicular vectors in 3D space as Bananenfreak refer to below

Code: Select all

Global terrain, stone, flg
Declare DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, height.f)

Enumeration
   #TEXTURE = 200
   #MATERIAL
   #ENTITY
   #CAMERA
   #PARTICLE
   #Catapult
  #arm
  #stone
  #limiter
  #limiter2
  
EndEnumeration

Structure impulse
  x.f
  y.f
  z.f
EndStructure

#CameraSpeed = 7
Global power = 1, stone
Define.f KeyX, KeyY, MouseX, MouseY
  

InitEngine3D()

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/"       , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"         , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"           , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
    
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/E rotate Catapult, .. P toggle super power projectile,.. C recharge ")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)

GetScriptMaterial(2, "Color/Red")
CreateMaterial(6, LoadTexture(2, "soil_wall.jpg"))
GetScriptMaterial(4, "Color/Green")
CreateMaterial(3, LoadTexture(3, "terrain_detail.jpg"))


CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera,-60,100,200, #PB_Absolute)
CameraLookAt(#Camera,100,0,0)


CreateLight(0, $FFFFFF, 1500, 800, 500)
AmbientColor(RGB(200,200,200))

SkyBox("desert07.jpg")

CreateCube(1, 1)

CreateEntity(#Catapult , MeshID(1), MaterialID(2))
ScaleEntity( #Catapult , 20 , 6 , 20 ) ;Catapults
CreateEntity(#arm, MeshID(1), MaterialID(6),5,7,10)
ScaleEntity( #arm,20,2,20)
stone = CreateEntity(#PB_Any, MeshID(1), MaterialID(2),0,10,1)
;Debug stone
ScaleEntity( stone,3,2,3)
CreateEntity(#limiter, MeshID(1), MaterialID(4),5,8,10)
ScaleEntity( #limiter,2,2,20)
CreateEntity(#limiter2, MeshID(1), MaterialID(4),-5,8,10)
ScaleEntity( #limiter2,2,2,20)


CreateEntityBody(#Catapult,#PB_Entity_BoxBody  ,100, 0.1, 1)
CreateEntityBody(#arm, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(#limiter, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(#limiter2, #PB_Entity_BoxBody,1, 0.1, 1)
CreateEntityBody(stone, #PB_Entity_BoxBody,2, 0.1, 0.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)
;PointJoint(#Joint, EntityID, PivotX, PivotY, PivotZ [, EntityID2, PivotX2, PivotY2, PivotZ2])
PointJoint(2, EntityID(#arm), -3,1,2, EntityID(#limiter), 0,-1,2)
PointJoint(3, EntityID(#arm), -3,1,-2, EntityID(#limiter), 0,-1,-2)
PointJoint(4, EntityID(#arm), -9,1,2, EntityID(#limiter2), 0,-1,2)
PointJoint(5, EntityID(#arm), -9,1,-2, EntityID(#limiter2), 0,-1,-2)


SetEntityAttribute(#Catapult, #PB_Entity_LinearSleeping, 0)
SetEntityAttribute(#arm, #PB_Entity_LinearSleeping, 0)
SetEntityAttribute(stone, #PB_Entity_LinearSleeping, 0)

LoadTexture(#TEXTURE, "flame.png")
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 = 100
   
SetupTerrains(LightID(0), 3000, #PB_Terrain_NormalMapping)
; initialize terrain 
terrain = CreateTerrain(#PB_Any, 513, 12000, 600, 3, "TerrainHeight", "dat")
;Debug terrain
TerrainLocate(terrain, 100, -110, 700)
; set all texture will be use when terrrain will be constructed 
AddTerrainTexture(terrain,  0, 100, "dirt_grayrocky_diffusespecular.jpg",  "dirt_grayrocky_normalheight.jpg")
AddTerrainTexture(terrain,  1, 30, "grass_green-01_diffusespecular.jpg", "grass_green-01_normalheight.jpg")
AddTerrainTexture(terrain,  2, 200, "growth_weirdfungus-03_diffusespecular.jpg", "growth_weirdfungus-03_normalheight.jpg")
;- define terrains
DefineTerrainTile(terrain, 0, 0, "terrain.png", 0, 0)     
BuildTerrain(terrain)  
CreateTerrainBody(terrain, 2, 1) 
UpdateTerrain(terrain)
; SkyBox
SkyBox("desert07.jpg")

EnableWorldPhysics(1)
EnableWorldCollisions(1)

WorldShadows(#PB_Shadow_Modulative)

TerrainRenderMode(0, #PB_Terrain_LowLODShadows)

;RotateEntity(#Catapult, 0, 45, 0,#PB_Relative)

Repeat
  Repeat
  event = WindowEvent()  
  Until event = 0
  If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
        If flg = 0
          
          ExamineWorldCollisions(#True)
  
          While NextWorldCollision()
             first = FirstWorldCollisionEntity()
             sec = SecondWorldCollisionEntity()
             

             If first = stone
             flg = 1
             ;Debug sec
             WorldCollisionAppliedImpulse()
             ImpulseX.f = GetX()
             ImpulseY.f = GetY()
             ImpulseZ.f = GetZ()
             ;ImpulseXYZ.f = Pow(Pow(ImpulseX, 2) + Pow(ImpulseY, 2) + Pow(ImpulseZ, 2), 1/2)
             ;Or 
             ImpulseXYZ.f = Sqr(Pow(ImpulseX, 2) + Pow(ImpulseY, 2) + Pow(ImpulseZ, 2))
             Debug "ImpulseX = " +ImpulseX:Debug "ImpulseY =" +ImpulseY: Debug "ImpulseZ =" +ImpulseZ
             Debug "ImpulseXYZ =" + ImpulseXYZ
             If ImpulseXYZ > 120
               brushSize.f = 0.01
               Else
               brushSize.f = 0.00001
             EndIf
             
             DoTerrainModify(0, 0, EntityX(stone),EntityY(stone),EntityZ(stone), brushSize, 48) 
             
             
             EndIf
   
          Wend
          
          
        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)  ; to move catapult right
        MoveEntity(#Catapult, 10,0,0)
        ApplyEntityImpulse(#Catapult, 10, -10, 0)

      ElseIf KeyboardPushed(#PB_Key_Z) ; to move left
        MoveEntity(#Catapult, -10,0,0)
        ApplyEntityImpulse(#Catapult, -10, -10, 0)
      ElseIf KeyboardPushed(#PB_Key_R)
        RotateEntity(#Catapult, 0, -0.3, 0,#PB_Relative)
      ElseIf KeyboardPushed(#PB_Key_E)
        RotateEntity(#Catapult, 0, 0.3, 0,#PB_Relative)
      EndIf
            
      DisableEntityBody(#Catapult, 0)
      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
       
       ;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_C) 
  flg = 0
  DisableEntityBody(stone, #True)
  DisableParticleEmitter(#PARTICLE, #True)
  MoveEntity(stone, EntityX(#Catapult)+0,EntityY(#Catapult)+10,EntityZ(#Catapult), #PB_Absolute)
  MoveParticleEmitter(#Particle, 0,0,0,#PB_Absolute)
  DisableEntityBody(stone, #False)
  DisableParticleEmitter(#PARTICLE, #False)
  ParticleEmissionRate(#PARTICLE, 50)
  ParticleSize(#PARTICLE, 5, 5)
  ParticleTimeToLive(#PARTICLE, 0.25, 0.25)
EndIf 

If KeyboardReleased(#PB_Key_P) 
  
  If power
    Impulse = 750
    power ! 1
  Else
    Impulse = 100
    Power ! 1
  EndIf
  
EndIf

If KeyboardPushed(#PB_Key_M)
  Debug EntityX(stone)
  Debug EntityY(stone)
  Debug EntityZ(stone)
  Debug TerrainHeight(terrain, 32, 80)

  Debug "---------------------"
EndIf
  
  RenderWorld()   
  FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)

; the following proc. is by Realizimo
Procedure DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, newheight.f)
  terrainSize.f = TerrainTileSize(terrain, tx, ty) - 1
  Pointx.f = TerrainTilePointX(terrain, tx, ty, wx, wy, wz) 
  Pointy.f = TerrainTilePointY(terrain, tx, ty, wx, wy, wz)   
  startx = (Pointx - mBrushSizeTerrainSpace) * terrainSize
  starty = (Pointy - mBrushSizeTerrainSpace) * terrainSize
  endx   = (Pointx + mBrushSizeTerrainSpace) * terrainSize
  endy   = (Pointy + mBrushSizeTerrainSpace) * terrainSize  
  

  For y = starty To endy
    For x = startx To endx  
      SetTerrainTileHeightAtPoint(terrain, tx, ty, x, y, newheight)      
    Next x
  Next y
  UpdateTerrain(terrain)
  CreateTerrainBody(terrain, 2, 1)
EndProcedure

Re: Catapult demo with a motor

Posted: Wed Oct 01, 2014 3:12 pm
by Bananenfreak
Excuse me, there´s something wrong in my last post here.

Code: Select all

WorldCollisionAppliedImpulse()
      imp\x = GetX()
      imp\y = GetY()
      imp\z = GetZ()
      imp_len = Sqr(Pow(imp\x, 2) + Pow(imp\y, 2) + Pow(imp\z, 2))
Only squareroot :)
note that the SecondWorldCollisionEntity() does not return the terrain number.
This is what Comtois posted and I wrote in Bugsection... Second Entity should be given terrainnumber, not a "senseless" number (Perhaps TerrainID()?).

Re: Catapult demo with a motor

Posted: Wed Oct 01, 2014 4:42 pm
by Comtois
About WorldCollisionAppliedImpulse (), please note that I made a mistake, I thought it was a vector3 but after checking (getX/Y/Z() give same value) it's a float.

extract from Bullet's code

Code: Select all

///this returns the most recent applied impulse, to satisfy contact constraints by the constraint solver
			btScalar	getAppliedImpulse()
btScalar is a float.

Re: Catapult demo with a motor

Posted: Wed Oct 01, 2014 8:24 pm
by applePi
Comtois wrote:
i thought it was a vector3 but after checking (getX/Y/Z() give same value) it's a float.
do i need to change change this part of the code to decide that the hole are big or small according to the WorldCollisionAppliedImpulse() .

Code: Select all

WorldCollisionAppliedImpulse()
ImpulseX.f = GetX()
ImpulseY.f = GetY()
ImpulseZ.f = GetZ()
ImpulseXYZ.f = Sqr(Pow(ImpulseX, 2) + Pow(ImpulseY, 2) + Pow(ImpulseZ, 2))
If ImpulseXYZ > 120
        brushSize.f = 0.01
            Else
            brushSize.f = 0.00001
 EndIf

Re: Catapult demo with a motor

Posted: Thu Oct 02, 2014 6:50 am
by Bananenfreak
You only Need this:

Code: Select all

WorldCollisionAppliedImpulse()
ImpulseX.f = GetX()

If ImpulseXYZ > 120
        brushSize.f = 0.01
            Else
            brushSize.f = 0.00001
 EndIf
@Comtois:
So, there´s no possibility for vector3 Impulse? In some reason it could be great to have that (But I don´t know in which case :D)