Page 1 sur 3

Collision et Gravité

Publié : sam. 01/avr./2017 10:10
par falsam
Ce code montre une méthode pour générer une traînée de poussière derrière une entité en mouvement.

Contrôlez Sinbad avec les flèches du clavier pour le déplacer.

Une camera va suivre Sinbad.
- Ctrl + Up ou Down pour diminuer ou augmenter la distance entre la camera et Sinbad.
- Ctrl + Left ou Right pour tourner autour de Sinbad.

J'ai laissé volontairement un débug montrant que très souvent Sindbad n'est pas en contact avec le sol.

Hors j'ai besoin de cette information pour pouvoir coder d'autres actions. Par exemple quand je voudrais lui faire effectuer des sauts et dans ce cas, pas de nuages de poussières. Comment puis je faire autrement ?

Une autre question : La gravité par défaut est celle ce la terre. Hors quand Sinbad tombe dans le vide il tombe au ralenti.

Merci de votre aide.

Code : Tout sélectionner

;[PB 5.60 Minimum]

EnableExplicit

;Font
Global FontInfo = LoadFont(-1, "Arial", 12)

;Material
Global MatGround, MatBox, MatSmoke  

;Entities
Global Camera, Player, PlayerSpeed, CurrentAnimation.s, Angle = 0, Distance = 200, Action 
Global Ground, Box

;Sprite  
Global Bottom

;Init engine
If Not (InitEngine3D() And InitSprite() And InitKeyboard())
  End
EndIf

OpenWindow(0, 0, 0, 0, 0, "", #PB_Window_BorderLess | #PB_Window_Maximize)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0))

;The elements are in the examples folder of PureBasic
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/Packs/Sinbad.zip", #PB_3DArchive_Zip)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_International | #PB_Keyboard_AllowSystemKeys)
EnableWorldPhysics(#True) 

;-[2D] Create ou load sprite (Help)
Bottom = CreateSprite(-1, ScreenWidth(), 150, #PB_Sprite_AlphaBlending)

;-[3D] Create ou load 3D element

;-Textures
MatGround  = CreateMaterial(-1, TextureID(LoadTexture(-1, "Dirt.jpg")))   ;Ground material
MatBox     = CreateMaterial(-1, TextureID(LoadTexture(-1, "Caisse.png"))) ;Box materiel
MatSmoke   = CreateMaterial(-1, TextureID(LoadTexture(-1, "smoke.png")))  ;Particule materiel
MaterialBlendingMode(MatSmoke, #PB_Material_AlphaBlend)
DisableMaterialLighting(MatSmoke, 1)
;-Camera
Camera = CreateCamera(-1, 0, 0, 100, 100)

;-Sky and fog
SkyBox("desert07.jpg", RGB(255, 255, 255), 1, 900, 6000)
Fog(RGB(255, 255, 255), 1, 100, 1000)

;-Light and shadow
CreateLight(0, RGB(255, 255, 255), -100, 500, 100)
WorldShadows(#PB_Shadow_Additive)

;-Ground
Ground = CreateEntity(-1, MeshID(CreatePlane(-1, 1500, 1500, 1, 1, 20, 20)), MaterialID(MatGround))
CreateEntityBody(Ground, #PB_Entity_StaticBody)


;-Box
Box = CreateEntity(-1, MeshID(CreateCube(-1, 20)), MaterialID(MatBox), 0, 15, 0)
CreateEntityBody(Box, #PB_Entity_BoxBody, 1, 1, 1)

;-Player
Player = CreateEntity(-1, MeshID(LoadMesh(#PB_Any, "sinbad.mesh")), #PB_Material_None, 10, 40, -40)
ScaleEntity(Player, 5, 5, 5)
CreateEntityBody(Player, #PB_Entity_ConvexHullBody, 1, 3, 20)
EntityAngularFactor(Player, 0, 1, 0)
RotateEntity(Player, 0, -120, 0)

;- Dust emiter / Emetteur de poussiére
CreateParticleEmitter(0, 10, 10,0, #PB_Particle_Box)
ParticleMaterial(0, MaterialID(MatSmoke))
ParticleSize(0, 20, 20)
ParticleEmissionRate(0, 20)
ParticleEmitterDirection(0,0,1,0)
ParticleVelocity(0, #PB_Particle_Velocity, 20)
ParticleTimeToLive(0, 1, 1)
ParticleColorFader(0,0,0,0,-1)

Repeat
  ;-
  ;-[3D] Rendering
  If ExamineKeyboard()
    
    ;-Test collision 
    Debug  "EntityCollide(Ground, Player) " + Str(EntityCollide(Ground, Player))
    
    ;-keyboard events : Distance and move
    If KeyboardPushed(#PB_Key_LeftControl) And KeyboardPushed(#PB_Key_Up)
      Distance - 5
    ElseIf KeyboardPushed(#PB_Key_LeftControl) And KeyboardPushed(#PB_Key_Down)  
      Distance + 5
    ElseIf KeyboardPushed (#PB_Key_Up)
      PlayerSpeed = 50
      CurrentAnimation = "RunBase"
      Angle = 180
      Action = #True
    ElseIf KeyboardPushed (#PB_Key_Down)
      PlayerSpeed = -50
      Angle = 0
      CurrentAnimation = "RunBase"
      Action = #True
    Else
      CurrentAnimation = "IdleTop"
      PlayerSpeed = 0
      Action = #False
    EndIf
    
    ;-keyboard events : Rotation around player and turn
    If KeyboardPushed(#PB_Key_LeftControl) And KeyboardPushed(#PB_Key_Left)
      Angle - 5
    ElseIf KeyboardPushed(#PB_Key_LeftControl) And KeyboardPushed(#PB_Key_Right)
      Angle + 5
    ElseIf KeyboardPushed (#PB_Key_Left)
      RotateEntity(Player, 0, 3, 0, #PB_Relative)
      CurrentAnimation = "RunBase"
    ElseIf KeyboardPushed (#PB_Key_Right)
      RotateEntity(Player, 0, -3, 0, #PB_Relative)
      CurrentAnimation = "RunBase"
    EndIf
    
    If KeyboardReleased(#PB_Key_Escape)
      Break
    EndIf      
  EndIf
    
  If Action 
    DisableEntityBody(Player, #False) ; Wake up entity (BugWare) 
    
    ;Move player to new position
    MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
    
  EndIf   
  
  ;Play animation
  If EntityAnimationStatus(Player, CurrentAnimation) = #PB_EntityAnimation_Stopped 
    StartEntityAnimation(Player, CurrentAnimation)
  EndIf
  
  ;End of dust if the player does not move or not in contact with the ground
  MoveParticleEmitter(0,EntityX(Player), 0 ,EntityZ(Player),#PB_Absolute)
  DisableParticleEmitter(0,Bool(action=0))  
  
  
  CameraFollow(Camera, EntityID(Player), Angle, EntityY(Player) + 20, Distance, 0.05, 1, #True)
  
  RenderWorld(50)
  
  ;-
  ;-[2D] Rendering
  StartDrawing(SpriteOutput(Bottom))
  DrawingFont(FontID(FontInfo))
  DrawingMode(#PB_2DDrawing_AlphaChannel)
  Box(0, 0, ScreenWidth(), SpriteWidth(Bottom), RGBA(0, 0, 0, 110))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(10, 10, ". : : Help : : .")
  DrawText(10, 40, "Arrow keys - Move player.")
  DrawText(10, 70, "Ctrl + Up / Ctrl + Down  - Distance between camera and player.")
  DrawText(10, 100, "Ctrl + Left ou Ctrl + Right - Turn around the player.")
  StopDrawing()
  DisplayTransparentSprite(Bottom, 0, ScreenHeight() - SpriteHeight(Bottom))
  
  FlipBuffers()
Until WaitWindowEvent(1) = #PB_Event_CloseWindow

Re: Collision et Gravité

Publié : sam. 01/avr./2017 10:38
par Mindphazer
Salut Falsam
J'ai une erreur inédite sur mon Mac à la ligne 161 :

Code : Tout sélectionner

[11 :35 :48] Attente du démarrage du programme...
[11 :35 :48] Type d'exécutable: MacOSX - x64  (64bit, Unicode)
[11 :35 :48] Exécutable démarré.
[11 :35 :49] [ERREUR] Ligne: 161
[11 :35 :49] [ERREUR] Programme avorté. (par librairie externe)
[11 :35 :56] Le programme a été arrêté.
8O

Re: Collision et Gravité

Publié : sam. 01/avr./2017 10:43
par falsam
RenderWorld() est une fonction native. Pourtant tu n'as pas cette erreur avec le code Mirror Experience http://www.purebasic.fr/french/viewtopi ... 35#p191335 !!

Re: Collision et Gravité

Publié : sam. 01/avr./2017 11:04
par Mindphazer
Non.
Mais je l'ai avec Sinbad et la mer : http://www.purebasic.fr/french/viewtopi ... 13&t=16617

Re: Collision et Gravité

Publié : sam. 01/avr./2017 11:08
par falsam
ha mince tu m'embrouilles maintenant :wink: . Ton erreur ligne 161 est sur le code Sinbad et la mer ou Collision et Gravité ?

Re: Collision et Gravité

Publié : sam. 01/avr./2017 11:40
par djes
J'ai souvent eu des erreurs avec l'ombre (shadow), essaye aussi de désactiver ça...

Re: Collision et Gravité

Publié : sam. 01/avr./2017 11:51
par falsam
@Djes : Avec ce code je n'ai pas de souci d'ombre. :wink:

Re: Collision et Gravité

Publié : sam. 01/avr./2017 11:57
par djes
falsam a écrit :@Djes : Avec ce code je n'ai pas de souci d'ombre. :wink:
Oui, pardon, je parlais à Mindphazer. J'ai vu les autres sujets après :) Tu t'amuses bien, dis donc, tu vas nous faire un mario 3d ou un prince of persia ?

Re: Collision et Gravité

Publié : sam. 01/avr./2017 12:21
par falsam
Djes a écrit :Tu t'amuses bien, dis donc
J'essaye de comprendre un peu mieux ce moteur. Hier soir j'ai trop abusé de la 3D et à force de tester j'ai eu des nausées et un mal de crane terrible.
Djes a écrit :dis donc, tu vas nous faire un mario 3d ou un prince of persia ?
Monsieur est taquin :mrgreen:

J'ai rencontré une équipe de développement de jeux commerciaux et j'ai bien compris que tout seul c'est impossible.

Prince of persia c'est trois années de conception avec 55 personnes 4 188 fichiers pour un total de 1 263 580 lignes de codes. Je te laisse déduire la masse salariale que ça représente :mrgreen:

Re: Collision et Gravité

Publié : sam. 01/avr./2017 12:27
par falsam
Pour le moment je ne sais toujours pas comment régler correctement les collisions entre Sinbad et le sol.

Re: Collision et Gravité

Publié : sam. 01/avr./2017 14:33
par djes
Oui, réaliser un jeu de ce type est impossible seul, mais on peut toujours trouver un petit concept innovant qui cartonne... Je te fais confiance ;)
Sinon pour les collisions, ce serait peut-être plus indiqué de les gérer avec un modèle simplifié invisible ...

Re: Collision et Gravité

Publié : sam. 01/avr./2017 16:44
par Mindphazer
falsam a écrit :ha mince tu m'embrouilles maintenant :wink: . Ton erreur ligne 161 est sur le code Sinbad et la mer ou Collision et Gravité ?
Les deux !!

Re: Collision et Gravité

Publié : sam. 01/avr./2017 16:46
par Mindphazer
djes a écrit :J'ai souvent eu des erreurs avec l'ombre (shadow), essaye aussi de désactiver ça...
A quel endroit je désactive l'ombre ?

Re: Collision et Gravité

Publié : sam. 01/avr./2017 16:49
par falsam
Mindphazer a écrit :A quel endroit je désactive l'ombre ?
Cherche WorldShadows() et commente

Re: Collision et Gravité

Publié : sam. 01/avr./2017 17:10
par Mindphazer
Oui, j'ai posé la question un peu vite :mrgreen:
Bon, en commentant la ligne WorldShadows(), ça ne fonctionne pas mieux

--edité par Ar-S--