http://www.purebasic.fr/english/viewtopic.php?t=31022
j'ai bien essayé de comprendre le pourquoi du comment, mais j'ai rien compris. Néanmoins, j'ai fait des petits trucs en partant de la 4eme demo, et çà donne çà:
C'est un moteur fantastique et j'ai bien fait mumuse
Question: est ce que Ogre a son propre moteur physique ? Si non, j'espère que çà va remonter aux oreilles de Fred.
le code suivant est une modif du code original nommé chipmunkDemoMain.pb
Code : Tout sélectionner
;
; Try to make the chipmunk-demo available in PB !
; auteur eddy( grand merci :) ) et légèrement modifié par beauregard:
; intéraction avec le grand carré de la Demo 4( touche 4)
; touches fléchées du clavier gauche et droite.
;
XIncludeFile #PB_Compiler_Home + "Includes\chipmunk_include.pbi" ; Hum !
Define *space, *staticBody ; avec des asterix, çà déchire !
Global INFINITY.f = 1e1000
Global sensrotation.f ; 1er rajout: une variable, hum.
Structure sDemo ; çà c'est de la bonne variables ma bonne dame, vous pouvez m'en croire !
*start
*update
*destroy
EndStructure
Dim Demos.sDemo(7)
Procedure LinePro (hdc.l,x1.l,y1.l,x2.l,y2.l,width.l,color.l); sept trucs, 7 !
pen.l=CreatePen_(#PS_SOLID,width.l,color.l)
hPenOld.l=SelectObject_(hdc.l,pen.l)
MoveToEx_(hdc.l,x1.l,y1.l,0):LineTo_(hdc.l,x2.l,y2.l)
DeleteObject_(pen.l)
DeleteObject_(hPenOld.l)
EndProcedure
Procedure CirclePro (hdc.l,x1.l,y1.l,radius.l,width.l,color.l)
pen.l=CreatePen_(#PS_SOLID,width.l,color.l)
hPenOld.l=SelectObject_(hdc.l,pen.l)
Ellipse_(hdc.l,x1.l-(radius.l/2),y1.l-(radius.l/2),x1.l+radius.l,y1.l+radius.l)
DeleteObject_(pen.l)
DeleteObject_(hPenOld.l)
EndProcedure
Procedure BoxPro (hdc.l,x1.l,y1.l,x2.l,y2.l,width.l,color.l)
pen.l=CreatePen_(#PS_SOLID,width.l,color.l)
hPenOld.l=SelectObject_(hdc.l,pen.l)
Rectangle_(hdc.l,x1.l,y1.l,x1+x2.l,y1+y2.l)
DeleteObject_(pen.l)
DeleteObject_(hPenOld.l)
EndProcedure
Procedure RoundedBoxPro (hdc.l,x1.l,y1.l,x2.l,y2.l,corner.l,width.l,color.l)
pen.l=CreatePen_(#PS_SOLID,width.l,color.l)
hPenOld.l=SelectObject_(hdc.l,pen.l)
RoundRect_(hdc.l,x1.l,y1.l,x1.l+x2.l,y1.l+y2.l,corner.l,corner.l)
DeleteObject_(pen.l)
DeleteObject_(hPenOld.l)
EndProcedure
Global actHDC.l
ProcedureC collFunc(*shapea, *shapeb, *contacts, numContacts.l, normal_coef.f, *Datas); une petite procédure bien mystérieuse...
Debug("Contacts0"+Str(numContacts))
For i = 0 To numContacts-1
*c.cpContact = *contacts+ ( SizeOf(cpContact) * i)
Debug ("Collision at "+StrF(*c\p\x)+","+StrF(*c\p\y))
Next
ProcedureReturn 1
EndProcedure
Declare display()
Declare demo_destroy()
Declare Fin()
XIncludeFile "chipMunkDemo1.pbi"
XIncludeFile "chipMunkDemo2.pbi"
XIncludeFile "chipMunkDemo3.pbi"
;XIncludeFile "chipMunkDemo4.pbi"
XIncludeFile "chipMunkDemo4b.pbi"
; XIncludeFile "chipMunkDemo5.pbi"
; XIncludeFile "chipMunkDemo6.pbi"
;XIncludeFile "chipMunkDemo7.pbi"
Global actDemo.l = 0
If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("Fehler", "Kann DirectX 7 oder höher nicht finden.", 0); c'est de l'allemand, j'ai bon ?
End
EndIf
wcpInitChipmunk() ; ... fait chaud aujourd'hui !
;Init Function
CallFunctionFast(Demos(actDemo)\start)
; °°°°°°°°°°°°°°°°°°°° fonte °°°°°°°°°°°°°°°°°°°°°°°°°°
LoadFont ( 0 , "Comic Sans Ms" ,10, #PB_Font_Bold)
Enumeration ; ici, on donne un petit nom à nos sprites( non, chuis pas un numéro !):
#fonda:#fondb
#carresr:#jauge
EndEnumeration
If OpenWindow ( 0 , 0, 0, 800, 600, "Chipmunk-Demo Press 1-4 for Demos" , #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar| #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_ScreenCentered )
;If OpenWindow(0,0,0,640,480,"Chipmunk-Demo Press 1-4 for Demos", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget)
If OpenWindowedScreen(WindowID(0),0,0,800,600, 1, 0, 0)
;If OpenScreen(640, 480, 32,"Kuchen")
CreateSprite ( #fonda , 64, 64, 0)
StartDrawing ( SpriteOutput ( #fonda ))
r=0:g=27:b=137
For t=0 To 63
Line(0, 0+t, 64, 0 , RGB (r,g,b))
Tcouleur+1
If Tcouleur=1
If r<154:r+1:EndIf
If g>0:g-1:EndIf
If b<165:b+1:EndIf
Tcouleur=0
EndIf
Next
StopDrawing ()
CreateSprite ( #fondb , 64, 64, 0)
StartDrawing ( SpriteOutput ( #fondb ))
r=0:g=27:b=137
For t=0 To 63
Line(0, 63-t, 64, 0 , RGB (r,g,b))
Tcouleur+1
If Tcouleur=1
If r<154:r+1:EndIf
If g>0:g-1:EndIf
If b<165:b+1:EndIf
Tcouleur=0
EndIf
Next
StopDrawing ()
CreateSprite ( #carresr , 16, 16, 0)
StartDrawing ( SpriteOutput ( #carresr )); carre sens de rotation
BackColor ( RGB (0,0,0))
Box (0,0, 15, 15, RGB (190, 27, 27)):Box (1,1, 13, 13, RGB (210, 27, 27))
StopDrawing ()
CreateSprite ( #jauge , 20, 20, 0)
StartDrawing ( SpriteOutput ( #jauge ))
BackColor ( RGB (0,0,0))
Box (0,0, 19, 19, RGB (255, 209, 254)):Box (2,2, 15, 15, RGB (0, 0, 0))
StopDrawing ()
Global zutA.w, zutB.w ; chuis incorrigible...
; ******************************************************************************************************
Repeat ; Boucle principale
; ******************************************************************************************************
;ClearScreen(#White )
ClearScreen(RGB(0, 27, 137))
ExamineKeyboard()
For t=0 To 12; un fond constitué de sprite2D, qu'il faudra peut être remplacer en sprite3D( car plus rapide).
DisplayTransparentSprite( #fonda , 0+(t*64), 600-64)
DisplayTransparentSprite( #fondb , 0+(t*64), 0)
Next
If KeyboardReleased(#PB_Key_1)
demo_destroy()
actDemo=0
CallCFunctionFast(Demos(actDemo)\start)
EndIf
If KeyboardReleased(#PB_Key_2)
If actDemo<> 1
demo_destroy()
actDemo=1
CallCFunctionFast(Demos(actDemo)\start)
EndIf
EndIf
If KeyboardReleased(#PB_Key_3)
If actDemo<> 2
demo_destroy()
actDemo=2
CallCFunctionFast(Demos(actDemo)\start)
EndIf
EndIf
If KeyboardReleased(#PB_Key_4)
If actDemo<> 3
demo_destroy()
actDemo=3
CallCFunctionFast(Demos(actDemo)\start)
EndIf
EndIf
; If KeyboardReleased(#PB_Key_5)
; If actDemo<> 4
; demo_destroy()
; actDemo=4
; CallCFunctionFast(Demos(actDemo)\start)
; EndIf
; EndIf
; If KeyboardReleased(#PB_Key_6)
; If actDemo<> 5
; demo_destroy()
; actDemo=5
; CallCFunctionFast(Demos(actDemo)\start)
; EndIf
; EndIf
; If KeyboardReleased(#PB_Key_7)
; If actDemo<> 6
; demo_destroy()
; actDemo=6
; CallCFunctionFast(Demos(actDemo)\start)
; EndIf
; EndIf
;
;- Alors attention, c'est là:
If actDemo=3
If KeyboardReleased ( #PB_Key_Right ) And sensrotation<2
sensrotation+0.25
EndIf
If KeyboardReleased ( #PB_Key_left ) And sensrotation>-2
sensrotation-0.25
EndIf
StartDrawing ( ScreenOutput ())
DrawingMode (1)
FrontColor ( RGB (255,255,255))
DrawingFont ( FontID (0))
FrontColor ( RGB (154,154,154)) :DrawText (150+1, 0+1, "sens de rotation:")
FrontColor ( RGB (234,234,234)) :DrawText (150, 0, "sens de rotation:")
StopDrawing ()
If sensrotation>0.24:DisplayTransparentSprite( #carresr , 400+4, 2):EndIf
If sensrotation>0.49:DisplayTransparentSprite( #carresr , 400+4+20, 2):EndIf
If sensrotation>0.74:DisplayTransparentSprite( #carresr , 400+4+20+20, 2):EndIf
If sensrotation>0.99:DisplayTransparentSprite( #carresr , 400+4+20+20+20, 2):EndIf
If sensrotation>1.24:DisplayTransparentSprite( #carresr , 400+4+20+20+20+20, 2):EndIf
If sensrotation>1.49:DisplayTransparentSprite( #carresr , 400+4+20+20+20+20+20, 2):EndIf
If sensrotation<-0.24:DisplayTransparentSprite( #carresr , 380, 2):EndIf
If sensrotation<-0.49:DisplayTransparentSprite( #carresr , 380-20, 2):EndIf
If sensrotation<-0.74:DisplayTransparentSprite( #carresr , 380-20-20, 2):EndIf
If sensrotation<-0.99:DisplayTransparentSprite( #carresr , 380-20-20-20, 2):EndIf
If sensrotation<-1.24:DisplayTransparentSprite( #carresr , 380-20-20-20-20, 2):EndIf
If sensrotation<-1.49:DisplayTransparentSprite( #carresr , 380-20-20-20-20-20, 2):EndIf
For t=0 To 5; jauge gauche.
DisplayTransparentSprite( #jauge , 278+(t*20), 0)
Next
For t=0 To 5; jauge droite.
DisplayTransparentSprite( #jauge , 402+(t*20), 0)
Next
EndIf; If actDemo=3
If KeyboardPushed ( #PB_Key_F ) And nbreis=0:nbreis+1:EndIf
If KeyboardPushed ( #PB_Key_F )=0 And nbreis=1:nbreis+1:EndIf
If KeyboardPushed ( #PB_Key_F ) And nbreis=2:nbreis+1:EndIf
If KeyboardPushed ( #PB_Key_F )=0 And nbreis=3:nbreis=0:EndIf
Gosub fps ; nombre d'image par seconde.
display()
;Delay(10); j'ai désactivé cette commande, et sans aucune pitié !
ev.l = WindowEvent()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or ev = #PB_Event_CloseWindow
EndIf
EndIf
Fin()
; ******************************************************************************************************
;- fps
fps:
If Val ( FormatDate ( "%ss" , Date ()))=sek
fps+1
Else
FPS$= Str (fps)
fps=0
EndIf
sek= Val ( FormatDate ( "%ss" , Date ()))
StartDrawing ( ScreenOutput ())
DrawingMode (1)
FrontColor ( RGB (255,255,255))
If nbreis=1 Or nbreis=2:DrawText (1,1, "FPS: " +FPS$)
vram=AvailableScreenMemory()
zutA=sensrotation
;zutB=ticks
vram$= Str (vram)
FrontColor ( RGB (154,154,154)) :DrawText (1+1,20+1, "vram: " +vram$)
FrontColor ( RGB (234,234,234)) :DrawText (1,20, "vram: " +vram$)
zutA$= Str (zutA)
FrontColor ( RGB (154,154,154)) :DrawText (1+1,120+1, "zutA: " +zutA$)
FrontColor ( RGB (234,234,234)) :DrawText (1,120, "zutA: " +zutA$)
; zutB$= Str (sensrotation)
; FrontColor ( RGB (154,154,154)) :DrawText (1+1,140+1, "zutB: " +zutB$)
; FrontColor ( RGB (234,234,234)) :DrawText (1,140, "zutB: " +zutB$)
EndIf
StopDrawing ()
Return
ProcedureC drawCollisions(*arbiter, *Datas) ; Ah oui, quand même ! J'ai chaud à la tête, toutes ces...
num.l = wcpArbiterGetNumContacts(*arbiter);
For i = 0 To num-1
wcpArbiterGetContactPoint(*arbiter, i, @vRet.cpVect )
CirclePro (actHDC,vRet\x,vRet\y,2,2, #Red)
Next
EndProcedure
Procedure drawCircleShape(*shape) ; ...nouvelles commandes innaccessibleblebleblegloubgloub, je, heu, gloubgloub, gloub... gloub.
*body.cpBody = wcpShapeGetBody(*shape); *body = pb_cpshape_getbody(*shape);
wcpBodyGetPosition(*body,@vecpos.cpVect); pb_cpbody_getposition(*body,@vecpos.cpVect)
wcpBodyGetAngle(*body,@angle.f); pb_cpbody_getangle(*body,@angle.f)
wcpCircleGetTransformedCenter(*shape,@veccenter.cpVect); pb_cpcircleshape_gettransformedcenter(*shape,@veccenter.cpVect)
wcpCircleGetRadius(*shape,@radius.f); pb_cpcircleshape_getradius(*shape,@radius.f)
Circle(veccenter\x , veccenter\y , radius, RGB(245, 245, 245))
x2.f = (radius*Cos(angle)+veccenter\x)
y2.f = (radius*Sin(angle)+veccenter\y)
LineXY(veccenter\x,veccenter\y,x2,y2,RGB(170, 245, 245))
; glVertex2f(r*Cos(rads + a) + x, r*Sin(rads + a) + y);
EndProcedure
Procedure drawPolyShape(*shape) ; gloub !
DrawingMode( #PB_2DDrawing_Outlined)
*body.cpBody = wcpShapeGetBody(*shape); *body = pb_cpshape_getbody(*shape);
wcpBodyGetPosition(*body,@vecpos.cpVect); pb_cpbody_getposition(*body,@vecpos.cpVect)
; Debug("Body pos("+StrF(vecpos\x)+","+Str(vecpos\y))
vertnum.l = wcpPolyShapeGetNumVerts(*shape); pb_cppolyshape_numverts(*shape)
Dim verts.cpVect(vertnum+1)
wcpPolyShapeGetVerts(*shape, @verts()); pb_cppolyshape_getverts(*shape, @verts());
first.cpVect;
last.cpVect;
last.cpVect
wcpBodyGetRotation(*body, @bodyRot.cpVect)
For i = 0 To vertnum-1
wcpVectRotate( verts(i), bodyRot, rot)
wcpVectAdd( vecpos, rot, v)
; pb_cpvect_add2(@v.cpVect,@vecpos, pb_cpvect_rotate(@verts(i), pb_cpbody_getrot(*body)) )
If i = 0
first\x = v\x
first\y = v\y
Else
LineXY(last\x,last\y,v\x,v\y, RGB(245, 245, 245));#Black)
; LinePro (actHDC.l,last\x,last\y,v\x,v\y,3,#Black)
EndIf
last\x = v\x
last\y = v\y
Next
LineXY(last\x,last\y,first\x,first\y, RGB(245, 245, 245));#Black)
Circle(vecpos\x,vecpos\y,2, RGB(170, 245, 245));#Blue)
; CirclePro (actHDC,vecpos\x,vecpos\y,2,2,#Blue)
EndProcedure
Procedure drawSegmentShape(*shape) ; et désormais il reposa en paix...
*body.cpBody = wcpShapeGetBody(*shape); *body = pb_cpshape_getbody(*shape);
wcpBodyGetPosition(*body,@vecpos.cpVect); pb_cpbody_getposition(*body,@vecpos.cpVect)
wcpBodyGetRotation(*body, @bodyRot.cpVect)
; *body = pb_cpshape_getbody(*shape);
; *seg = *shape;
; pb_cpbody_getposition(*body,@vecpos.cpVect)
wcpSegmentShapeGetPointA(*shape,pointA.cpVect)
wcpSegmentShapeGetPointB(*shape,pointB.cpVect)
wcpVectRotate( pointA, bodyRot, rota)
wcpVectRotate( pointB, bodyRot, rotb)
wcpVectAdd( vecpos, rota, veca)
wcpVectAdd( vecpos, rotb, vecb)
; pb_cpvect_add2(@veca.cpVect,@vecpos, pb_cpvect_rotate( pb_cpsegmentshape_getendpointa(*seg), pb_cpbody_getrot(*body)) )
; pb_cpvect_add2(@vecb.cpVect,@vecpos, pb_cpvect_rotate( pb_cpsegmentshape_getendpointb(*seg), pb_cpbody_getrot(*body)) )
LineXY(veca\x, veca\y, vecb\x,vecb\y, RGB(245, 245, 170));#Black)
EndProcedure
ProcedureC drawObject(*shape, *Datas) ; ... dans sa dernière demeure...
; Debug("draw")
type.l = wcpShapeGetType(*shape); pb_cpshape_gettype(*shape)
Select type
Case #CP_CIRCLE_SHAPE
drawCircleShape(*shape)
; Debug("Circle")
Case #CP_SEGMENT_SHAPE
drawSegmentShape(*shape)
; Debug("Segment")
Case #CP_POLY_SHAPE
drawPolyShape(*shape)
; Debug("Poly")
EndSelect
EndProcedure
Procedure display() ; ...dans de vastes prairies et sous de frais ombrages...
Shared *space, ticks.l
Shared Demos()
ticks+1
actHDC = StartDrawing(ScreenOutput())
wcpSpaceHashEach( wcpSpaceGetActiveShapes(*space), @drawObject(), 0)
wcpSpaceHashEach( wcpSpaceGetStaticShapes(*space), @drawObject(), 0)
; cpSpaceHashEach(pb_cpspace_getactiveshapes(*space), @drawObject(), 0)
; cpSpaceHashEach(pb_cpspace_getstaticshapes(*space), @drawObject(), 0)
wcpSpaceEachArbiter(*space, @drawCollisions(), #Null)
StopDrawing()
CallFunctionFast(Demos(actDemo)\update,ticks)
EndProcedure
Procedure demo_destroy() ; ...où il n'éprouva nulle lassitude et ne voudra rien changer...
Shared *space, *staticBody
; cpSpaceFreeChildren(*space);
wcpSpaceFree(*space);
wcpBodyFree(*staticBody);
EndProcedure
Procedure.l Fin()
If IsSprite(#fonda):FreeSprite(#fonda):EndIf
If IsSprite(#fondb):FreeSprite(#fondb):EndIf
If IsSprite(#carresr):FreeSprite(#carresr):EndIf
If IsSprite(#jauge):FreeSprite(#jauge):EndIf
End ; et c'est toujours un peu triste...
EndProcedure
; jaPBe Version=3.7.10.669
; Build=4
; Language=0x0000 Language Neutral et mon pb d'amour ^.^
; FirstLine=65
; CursorPosition=86
; EnableXP
; EnableOnError
; ExecutableFormat=Windows
; Executable=T:\Dev\PB4\Includes\Examples\chipmunkDemo.exe
; DontSaveDeclare