## METAL: Circle collisions

Nibbie
Posts: 1
Joined: 2010.11
Post: #16
Swap velocities but do something like this:
Code:
`if sprite collides(ball1,ball2) then swap b1xs*.9,b2xs  : swap b1ys*.9,b2ys`
so you loose some speed.
also maybe you should have a max speed you can hit the ball with. If you have 2 screens like I do you can drag the mouse way to far away and it wizzes around until at least 1 ball go's in the hole.
Member
Posts: 30
Joined: 2002.10
Post: #17
Here's a simple code:

Assuming the vx1,vy1 and vx2,vy2 are the horizontal and vertical speeds of the objects,

if theycollide then

swap vx1,vx2
swap vy1,vy2

endif

Now don't forget that the balls should loose speed all the time (or they will move til the end of days!). Use something like

vx1=vx1*0.9
vy1=vy1*0.9

in your mainloop. Experiment with the value (0.9) to get it looking realistic.

By the way, you got the code of FLOWMOTION, check that, there the blocks also swap speed, but they don't get slower.

Hope that helped.
Member
Posts: 509
Joined: 2002.05
Post: #18
That works great for boxes or rectangles, but for a pool game those physics don't cut it. Do you know what I mean kemal?
Moderator
Posts: 916
Joined: 2002.10
Post: #19
for elastic collisions
Code:
```            if(Hit(b1,b2))    //if hit, bounce them             {                 double n[2]={b1->X-b2->X,b1->Y-b2->Y};                 double v1[2]={b1->XV,b1->YV};                 double v2[2]={b2->XV,b2->YV};                 double v3[2];                 double v4[2];                 double temp[2];                 double m=magnitude(n);                 double a1,a2;                 double optp;                 n[0]/=m;    //normalize n                 n[1]/=m;                 a1=dot(v1,n,2);                 a2=dot(v2,n,2);                 optp=(2.0*(a1-a2))/(2*circmass);                 temp[0]=optp*circmass*n[0];temp[1]=optp*circmass*n[1];                 v3[0]=v1[0]-temp[0];v3[1]=v1[1]-temp[1];                 v4[0]=v2[0]+temp[0];v4[1]=v2[1]+temp[1];                 b1->XV=v3[0];b1->YV=v3[1];                 b2->XV=v4[0];b2->YV=v4[1];             }```

yes I am aware there are a lot of unneccesary variable defining in there.
Member
Posts: 509
Joined: 2002.05
Post: #20
I don't program in C, but will that code work for a pool game (are the physics correct, or is that just another way to get it to kinda work)
Member
Posts: 30
Joined: 2002.10
Post: #21
You're right, my example works only for square objects.
When the objects are round, the angle has to be considered.
What I can think of, without knowing the correct physics, is:

Draw a line between the two centers of the balls. This is the angle which has to be added to the angle of the movement of both balls I think.

In order to calculate this, you have to get away from the simple x-speed and y-speed rule and use a total speed- and an angle-parameter. Then you can switch the angles and speeds when the balls collide, detect the hit-angle (from the centers of the balls) and add that to both angles. That should work fine.

But, other idea: I think there are a lot of standard-pool-games outside. Why not do one with square-balls? "Squool"? Maybe I'll adapt Flowmotion to that, should be very easy indeed.
Member
Posts: 509
Joined: 2002.05
Post: #22
I see what you mean by the angel/velocity oposed to x-y velocitys, but I have already came up with a solution to the problem. Here is what I have with ball i and j colliding.

balls(i,1) is the x location
balls(i,2) is the y location
balls(i,3) is the x speed
balls(i,4) is the y speed
v(i,whatever) = the new speed of the ball (added at the end of the loop)

This code works actualy pretty good, the pool game is realistic and you can cut balls and give them specific angles, just like in real life.

Code:
```ix = balls(i,1)                   jx = balls(j,1)                   iy = balls(i,2)                   jy = balls(j,2)                   v(i,3) = v(i,3) + ((ix-jx)/16)*sqr(balls(j,3)^2+balls(j,4)^2)+.05+((iy-jy)/16)*balls(i,3)                   v(i,4) = v(i,4) + ((iy-jy)/16)*sqr(balls(j,3)^2+balls(j,4)^2)+.05+((ix-jx)/16)*balls(i,4)                   v(j,3) = v(j,3) + ((jx-ix)/16)*sqr(balls(i,3)^2+balls(i,4)^2)+.05+((jy-iy)/16)*balls(j,3)                   v(j,4) = v(j,4) + ((jy-iy)/16)*sqr(balls(i,3)^2+balls(i,4)^2)+.05+((jx-ix)/16)*balls(j,4)```
Member
Posts: 30
Joined: 2002.10
Post: #23
That's good!
I'm currently making the FLOWMOTION game with music, sound and gfx assets. It's really cool.
What will you do with the Pool-game when finished?
Member
Posts: 509
Joined: 2002.05
Post: #24
I don't know what I am going to do with it when Im finished. I might add some trick shots and give it away as freeware on my site.

Are you going to make your new game shareware kemal?