Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 circular collision
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Saad85




PostPosted: Mon Dec 26, 2005 12:39 am   Post subject: circular collision

sorry if its come up before, but how would i go about creating a program that mimics circular collision/angular collision?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Dec 26, 2005 2:22 am   Post subject: (No subject)

You start by reading the [Turing Tutorials], in order of Turing Walkthrough
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Albrecd




PostPosted: Mon Dec 26, 2005 8:28 am   Post subject: (No subject)

Use Trig functions (sin, cosin, etc.)
Cervantes




PostPosted: 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.

As for the motion of the circles after collision, you should examine this source code, by thoughtful.
Martin




PostPosted: 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




PostPosted: 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Martin




PostPosted: Mon Dec 26, 2005 8:44 pm   Post subject: (No subject)

http://www.compsci.ca/v2/viewtopic.php?t=9706

I don't have the source anymore, unfortunately. I think I'll rewrite it this weekend though.
Saad85




PostPosted: 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
 
  c2nx:=c1x+prevd*cos(defang)
  c2ny:=c1y+prevd*sin(defang)
 
  c2vx:=(c2x-c2nx)/15%      <  new velocities
  c2vy:=(c2y-c2ny)/15%      <
   
 end if
end collision


c1vx:=Rand.Int(-1,1)%<----------------|
c1vy:=Rand.Int(-1,1)%                 |
%                                     |--circle velocities
c2vx:=Rand.Int(1,4)%                  |
c2vy:=Rand.Int(1,4)%<-----------------|
loop
 movement
 collision
 
 View.Update
 cls
 delay(15)
end loop



if anyone would help me out with this, it would be appreciated
Sponsor
Sponsor
Sponsor
sponsor
Saad85




PostPosted: 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]




PostPosted: Mon Dec 26, 2005 11:43 pm   Post subject: (No subject)

Re-read Cervantes' last post, with an emphasis on the ending.
Martin




PostPosted: Tue Dec 27, 2005 12:40 am   Post subject: (No subject)

For the actual collisions -
http://en.wikipedia.org/wiki/Elastic_collision
MysticVegeta




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: Wed Dec 28, 2005 3:23 pm   Post subject: (No subject)

what about versions 4.0.5 less, they dont have Math.Distance...
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: