2d balls smacking together
BlitzMax Forums/BlitzMax Programming/2d balls smacking together
| ||
I'm making a little mini game... In this mini game balls bounce around the screen and collide with eachother. There is no gravity so they continue on. I am little confused by what exactly needs to happen to make the rebounding accurate.. What do you do exactly? Add their vectors to eachother? If ball 1 is going in the direction (1.5,1.5) and ball 2 in a collision path going in the direction (-0.75,-0.75) what happens to their vectors when they collide? |
| ||
Slow the speed and invert the directions? It could extend to checking collision angles and appropriating the next direction (I don't know exactly how to do that though). |
| ||
... Thats where vectors come in... I'm pretty sure theres a way to calculate the new speed/direction(vector) if you have the size of both circle and both vectors |
| ||
I did find this... But I'm not sure what to make of it. |
| ||
V2' = sqrt((cos θ * V2)^2 + (sin β * V1)^2)) V1' = sqrt((cos θ * V1)^2 + (sin β * V2)^2)) Where: θ is the angle between the force vector of ball1 and the V1 direction vector, and β is the angle between the force vector of ball2 and the V2 direction vector. I'm going to run a couple tests and see if I can get this working in bmax with my vector module. Whats this force vector? |
| ||
I went through this tutorial when I was trying to learn vector math: http://tonypa.pri.ee/vectors/tut01.html it is in flash but easy to understand. I myself don't know flash but was able to understand the concept really easy. I created a little demo based on some stuff I learned from there.you can download it from my signature link below. It is in the demo Section called 2d ball physics&source |
| ||
That helps alot Jesse! so I still am trying to figure this out... can someone help me with this below: Ball collision(both have equal mass) procedure: 1. Constantly measure the abs distance between the centers of the balls 2. If abs distance < sum of radii then a collision has occured 3. the normal of the collision is perpendicular to the line that connects the two balls ?4. Ball #1's new vector is a result somehow of v1b and v2a ?5. Ball #2's new vector is a result somehow of v1a and v2b |
| ||
see if this helps you ob is ball 1 and ob2 is ball 2 they are both vectors newv=bounce(ob2, ob) ob.vx=newv.vx1 'new direction of ball 1 ob.vy=newv.vy1 ob2.vx=newv.vx2 'new direction of ball 2 ob2.vy=newv.vy2 Function bounce:vector2d(v2:vector2d, v:vector2d) Local proj11:vector2d=projectVector(v, v.dx, v.dy) 'projection of v1 on v Local proj12:vector2d=projectVector(v, v.lx, v.ly) 'projection of v1 on v normal Local proj21:vector2d=projectVector(v2, v.dx, v.dy) 'projection of v2 on v Local proj22:vector2d=projectVector(v2, v.lx, v.ly) 'projection of v2 on v normal Local P#= v.m * proj11.vx + v2.m * proj21.vx; Local Vn#=proj11.vx-proj21.vx; Local v2fx#=(P+Vn*v.m)/(v.m+v2.m); Local v1fx#=v2fx-Vn; P#=v.m*proj11.vy+v2.m*proj21.vy; Vn#=proj11.vy-proj21.vy; Local v2fy#=(P+Vn*v.m)/(v.m+v2.m); Local v1fy#=v2fy-Vn; Local proj:vector2d = New vector2d 'add the projections For v1 proj.vx1=proj12.vx+v1fx; proj.vy1=proj12.vy+v1fy; 'add the projections For v2 proj.vx2=proj22.vx+v2fx; proj.vy2=proj22.vy+v2fy; Return proj End Function ' project vector v1 on unit-sized vector dx/dy Function projectVector:vector2d (v:vector2d, dx#, dy#) ' find dot product Local dp# = v.vx*dx+v.vy*dy; Local proj:vector2d = New vector2d ' projection components proj.vx = dp*dx; proj.vy = dp*dy; Return proj; End Function |
| ||
I found this in my files maybe this can help you: |
| ||
Yeah I finally got it after re-reading the resource you linked... Thanks for all the help guys... Seems the hardest part of this minigame is done now! |
| ||
Odd... The balls seem to get on a straight x/y path alot when they are hit by another ball... Which the chances are really low that this could happen? Does anyone know why the balls behave like this looking at the source below? |
| ||
change rand to rnd. |