Posted: Mon Dec 26, 2005 8:28 am Post subject: (No subject)
Use Trig functions (sin, cosin, etc.)
Cervantes
Posted: Mon Dec 26, 2005 3:45 pm Post subject: (No subject)
A collision occurs between two circles if the distance between their centres is less than or equal to the sum of their radii. To determine the distance between their centres, use Math.Distance.
code:
if Math.Distance (circle1.x, circle1.y, circle2.x, circle2.y) <= circle1.radius + circle2.radius then
%collision
end if
Math.Distance requires Turing v4.0.5 or newer. If you don't have this, you'll have to make your own distance function, which is very easy.
Posted: Mon Dec 26, 2005 7:22 pm Post subject: (No subject)
BUT to make things interesting, notice that if they're moving fast enough just checking their distance apart every frame won't be good enough - they'll pass right through each other.
Tony
Posted: Mon Dec 26, 2005 8:30 pm Post subject: (No subject)
Martin -- I don't remember how you ended up fixing that issue. Do you remember where the thread was?
I don't have the source anymore, unfortunately. I think I'll rewrite it this weekend though.
Saad85
Posted: Mon Dec 26, 2005 10:45 pm Post subject: (No subject)
k, obviously i wasn't clear enough. i know how to tell if they've collided, how do i make 2 circles bounce off each other as they would in real life (instead of just reversing x and y velocities)
this is my attempt at it.. it didnt go too well so dont bother analysing my code in detail. the basic idea is to store the previous frame's coordinates and use that to find angle of incidence, which i then use to find the next frame's coordinates. this i then use to find velocities. my code is obviously flawed in more ways than 1 so i wont bother going into detail about it.
code:
setscreen("offscreenonly")
var defang:real %angle after deflection
%big circle
var c1x,c1y:real:=200
var c1vx,c1vy:real
var c1r:int:=70
%small circle
var c2px,c2py,prevd:real% < previous
var c2x,c2y:real:=100
var c2vx, c2vy:real
var c2r:int:=20
var c2nx,c2ny:real% < next
procedure movement
% draw
drawfilloval(round(c1x),round(c1y),c1r,c1r,red)
drawfilloval(round(c2x),round(c2y),c2r,c2r,blue)
c2px:=c2x
c2py:=c2y
c1x+=c1vx
c1y+=c1vy
if c1x>=maxx-c1r then c1vx:=-1*abs(c1vx) c1x:=maxx-c1r end if
if c1x<=c1r then c1vx:=abs(c1vx) c1x:=c1r end if
if c1y>=maxy-c1r then c1vy:=-1*abs(c1vy) c1y:=maxy-c1r end if
if c1y<=c1r then c1vy:=abs(c1vy) c1y:=c1r end if
c2x+=c2vx
c2y+=c2vy
if c2x>=maxx-c2r then c2vx:=-1*abs(c2vx) c2x:=maxx-c2r end if
if c2x<=c2r then c2vx:=abs(c2vx) c2x:=c2r end if
if c2y>=maxy-c2r then c2vy:=-1*abs(c2vy) c2y:=maxy-c2r end if
if c2y<=c2r then c2vy:=abs(c2vy) c2y:=c2r end if
end movement
procedure collision
if sqrt( (c2x-c1x)**2 + (c2y-c1y)**2)<=c1r+c2r then %<----collision
defang:=arctand((c2y-c1y)/(c2x-c1x))-arctand((c2py-c1y)/(c2px-c1x))+arctand((c2y-c1y)/(c2x-c1x))
prevd:=sqrt((c2px-c1x)**2 + (c2py-c1y)**2)
if c2x>=c1x and c2y>=c1y then
defang+=0
elsif c2x<c1x and c2y>c1y then
defang:=180-defang
elsif c2x<=c1x and c2y<=c1y then
defang:=180+defang
elsif c2x>c1x and c2y<c1y then
defang:=0-defang
end if
if anyone would help me out with this, it would be appreciated
Sponsor Sponsor
Saad85
Posted: Mon Dec 26, 2005 10:51 pm Post subject: (No subject)
sorry for double post, but where it says
c2vx:=(c2x-c2nx)/15
c2vy:=(c2y-c2ny)/15
i did not (at first) include the /15 in there but for some reason the balls start going REALLY fast if its not in there so i just did it to slow them down (couldnt find the problem)
[Gandalf]
Posted: Mon Dec 26, 2005 11:43 pm Post subject: (No subject)
Re-read Cervantes' last post, with an emphasis on the ending.
Martin
Posted: Tue Dec 27, 2005 12:40 am Post subject: (No subject)
Posted: Tue Dec 27, 2005 11:24 am Post subject: (No subject)
Guys your are excluding Andy completely from this topic! What about the whatdotcolor collision, not everyone has 4.0.5
md
Posted: Tue Dec 27, 2005 11:58 am Post subject: (No subject)
why would you use whatdotcolor when the mathematical way is so much better? Especially when you don't need 4.05 (at least I'm fairly certain you don't... math functions are present in all version IIRC; though perhaps not vector stuff).
Cervantes
Posted: Tue Dec 27, 2005 10:20 pm Post subject: (No subject)
Agreed. whatdotcolour is assuredly not the way to go. Turing v4.0.5 has the Math.Distance function which is useful to this, though ideally you would heed Martin's comments and do some more advanced math. Regardless, Turing v4.0.5 is not necessary for the mathematical approach.
MysticVegeta
Posted: Wed Dec 28, 2005 3:23 pm Post subject: (No subject)
what about versions 4.0.5 less, they dont have Math.Distance...