Physics of 2d object collisions using f=ma???
Blitz3D Forums/Blitz3D Beginners Area/Physics of 2d object collisions using f=ma???
Hi,
I need help. Does anyone know of any articles that describe how to calculate the velocities of 2 collided bodies taking account of their objects mass, etc?
I.e. similar to colliding asteroids in space.
Cheers,
Ralphy.
Thanks Rob.
I think the bumper car section has all the info I need.
Just a guess:
if a collide with b then:
vb=vb+( (va-vb) on (a to b) )*2*(mass a)/( (mass a)+(mass b) )
va=va+( (vb-va) on (b to a) )*2*(mass b)/( (mass b)+(mass a) )
va and vb is the movement vectors. I hope you know vector math
Anyway, just a wild guess ;)
Edit: Oh by the way, while I keep guessing, I guess this, if true, should only apply to completely hard balls, or circles in 2D.
Velocities is usually best calculated with momentum and kinetic energy equations...
mv = momentum
(m(v^2))/2 = K.E.
In a system, the total energy will be preserved.
KE1a + KE1b = ( KE2a + KE2b )
Malice... not really...
If the collision is elastic, both momentum and KE is conserved.
If the collision is inelastic, momentum is conserved whilst KE is not.
In other words, momentum is always conserved, KE is sometimes conserved.
I assume though that in a game-world you'll have all collisions be elastic. Therefore it'll be easy to calculate the resultant velocities as both KE and momentum is conserved.
Using the conservation of momentum equation worked great (thanks Rob for the myphysicslab link).
If anyone is interested, this is the code I ended up with and it works perfectt: (debris is object1, others is object2)
vcmy = ( (debris\mass*debris\vy) + (others\mass*others\vy) ) / (debris\mass+others\mass)
debris\vy = -(debris\vy - vcmy) + vcmy
debris\vx = -(debris\vx - vcmx) + vcmx
vcmx = ( (debris\mass*debris\vx) + (others\mass*others\vx) ) / (debris\mass+others\mass)
others\vx = -(others\vx - vcmx) + vcmx
others\vy = -(others\vy - vcmy) + vcmy
Whoa, you guys rock, I know nothing about physics.. pretty sad actually since 99% of games you create need physics.
Although I am thoroughly impressed with the brain power on this forum.
Kudos!
Ken
Using kinetic energy doesn't work - you have the *magnitude* of their velocities, but not their velocities themselves!
Ralphy,
debris\vx = -(debris\vx - vcmy) + vcmy
is just the same as
debris\vx = -debris\vx
Obviously, I haven't seen all your code here but all you seem to be acheiving is making the debris and the object bounce off each other with the reverse of their current velocity. The Mass of the debris or other should make no difference?
If it seems to work for you .....
Stevie, actually it's the same as
2 * vcmy - debris\vx
ralphy, your method doesn't seem to be totally correct, here is an example where it is quite obvious that something is wrong:
SeedRnd(1)
Graphics 1024,768
Const method=2
Const gravon=0
SetBuffer BackBuffer()
Const ballno=1;99
;Const gravg#=0.0001
Dim x#(ballno)
Dim y#(ballno)
Dim dx#(ballno)
Dim dy#(ballno)
Dim mass#(ballno)
Dim radius#(ballno)
For a=0 To ballno
x(a)=Rand(512-50-50+25,512+50+50-25)
y(a)=Rand(384-50-50+25,384+50+50-25)+250
dx(a)=Rnd(-1,1)
dy(a)=0;Rnd(-1,1)
mass(a)=1
radius(a)=20;mass(a)^(1/2.0)*1
Next
mass(0)=1
x(0)=200
dx(0)=.5
y(0)=339
x(1)=600
dx(1)=-.5
y(1)=300
mass(1)=1
While Not KeyHit(1)
For c=0 To 3
For a=0 To ballno
x(a)=x(a)+dx(a)
y(a)=y(a)+dy(a);+.0005
dy(a)=dy(a);+.001
If x(a)<10 And dx(a)<0 Then dx(a)=-dx(a)
; If y(a)<10 And dy(a)<0 Then dy(a)=-dy(a)
If x(a)>1013 And dx(a)>0 Then dx(a)=-dx(a)
If y(a)>757-50 And dy(a)>0 Then dy(a)=-dy(a)
Next
For a=0 To ballno-1
For b=a+1 To ballno
dist#=(x(a)-x(b))^2+(y(a)-y(b))^2
sqrdist#=Sqr(dist)*1000
If gravon=1 Then
dx(a)=dx(a)+(x(b)-x(a))*mass(b)/dist/sqrdist
dy(a)=dy(a)+(y(b)-y(a))*mass(b)/dist/sqrdist
dx(b)=dx(b)+(x(a)-x(b))*mass(a)/dist/sqrdist
dy(b)=dy(b)+(y(a)-y(b))*mass(a)/dist/sqrdist
End If
distmin=(radius(a)+radius(b))^2
If dist<distmin Then
If (x(a)+dx(a)-x(b)-dx(b))^2+(y(a)+dy(a)-y(b)-dy(b))^2<dist Then
If method=1 Then
atobx#=x(b)-x(a)
atoby#=y(b)-y(a)
lenatob2#=atobx^2+atoby^2
damdbx#=dx(a)-dx(b)
damdby#=dy(a)-dy(b)
factora#=-(damdbx*atobx+damdby*atoby)/lenatob2
factorb#=(damdbx*atobx+damdby*atoby)/lenatob2
aonatobx#=atobx*factora
aonatoby#=atoby*factora
bonatobx#=atobx*factorb
bonatoby#=atoby*factorb
dx(a)=dx(a)+aonatobx*mass(b)/(mass(a)+mass(b))*2
dy(a)=dy(a)+aonatoby*mass(b)/(mass(a)+mass(b))*2
dx(b)=dx(b)+bonatobx*mass(a)/(mass(a)+mass(b))*2
dy(b)=dy(b)+bonatoby*mass(a)/(mass(a)+mass(b))*2
Else
vcmy# = ( (mass(a)*dy(a)) + (mass(b)*dy(b)) ) / (mass(a)+mass(b))
vcmx# = ( (mass(a)*dx(a)) + (mass(b)*dx(b)) ) / (mass(a)+mass(b))
dy(a) = -(dy(a)) + vcmy + vcmy
dx(a) = -(dx(a)) + vcmx + vcmx
dx(b) = -(dx(b)) + vcmx + vcmx
dy(b) = -(dy(b)) + vcmy + vcmy
End If
; While (x(a)+dx(a)-x(b)-dx(b))^2+(y(a)+dy(a)-y(b)-dy(b))^2<dist
; x(a)=x(a)+dx(a)/2
; y(a)=y(a)+dy(a)/2+.0025
; dy(a)=dy(a)+.005
; x(b)=x(b)+dx(b)/2
; y(b)=y(b)+dy(b)/2+.0025
; dy(b)=dy(b)+.005
; Wend
End If
End If
If gravon=1 Then
dx(a)=dx(a)+(x(b)-x(a))*mass(b)/dist/sqrdist
dy(a)=dy(a)+(y(b)-y(a))*mass(b)/dist/sqrdist
dx(b)=dx(b)+(x(a)-x(b))*mass(a)/dist/sqrdist
dy(b)=dy(b)+(y(a)-y(b))*mass(a)/dist/sqrdist
End If
Next
Next
Next
Cls
For a=0 To ballno
Oval x(a)-radius(a),y(a)-radius(a),radius(a)*2,radius(a)*2
Next
Flip
Wend