Page 1 of 2

Just to play with 3D (4.60 beta 2)

Posted: Wed Apr 13, 2011 4:16 pm
by Comtois
Just copy this code in IDE and run it. I like The CreateCube, CreateSphere, CreateCylinder, now it is possible to create small demo for forum without medias.

Use cursor and mouse.

Code: Select all

;Ribbon example by kelebrindae
;
;http://www.purebasic.fr/french/viewtopic.php?f=13&t=11748&start=10

#PB_Engine_Space_Local=1
#PB_Engine_Space_Parent=2
#PB_Engine_Space_World=4
#PB_Engine_Absolute_Rotation=8
#PB_Engine_Relative_Rotation=16
#PB_Engine_Quaternion_Rotation=32
#PB_Engine_Euler_Rotation=64

Global RotationAbs = #PB_Engine_Euler_Rotation | #PB_Engine_Absolute_Rotation
Global RotationRel = #PB_Engine_Euler_Rotation | #PB_Engine_Relative_Rotation

#PlayerSpeed = 4
#Camera = 0

Enumeration
  #VueArriere
  #VueDessus
EndEnumeration

Macro NEW_X(x, Angle, Distance)
  ((x) + Cos(Radian(Angle)) * (Distance))
EndMacro

Macro NEW_Z(z, Angle, Distance)
  ((z) + Sin(Radian(Angle)) * (Distance))
EndMacro

Structure Bullet
  Bullet.i
  numNode.i
  numRibbon.i
  timer.f
  DirectionX.f
  DirectionY.f
  DirectionZ.f
  Speed.f
  Life.l
EndStructure

NewList Bullets.Bullet()

;-Variable and tab
Define.f KeyX, KeyY, MouseX, MouseY
Define.l i, j, Color
Define.f AngleCanon,Pas
Color = $0000FF
Pas = 0.05
AngleTourelle = 0
TirMax = 400
Time = ElapsedMilliseconds()
d.f=170

Declare GestionCamera(Mode.l)
Declare CreatePyramideCube(x.f, y.f, z.f, MeshPyramide, PyramideMaterial)


If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  ExamineDesktops()
  OpenScreen(DesktopWidth(0), DesktopHeight(0), DesktopDepth(0), "char")
  
  WorldShadows(#PB_Shadow_Modulative)
  
  EnableWorldPhysics(#True)
  EnableWorldCollisions(#True)
  
  ;-Texture
  Add3DArchive(".",#PB_3DArchive_FileSystem)
  CreateImage(0,128, 128)
  StartDrawing(ImageOutput(0))
  Box(0, 0, 128, 128, $FFFFFF)
  StopDrawing()
  SaveImage(0,"temp.bmp")
  FreeImage(0)
  LoadTexture(0,"temp.bmp")
  DeleteFile("temp.bmp")
  ; Ribbon texture
  CreateImage(0,128, 1)
  StartDrawing(ImageOutput(0))
  DrawingMode(#PB_2DDrawing_Gradient)     
  BackColor($000000)
  GradientColor(0.5, $FFFFFF)
  FrontColor($0000)
  LinearGradient(0, 0, 128, 0)
  Box(0, 0, 128, 1)
  StopDrawing()
  SaveImage(0,"temp2.bmp")
  FreeImage(0)
  LoadTexture(1,"temp2.bmp")
  DeleteFile("temp2.bmp")
  
  ;-Material
  ;Tank
  CreateMaterial(0, TextureID(0))
  MaterialAmbientColor(0, $00FFFF)
  ;Bullet
  CreateMaterial(1, TextureID(0))
  MaterialAmbientColor(1, Color)
  ;Sol
  CreateMaterial(5, TextureID(0))
  MaterialAmbientColor(5, $00FF00)
  ;Pyramide
  CreateMaterial(6, TextureID(0))
  MaterialAmbientColor(6, $4400CC)
  ;Ribbon
  CreateMaterial(7,TextureID(1)) ; ribbons
  MaterialDepthWrite(7, #False)
  DisableMaterialLighting(7,#True)
  MaterialBlendingMode(7,#PB_Material_Add)
  MaterialFilteringMode(7,#PB_Material_None)
  
  ;-Create Sol
  Sol = CreatePlane(#PB_Any, 100, 100, 20, 20, 20, 20)
  CreateEntity (5, MeshID(Sol), MaterialID(5))
  EntityRenderMode(5, 0) ; Disable shadow casting for this entity as it's our plan
  EntityPhysicBody(5, #PB_Entity_StaticBody)
  
  
  
  ;-Create Pyramide
  MeshPyramide = CreateCube(#PB_Any, 10)
  Nb=7
  Size=20*3+0.5
  For j = 0 To Nb
    For i= 0 To Nb-j
      CreatePyramideCube(j*(Size/2)+i*Size, (Size/2)+(j*Size), 800, MeshPyramide, 6)
    Next i
  Next j
  
  ;-Corps
  CreateCube(0, 20)
  CreateNode(0)
  CreateEntity(0, MeshID(0), MaterialID(0))
  ScaleEntity(0,8,2,4)
  MoveEntity(0,0,20,0)
  AttachNodeObject(0,EntityID(0),#PB_Node_Entity)
  
  ;Only for collision
  CopyEntity(0,10)
  ScaleEntity(10,8,2,4)
  MoveEntity(10,0,20,0)   
  EntityPhysicBody(10, #PB_Entity_BoxBody, 47)
  HideEntity(10,1)
  
  ;-Tourelle
  CreateSphere(1, 30)
  CreateNode(1)
  CreateEntity(1, MeshID(1), MaterialID(0))
  EntityRenderMode(1, 0)
  AttachNodeObject(0, NodeID(1),#PB_Node_Node)
  AttachNodeObject(1,EntityID(1),#PB_Node_Entity)
  MoveNode(1, 0, 40, 0)
  
  ;Only for collision
  CopyEntity(1, 11)
  EntityPhysicBody(11, #PB_Entity_SphereBody, 47)
  HideEntity(11,1)
  
  ;-Canon
  CreateCylinder(2, 10, 170)
  CreateEntity(2, MeshID(2), MaterialID(0))
  AttachNodeObject(1,EntityID(2),#PB_Node_Entity)
  RotateEntity(2,0, 0, 90)
  MoveEntity(2, 80, 15, 0)
  
  ;-Cameras
  CreateCamera(0, 0, 0, 100, 100)
  CameraLocate(0, 0, 200, 350)
  CameraLookAt(0,NodeX(0),NodeY(0),NodeZ(0))
  CameraFOV(0, 50)
  CameraBackColor(0,$221100)
  
  CreateCamera(1, 0, 67, 33, 33)
  CameraBackColor(1,$661100)
  
  ;-Light
  CreateLight(0,RGB(125,125,125),800,500,0)
  AmbientColor(RGB(85,85,85))
  
  ;-Sprite
  CreateSprite(0, 400, 60)
  
  ;- >>> Main Loop
  Repeat
    If ExamineKeyboard()
      
      If KeyboardReleased(#PB_Key_F1)
        Mode=#VueArriere
      ElseIf KeyboardReleased(#PB_Key_F2)
        Mode=#VueDessus
      EndIf
      
      If KeyboardPushed(#PB_Key_Up)
        
        NodeLocate(0, NEW_X(NodeX(0), NodeYaw(0), #PlayerSpeed), NodeY(0), NEW_Z(NodeZ(0), NodeYaw(0), #PlayerSpeed))
      ElseIf KeyboardPushed(#PB_Key_Down)
        NodeLocate(0, NEW_X(NodeX(0), NodeYaw(0), -#PlayerSpeed), NodeY(0), NEW_Z(NodeZ(0), NodeYaw(0), -#PlayerSpeed))
      EndIf
      If KeyboardPushed(#PB_Key_Left)
        RotateNode(0,0, -1.2, 0, RotationRel)
      ElseIf KeyboardPushed(#PB_Key_Right)
        RotateNode(0,0, 1.2 ,0, RotationRel)
      EndIf
      
    EndIf
    
    If ExamineMouse()
      MouseX = -(MouseDeltaX()/10)*#Playerspeed
      MouseY = -(MouseDeltaY()/10)*#PlayerSpeed
      
      If MouseButton(#PB_MouseButton_Left) And ElapsedMilliseconds()-Time > 500
        Time = ElapsedMilliseconds()
        Bullet = CreateEntity(#PB_Any, MeshID(1), MaterialID(1))
        ScaleEntity(Bullet,0.1,0.1,0.1)
        EntityLocate(Bullet, NodeX(1) + Cos(Radian(NodeYaw(0) + NodeYaw(1))) * d, 15 + NodeY(1) -Tan(Radian(AngleCanon))* d, NodeZ(1) + Sin(Radian(NodeYaw(0) + NodeYaw(1))) * d)
        EntityPhysicBody(Bullet, #PB_Entity_SphereBody, 10)
        AddElement(Bullets())
        With Bullets()
          \Bullet = Bullet
          \numNode = CreateNode(#PB_Any)
          NodeLocate(\numNode, EntityX(\Bullet), EntityY(\Bullet), EntityZ(\Bullet))
          \numRibbon = CreateRibbonEffect(#PB_Any,MaterialID(7),NodeID(\numNode),1,50,60*0.1, 500)
          ;AttachNodeObject(\numNode, EntityID(\Bullet), #PB_Node_Entity)
          RibbonEffectColor(\numRibbon,Color,$FFFFFF)
          \DirectionX = Cos(Radian(NodeYaw(0) + NodeYaw(1)))
          \DirectionY = -Tan(Radian(AngleCanon))
          \DirectionZ = Sin(Radian(NodeYaw(0) + NodeYaw(1)))
          \Speed = TirMax
          \Life = ElapsedMilliseconds()
        EndWith
      EndIf
    EndIf
    
    ForEach Bullets()
      With Bullets()
        ApplyEntityImpulse(\Bullet, \DirectionX*\Speed, \DirectionY*\Speed, \DirectionZ*\Speed)
        NodeLocate(\numNode, EntityX(\Bullet), EntityY(\Bullet), EntityZ(\Bullet))
        
        If ElapsedMilliseconds()-Bullets()\Life>20000
          If IsEntity(\Bullet)
            ;FreeEntity(\Bullet)
            FreeNode(\numNode)
            FreeEffect(\numRibbon)
            
          EndIf
          DeleteElement(Bullets(), 1)
        EndIf
      EndWith
    Next
    
    AngleCanon + MouseY
    If AngleCanon<-45
      AngleCanon = -45
    ElseIf AngleCanon>2
      AngleCanon = 2
    EndIf
    
    RotateNode(1, NodePitch(1), NodeYaw(1)-MouseX, AngleCanon,  RotationAbs)
    RotateEntity(10, NodePitch(0), NodeYaw(0), NodeRoll(0),RotationAbs)
    EntityLocate(10, EntityX(0), EntityY(0), EntityZ(0))
    EntityLocate(11, NodeX(1), NodeY(1), NodeZ(1))
    
    ;Retravailler la position !
    CameraLocate(1, NodeX(1), 40 + NodeY(1), NodeZ(1))
    RotateCamera(1, NodePitch(1), 90+NodeYaw(0)+NodeYaw(1), AngleCanon, RotationAbs)   
    CameraLookAt(1, NodeX(1) + Cos(Radian(NodeYaw(0) + NodeYaw(1))) * d, 15 + NodeY(1) -Tan(Radian(AngleCanon))* d, NodeZ(1) + Sin(Radian(NodeYaw(0) + NodeYaw(1))) * d)
    
    GestionCamera(Mode)
    RenderWorld()
    StartDrawing(SpriteOutput(0))
    DrawText(10,10, "FPS = " +Str(Engine3DFrameRate(#PB_Engine3D_Current)), $FFFF00, $FF0000)
    StopDrawing()
    DisplayTransparentSprite(0,0,0)
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Procedure CreatePyramideCube(x.f, y.f, z.f, MeshPyramide, PyramideMaterial)
  Block = CreateEntity(#PB_Any, MeshID(MeshPyramide), MaterialID(PyramideMaterial), x, y, z)
  ScaleEntity(Block, 6, 6, 6)
  EntityPhysicBody(Block, #PB_Entity_BoxBody, 47)
  ProcedureReturn Block
EndProcedure

Procedure.f curveValue(actuelle.f, Cible.f, P.f)
  If P > 1000.0
    P = 1000.0
  EndIf
  ProcedureReturn (actuelle + ( (Cible - actuelle) * P / 1000.0))
EndProcedure

Procedure.f CurveAngle(oldangle.f,newangle.f, increments.f)
  If increments>1
    If (oldangle+360)-newangle<newangle-oldangle
      oldangle=360+oldangle
    EndIf
    If (newangle+360)-oldangle<oldangle-newangle
      newangle=360+newangle
    EndIf
    oldangle=oldangle-(oldangle-newangle)/increments
  EndIf
  
  If increments<=1
    ProcedureReturn newangle
  EndIf
  ProcedureReturn oldangle
EndProcedure

Procedure GestionCamera(Mode.l)
  Define.f Px, Py, Pz, Pv, Pp
  Static AngleCamera.f
  
  Pv = 25
  Pp = 10
  Select Mode
      
    Case #VueDessus
      AngleCamera = CurveAngle(AngleCamera, NodeYaw(0) + 180, Pp)
      Px = CurveValue(CameraX(#Camera), NEW_X(NodeX(0), AngleCamera, 100), Pv)
      Py = CurveValue(CameraY(#Camera), NodeY(0) + 1390, Pv)
      Pz = CurveValue(CameraZ(#Camera), NEW_Z(NodeZ(0), AngleCamera, 100), Pv)
      
    Case #VueArriere
      AngleCamera = CurveAngle(AngleCamera, NodeYaw(0) + 180, Pp)
      Px = CurveValue(CameraX(#Camera), NEW_X(NodeX(0), AngleCamera, 260), Pv)
      Py = CurveValue(CameraY(#Camera), NodeY(0) + 70, Pv)
      Pz = CurveValue(CameraZ(#Camera), NEW_Z(NodeZ(0), AngleCamera, 260), Pv)
      
  EndSelect
  CameraLocate(#Camera, Px, Py, Pz)
  CameraLookAt(#Camera, NodeX(1), NodeY(1)+ 15, NodeZ(1))
EndProcedure

Re: Just to play with 3D (4.60 beta 2)

Posted: Wed Apr 13, 2011 4:31 pm
by c4s
Wow, looks and feels pretty cool! 8)

Re: Just to play with 3D (4.60 beta 2)

Posted: Thu Apr 14, 2011 7:51 am
by Rings
damm cool example !!!

Re: Just to play with 3D (4.60 beta 2)

Posted: Thu Apr 14, 2011 8:03 am
by Comtois
Thanks

Added Ribbon.

kelebrindae have done a more beautiful example here
http://www.purebasic.fr/french/viewtopi ... 8&start=10

Code deleted , see first post (updated).

Re: Just to play with 3D (4.60 beta 2)

Posted: Thu Apr 14, 2011 8:40 am
by c4s
This is fantastic!

Is it possible to make smoother lights though? I tried WorldShadows(#PB_Shadow_Additive) but the shadows still don't look smooth.
Also would it be possible to make the tank collide with the boxes as well?

(Don't know anything about 3D ;))

Re: Just to play with 3D (4.60 beta 2)

Posted: Thu Apr 14, 2011 10:34 am
by Kelebrindae
@Comtois: Thanks for the link! :)

Cool demo, but I think you should reduce the size of your entities: it would make the physics look more "real".
I'd say the physic engine is calibrated to something like "1 unit = 1 meter in the real world".
In your demo, it means that every cube in the pyramid are 20 meters wide; when they fall, it looks like they move in low gravity; just like a real building appears to fall slower than a "Jenga" tower :wink: ...
I don't know if my explanation is clear, but the fact is: small entities fall quicker.

Take a look at this demo (press space to swing the hammer). The cubes in the pyramids are only 1 unit wide => their fall looks more realistic (in my opinion).

[EDIT] Updated to 4.60RC2

Code: Select all

; Window size
#SCREENWIDTH = 800
#SCREENHEIGHT = 500

;- initialization
InitEngine3D()
InitSprite()
InitKeyboard()

;- Window
OpenWindow(0, 0, 0, #SCREENWIDTH, #SCREENHEIGHT, "Joint test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, #SCREENWIDTH,#SCREENHEIGHT, 0, 0, 0,#PB_Screen_SmartSynchronization)

;-Texture
Add3DArchive(".",#PB_3DArchive_FileSystem)
CreateImage(0,128, 128)
StartDrawing(ImageOutput(0))
  Box(0, 0, 128, 128, $FFFFFF)
StopDrawing()
SaveImage(0,"temp.bmp")
FreeImage(0)
LoadTexture(0,"temp.bmp")
DeleteFile("temp.bmp")

;-Material
CreateMaterial(1,TextureID(0))
MaterialAmbientColor(1,#PB_Material_AmbientColors)
CreateMaterial(2,TextureID(0))
MaterialAmbientColor(2,$FFFF00)
CreateMaterial(3,TextureID(0))
MaterialAmbientColor(3,$0077FF)
CreateMaterial(4,TextureID(0))
MaterialAmbientColor(4,$FF00FF)
CreateMaterial(5,TextureID(0))
MaterialAmbientColor(5,$0055BB)

;- Entities
CreatePlane(1,30,30,20,20,1,1)
sol  = CreateEntity(#PB_Any, MeshID(1), MaterialID(1))
EntityPhysicBody(sol, #PB_Entity_StaticBody)   


; Ball and tee
CreateCube(2, 1.0)
socle = CreateEntity(#PB_Any,MeshID(2), MaterialID(1))
ScaleEntity(socle,0.5,1,0.5)
EntityLocate(socle,2.6,0.5,0)
EntityPhysicBody(socle, #PB_Entity_StaticBody)

CreateSphere(3,0.5)
ball = CreateEntity(#PB_Any, MeshID(3), MaterialID(3))
EntityLocate(ball,2.6,1.5,0)
EntityPhysicBody(ball, #PB_Entity_SphereBody,1,4,1)

; Targets
For i = 1 To 4
  temp = CreateEntity(#PB_Any, MeshID(2), MaterialID(4))
  EntityLocate(temp,-10,0.5,(i*1.1)-2.8)
  EntityPhysicBody(temp, #PB_Entity_BoxBody, 0.25)
Next i
For i = 1 To 3
  temp = CreateEntity(#PB_Any, MeshID(2), MaterialID(4))
  EntityLocate(temp,-10,1.5,(i*1.1)-2.3)
  EntityPhysicBody(temp, #PB_Entity_BoxBody, 0.25)
Next i
For i = 1 To 2
  temp = CreateEntity(#PB_Any, MeshID(2), MaterialID(4))
  EntityLocate(temp,-10,2.5,(i*1.1)-1.8)
  EntityPhysicBody(temp, #PB_Entity_BoxBody, 0.25)
Next i


; Mobile
Global hammerX.f = 5,hammerY.f = 8.5,hammerZ.f
CreateSphere(4,0.4)
axe = CreateEntity(#PB_Any, MeshID(4), MaterialID(2))
EntityLocate(axe,hammerX,hammerY,hammerZ)
EntityPhysicBody(axe, #PB_Entity_StaticBody)

shaft = CreateEntity(#PB_Any, MeshID(2), MaterialID(5))
ScaleEntity(shaft,0.3,6,0.3)
EntityLocate(shaft,hammerX,hammerY - 3.5,hammerZ)
EntityPhysicBody(shaft, #PB_Entity_BoxBody,0.1)

hammerhead = CreateEntity(#PB_Any,MeshID(2), MaterialID(2))
ScaleEntity(hammerhead,2,1,1)
EntityLocate(hammerhead,hammerX,hammerY - 7,hammerZ)
EntityPhysicBody(hammerhead,#PB_Entity_BoxBody,20)

PointJoint(shaft,0,-3.1 ,0, hammerhead,0,0.5,0) ; Attach the hammerhead to the shaft
; Attach the hammer to axis.
HingeJoint(axe,0,0,0, #False,#False,#True, shaft,0,3.5,0, #False,#False,#True)
HingeJoint(axe,0,0,0, #False,#False,#True, hammerhead,0,7,0, #False,#False,#True)


;- Camera
CreateCamera(0, 0, 0, 100, 100)
CameraLocate(0,-2,8,18)
CameraLookAt(0,-2,5,0)

;-Light
AmbientColor($666666)
CreateLight(0,RGB(160,160,255),200,400,200)
WorldShadows(#PB_Shadow_Additive)

KeyboardMode(#PB_Keyboard_International)
;ApplyEntityImpulse(hammerhead,  20, 0, 0) ; not necessary anymore since 4.60 RC2

;- Main loop
Repeat
  Delay(1)
  While WindowEvent() : Wend

  ;- F1, F2, F3 : Change view
  If ExamineKeyboard()
    If KeyboardReleased(#PB_Key_F1)
      CameraLocate(0,-18,2,0)
      CameraLookAt(0,0,3,0)
    EndIf
    If KeyboardReleased(#PB_Key_F2)
      CameraLocate(0,0,12,20)
      CameraLookAt(0,0,5,0)
    EndIf
    If KeyboardReleased(#PB_Key_F3)
      CameraLocate(0,1,5,10)
      CameraLookAt(0,-3,3,0)
    EndIf
   
    ;- [Space] : Throw hammer
    If KeyboardPushed(#PB_Key_Space)
      ApplyEntityImpulse(hammerhead,  5, 0, 0)
    EndIf
    ;- R : Reset ball's position (doesn't work)
    If KeyboardReleased(#PB_Key_R)
      FreeEntity(ball)
    EndIf
   
    ;- Return : Display FPS
    If KeyboardReleased(#PB_Key_Return)
      MessageRequester("Infos","FPS = " + Str(Engine3DFrameRate(#PB_Engine3D_Average)))
    EndIf

  EndIf

  ; Render
  RenderWorld()
  FlipBuffers()

  If Not IsEntity(ball)
      ball = CreateEntity(#PB_Any, MeshID(3), MaterialID(3))
      EntityLocate(ball,2.6,1.5,0)
      EntityPhysicBody(ball, #PB_Entity_SphereBody,1,4,1)
  EndIf

Until KeyboardPushed(#PB_Key_Escape)

End

Re: Just to play with 3D (4.60 beta 2)

Posted: Thu Apr 14, 2011 10:52 am
by Comtois
Nice demo :)

I tested WorldGravity (-1000) without success, and I see that you just made a bug report, thank you:)

Re: Just to play with 3D (4.60 beta 2)

Posted: Thu Apr 14, 2011 11:15 am
by Comtois
c4s wrote:Also would it be possible to make the tank collide with the boxes as well?
I already tried adding EntityPhysicBody (0, # PB_Entity_BoxBody, 47), but without success. I do not know if it's a bug or not?
So to remedy this I added 2 entity. it works a little, and after a moment, it bug (no more collision).

Code: Select all

 RotateEntity(10, NodePitch(0), NodeYaw(0), NodeRoll(0),RotationAbs)
 EntityLocate(10, EntityX(0), EntityY(0), EntityZ(0))
 EntityLocate(11, NodeX(1), NodeY(1), NodeZ(1))
I have edited the code.

Re: Just to play with 3D (4.60 beta 2)

Posted: Tue Oct 18, 2011 12:08 am
by falsam
Since version 4.60 RC2, this code has a different behavior.

Place InitEngine3D() before InitSprite()
replace CreatePlane(1,2,2,20,20,1,1) by CreatePlane(1,30,20,20,20,1,1)

Re: Just to play with 3D (4.60 beta 2)

Posted: Wed Oct 19, 2011 9:19 pm
by Comtois
falsam wrote:Since version 4.60 RC2, this code has a different behavior.
replace CreatePlane(1,2,2,20,20,1,1) by CreatePlane(1,30,20,20,20,1,1)
If you want the same Size as before -> Replace CreatePlane(1,2,2,20,20,1,1) by CreatePlane(1,40,40,20,20,1,1)

TileSizeX, TileSizeZ are replaced by PlaneSizeX and PlaneSizeZ

Re: Just to play with 3D (4.60 beta 2)

Posted: Wed Oct 19, 2011 10:51 pm
by Polo
On the Mac, with Comtois' demo I can only see a black screen, nothing more (not crashing, though).

With Kele's demo I can see some objects, though most of them seems to fall, I guess it's not the desired behaviour :)
BTW Kele's code is wrong at least on the mac, it requires to put init engine before init sprite.

Re: Just to play with 3D (4.60 beta 2)

Posted: Wed Oct 19, 2011 10:52 pm
by Polo
(forgot to read Falsam comment, sorry! guess nothing's wrong then :))

Re: Just to play with 3D (4.60 beta 2)

Posted: Sat Oct 22, 2011 6:23 pm
by Kwai chang caine
Excuse me, but i have a memory error at the line 145 " RenderWorld()"
What i have forgotten :oops:

Re: Just to play with 3D (4.60 beta 2)

Posted: Mon Oct 24, 2011 8:17 am
by Kelebrindae
@Polo:
Yes, the parameters for "CreatePlane" have changed in RC2 (and are better that way, methinks) along a few other things. I've updated my code (in the post above).
And now, it's possible to reposition the ball on the tee by pressing "R".

Re: Just to play with 3D (4.60 beta 2)

Posted: Sun Mar 31, 2013 1:22 pm
by applePi
Hi Kelebrindae
regarding your hammer and ball example, i can't make it to work in 5.11 the hammer falls on the ground from the beginning, may be the problem with pointjoint and hingejoint, it works in 4.61, the example should be in the PB official examples.

Code: Select all

; Window size
#SCREENWIDTH = 800
#SCREENHEIGHT = 500

;- initialization
InitEngine3D()
InitSprite()
InitKeyboard()

;- Window
OpenWindow(0, 0, 0, #SCREENWIDTH, #SCREENHEIGHT, "Joint test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, #SCREENWIDTH,#SCREENHEIGHT, 0, 0, 0,#PB_Screen_SmartSynchronization)

;-Texture
Add3DArchive(".",#PB_3DArchive_FileSystem)
CreateImage(0,128, 128)
StartDrawing(ImageOutput(0))
  Box(0, 0, 128, 128, $FFFFFF)
StopDrawing()
SaveImage(0,"temp.bmp")
FreeImage(0)
LoadTexture(0,"temp.bmp")
DeleteFile("temp.bmp")

;-Material
CreateMaterial(1,TextureID(0))
;MaterialAmbientColor(1,#PB_Material_AmbientColors)
SetMaterialColor(1, #PB_Material_AmbientColor, RGB(100,100,100))
CreateMaterial(2,TextureID(0))
;MaterialAmbientColor(2,$FFFF00)
SetMaterialColor(2, #PB_Material_AmbientColor, $FFFF00)
CreateMaterial(3,TextureID(0))
;MaterialAmbientColor(3,$0077FF)
SetMaterialColor(3, #PB_Material_AmbientColor, $0077FF)
CreateMaterial(4,TextureID(0))
;MaterialAmbientColor(4,$FF00FF)
SetMaterialColor(4, #PB_Material_AmbientColor, $FF00FF)
CreateMaterial(5,TextureID(0))
;MaterialAmbientColor(5,$0055BB)
SetMaterialColor(5, #PB_Material_AmbientColor, $FF00FF)

;- Entities
CreatePlane(1,30,30,20,20,1,1)
sol  = CreateEntity(#PB_Any, MeshID(1), MaterialID(1))
EntityPhysicBody(sol, #PB_Entity_StaticBody)   


; Ball and tee
CreateCube(2, 1.0)
socle = CreateEntity(#PB_Any,MeshID(2), MaterialID(1))
ScaleEntity(socle,0.5,1,0.5)
MoveEntity(socle,2.6,0.5,0)
EntityPhysicBody(socle, #PB_Entity_StaticBody)

CreateSphere(3,0.5)
ball = CreateEntity(#PB_Any, MeshID(3), MaterialID(3))
MoveEntity(ball,2.6,1.5,0)
EntityPhysicBody(ball, #PB_Entity_SphereBody,1,4,1)

; Targets
For i = 1 To 4
  temp = CreateEntity(#PB_Any, MeshID(2), MaterialID(4))
  MoveEntity(temp,-10,0.5,(i*1.1)-2.8)
  EntityPhysicBody(temp, #PB_Entity_BoxBody, 0.25)
Next i
For i = 1 To 3
  temp = CreateEntity(#PB_Any, MeshID(2), MaterialID(4))
  MoveEntity(temp,-10,1.5,(i*1.1)-2.3)
  EntityPhysicBody(temp, #PB_Entity_BoxBody, 0.25)
Next i
For i = 1 To 2
  temp = CreateEntity(#PB_Any, MeshID(2), MaterialID(4))
  MoveEntity(temp,-10,2.5,(i*1.1)-1.8)
  EntityPhysicBody(temp, #PB_Entity_BoxBody, 0.25)
Next i


; Mobile
Global hammerX.f = 5,hammerY.f = 8.5,hammerZ.f
CreateSphere(4,0.4)
axe = CreateEntity(#PB_Any, MeshID(4), MaterialID(2))
MoveEntity(axe,hammerX,hammerY,hammerZ)
EntityPhysicBody(axe, #PB_Entity_StaticBody)

shaft = CreateEntity(#PB_Any, MeshID(2), MaterialID(5))
ScaleEntity(shaft,0.3,6,0.3)
MoveEntity(shaft,hammerX,hammerY - 3.5,hammerZ)
EntityPhysicBody(shaft, #PB_Entity_BoxBody,0.1)

hammerhead = CreateEntity(#PB_Any,MeshID(2), MaterialID(2))
ScaleEntity(hammerhead,2,1,1)
MoveEntity(hammerhead,hammerX,hammerY - 7,hammerZ)
EntityPhysicBody(hammerhead,#PB_Entity_BoxBody,20)

PointJoint(0,shaft,0,-3.1 ,0, hammerhead,0,0.5,0) ; Attach the hammerhead to the shaft
; Attach the hammer to axis.
HingeJoint(1,axe,0,0,0, #False,#False,#True, shaft,0,3.5,0, #False,#False,#True)
HingeJoint(2,axe,0,0,0, #False,#False,#True, hammerhead,0,7,0, #False,#False,#True)


;- Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0,-2,8,18)
CameraLookAt(0,-2,5,0)

;-Light
AmbientColor($666666)
CreateLight(0,RGB(160,160,255),200,400,200)
WorldShadows(#PB_Shadow_Additive)

KeyboardMode(#PB_Keyboard_International)
;ApplyEntityImpulse(hammerhead,  20, 0, 0) ; not necessary anymore since 4.60 RC2

;- Main loop
Repeat
  Delay(1)
  While WindowEvent() : Wend

  ;- F1, F2, F3 : Change view
  If ExamineKeyboard()
    If KeyboardReleased(#PB_Key_F1)
      MoveCamera(0,-18,2,0)
      CameraLookAt(0,0,3,0)
    EndIf
    If KeyboardReleased(#PB_Key_F2)
      MoveCamera(0,0,12,20)
      CameraLookAt(0,0,5,0)
    EndIf
    If KeyboardReleased(#PB_Key_F3)
      MoveCamera(0,1,5,10)
      CameraLookAt(0,-3,3,0)
    EndIf
   
    ;- [Space] : Throw hammer
    If KeyboardPushed(#PB_Key_Space)
      ApplyEntityImpulse(hammerhead,  5, 0, 0)
    EndIf
    ;- R : Reset ball's position (doesn't work)
    If KeyboardReleased(#PB_Key_R)
      FreeEntity(ball)
    EndIf
   
    ;- Return : Display FPS
    If KeyboardReleased(#PB_Key_Return)
      MessageRequester("Infos","FPS = " + Str(Engine3DFrameRate(#PB_Engine3D_Average)))
    EndIf

  EndIf

  ; Render
  RenderWorld()
  FlipBuffers()

  If Not IsEntity(ball)
      ball = CreateEntity(#PB_Any, MeshID(3), MaterialID(3))
      MoveEntity(ball,2.6,1.5,0)
      EntityPhysicBody(ball, #PB_Entity_SphereBody,1,4,1)
  EndIf

Until KeyboardPushed(#PB_Key_Escape)

End