http://d.krauss.free.fr/documents/Physi ... _chocs.htm
En ce moment, je le décortique et je compte bien l'enrichir !
(il y a une question à la fin

Pour l'instant, une collision élastique entre deux corps de tailles et de masses différentes.
Ca à l'air tout bête, ben je me suis amusé à refaire les calculs(fallait bien vérifier

Un exemple d'évidence qui m'était passée sous le nez : pour que deux corps se touchent, il faut qu'ils se rapprochent

Code : Tout sélectionner
If (x1-x2)*(vx1-vx2)+(y1-y2)*(vy1-vy2)<0
Code : Tout sélectionner
;collision_1
;auteur Huitbit
;pb v4.20
;*********************************
#largeur_ecran=1024
#hauteur_ecran=768
;boule n°1
#r_1=128
#m_1=4
x_1.f : y_1.f=100
x_1_centre.f=x_1+#r_1 : y_1_centre.f=y_1+#r_1
vx_1.f=4 : vy_1.f=2
;boule n°2
#r_2=16
#m_2=2
x_2.f=600 : y_2.f=300
x_2_centre.f=x_2+#r_2 : y_2_centre.f=y_2+#r_2
vx_2.f=-2 : vy_2.f=2
#d_carre=(#r_1+#r_2)*(#r_1+#r_2)
#beta=2.0*#m_2 /((#m_1+#m_2)*#d_carre)
alpha.f
test.s=""
Enumeration
#spr_2
#spr_1
EndEnumeration
Macro test_variation_distance(x1c,y1c,vx1,vy1,x2c,y2c,vx2,vy2)
If (x1c-x2c)*(vx1-vx2)+(y1c-y2c)*(vy1-vy2)<0
test="Rapprochement"
Else
test="Eloignement"
EndIf
EndMacro
Macro test_contact(x1c,y1c,vx1,vy1,x2c,y2c,vx2,vy2)
If (x1c-x2c)*(x1c-x2c)+(y1c-y2c)*(y1c-y2c)<=#d_carre And test="Rapprochement"
contact=1
alpha=#beta*((x2c-x1c)*(vx2-vx1)+(y2c-y1c)*(vy2-vy1))
vx1=vx1+alpha*(x2c-x1c)
vy1=vy1+alpha*(y2c-y1c)
vx2=vx2-alpha*#m_1/#m_2*(x2c-x1c)
vy2=vy2-alpha*#m_1/#m_2*(y2c-y1c)
Else
contact=0
EndIf
EndMacro
Macro test_bord_ecran(xc,yc,vx,vy,r)
If xc+r>#largeur_ecran Or xc<r
vx=-vx
EndIf
If yc+r>#hauteur_ecran Or yc<r
vy=-vy
EndIf
EndMacro
InitSprite()
InitKeyboard()
OpenWindow(0,0,0,#largeur_ecran,#hauteur_ecran,"Collision_2D",#PB_Window_ScreenCentered|#PB_Window_SystemMenu )
OpenWindowedScreen(WindowID(0),0,0,#largeur_ecran,#hauteur_ecran,0,0,0)
CreateSprite(#spr_2,#r_2*2,#r_2*2)
StartDrawing(SpriteOutput(#spr_2))
DrawingMode(#PB_2DDrawing_Outlined)
Circle(#r_2,#r_2,#r_2,RGB(0,255,0))
Plot(#r_2,#r_2,RGB(255,255,255))
StopDrawing()
CreateSprite(#spr_1,#r_1*2,#r_1*2)
StartDrawing(SpriteOutput(#spr_1))
DrawingMode(#PB_2DDrawing_Outlined)
Circle(#r_1,#r_1,#r_1,RGB(255,0,0))
Plot(#r_1,#r_1,RGB(255,255,255))
StopDrawing()
Repeat
Repeat
Event = WindowEvent()
If Event = #PB_Event_CloseWindow
End
EndIf
Until Event = 0
x_1_centre=x_1_centre+vx_1
y_1_centre=y_1_centre+vy_1
x_2_centre=x_2_centre+vx_2
y_2_centre=y_2_centre+vy_2
test_variation_distance(x_1_centre,y_1_centre,vx_1,vy_1,x_2_centre,y_2_centre,vx_2,vy_2)
test_contact(x_1_centre,y_1_centre,vx_1,vy_1,x_2_centre,y_2_centre,vx_2,vy_2)
test_bord_ecran(x_1_centre,y_1_centre,vx_1,vy_1,#r_1)
test_bord_ecran(x_2_centre,y_2_centre,vx_2,vy_2,#r_2)
ClearScreen(RGB(0,0,0))
DisplayTransparentSprite(#spr_1,x_1_centre-#r_1,y_1_centre-#r_1)
DisplayTransparentSprite(#spr_2, x_2_centre-#r_2,y_2_centre-#r_2)
StartDrawing(ScreenOutput())
DrawText(0,0,test)
StopDrawing()
Delay(1)
FlipBuffers()
ForEver

Le contact peut-être amélioré puisque selon la vitesse les boules peuvent s'interpénétrer. En fait, il faudrait replacer les boules à la distance r1+r2 à chaque contact.
Question : Le résultat du code ci-dessous est-il normal ?
Code : Tout sélectionner
;boule n°1
#r_1=128
#m_1=4
;boule n°2
#r_2=16
#m_2=2
#d_carre=(#r_1+#r_2)*(#r_1+#r_2)
betabis.d=2*#m_2 /((#m_1+#m_2)*#d_carre)
Debug betabis
#beta=2*#m_2 /((#m_1+#m_2)*#d_carre)
Debug #beta