Circular Collision
Author |
Message |
Random
|
Posted: Sun Nov 07, 2004 1:03 am Post subject: Circular Collision |
|
|
Well, here is my pitiful attempt to recreate balls bouncing off eachother. I read the tutorials on circular collision and I trying using concepts I thought I learned from thoughfuls tutorial. Im not as smart as i though. Anyways, sometimes the balls will bounce off right, but more often than not they will go into eachother and do some wierd shaking and come out and a wierd angle. I understand trig allright and I kinda understand how it is used here, but I cant get this thing working.
Also, i havnt applied any kinematic equasions or whatever ill end up using to determind finishing velocity because im just trying to get the angles right. If you want me to explain any of my code ask but i hope the comments help. Anyway, any help you can give would be great. Thanks in advance.
code: |
type ball :
record
mass : int %this is basically the radius. I'll use it later when I put in some momentum equasions from physics.
x, y, xv, yv : real %x and y is the position / xv and yv are the x and y velocities (respectivly)
end record
var ball1, ball2 : ball %by two balls
var tangent : real
ball1.x := 400 %ball 1's info
ball1.y := 300
ball1.mass := 30
ball1.xv := 3
ball1.yv := -4
ball2.x := 200 %ball 2's info
ball2.y := 200
ball2.mass := 30
ball2.xv := -1
ball2.yv := -2
View.Set ("offscreenonly")
loop
%i like drawing them in white instead of cls... i guess old habits die hard. This is how we learned it b4 i figured out
%turing has a buffer you could draw to.
drawfilloval (round (ball1.x), round (ball1.y), ball1.mass, ball1.mass, white) %ball 1 in white
drawfilloval (round (ball2.x), round (ball2.y), ball2.mass, ball2.mass, white) %ball 2 in white
%increase the balls by their respected velocities.
ball1.x += ball1.xv
ball1.y += ball1.yv
ball2.x += ball2.xv
ball2.y += ball2.yv
%redraw the balls
drawfilloval (round (ball1.x), round (ball1.y), ball1.mass, ball1.mass, green)
drawfilloval (round (ball2.x), round (ball2.y), ball2.mass, ball2.mass, blue)
View.Update
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ANGLE DETECTION... I CANT GET THIS...%%%%%%%%%%%%%%%%%
if Math.Distance (ball1.x, ball1.y, ball2.x, ball2.y) <= ball1.mass + ball2.mass then
%gets the angle
tangent := arctand ((ball2.y - ball1.y) / (ball2.x - ball1.x))
%i pretty much copied this idea outta thoughtfuls tut i think... I havnt included any mass or previous speed yet, im
%just trying to get the collision angles right. My theory is that since such a small number is being returned
%the balls will end up increasing by 1 all the time because the new velocities are gonna be so low. Any help is
%appreciated.
ball1.xv := sind (tangent)
ball1.yv := cosd (tangent)
ball2.xv := -sind (tangent)
ball2.yv := -cosd (tangent)
%sometimes it will look like it work right but more often then not, the balls will go through each other and
%come out at wierd messed up angles. Any help or pointers is greatly appreciated.
end if
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%END ANGLE DETECTION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%So Ball 1 doesnt go off the screen
if ball1.y + ball1.mass > maxy or ball1.y - ball1.mass < 0 then
ball1.yv := -ball1.yv
end if
if ball1.x + ball1.mass > maxx or ball1.x - ball1.mass < 0 then
ball1.xv := -ball1.xv
end if
%So Ball 2 doesnt go off the screen
if ball2.y + ball2.mass > maxy or ball2.y - ball2.mass < 0 then
ball2.yv := -ball2.yv
end if
if ball2.x + ball2.mass > maxx or ball2.x - ball2.mass < 0 then
ball2.xv := -ball2.xv
end if
end loop
View.Set ("nooffscreenonly")
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sun Nov 07, 2004 6:40 am Post subject: (No subject) |
|
|
Well, this is pretty simple. You just have two circular objects bouncing off each other. The cool thing about circles?
Every point on the edge is equally distant from it's center.
This means that if you have two circles touching, the distance between their centers is the sum of their radii.
So you have circle1 with a radius of 4 and circle2 with a radius of 1. If the distance between their centers is greater than 5, they are not in contact. If the distance is exactly 5 (or as close as you can get in pixels), then they're in contact.
The only trigonometry you need to know is having the ability to find the hypotenuse of a triangle. |
|
|
|
|
|
Random
|
Posted: Sun Nov 07, 2004 10:36 am Post subject: Collision |
|
|
Yea, i can get the collision detection, so it knows when the balls collide, but it is my angles that are messed up. You'd use sine theta because for x because that is equal to adjacent / hypoteneuse, and the adjacent is horizontal. Then I'd use cosine theta because that is opposite / hypoteneuse, and the opposite is vertical. That make sence?
The problem is, when the new x and y velocities are calculated sometimes they are in the wrong dirrection so one ball will go through the other. I think this is because a velocity continues to be positive when it should be negative or vice-versa. Understand what I'm saying? I was just wondering how it could be done so that the velocity is always in the proper dirrection instead of it sometimes being right and sometimes not.
Thanks for your reply. I think I'll try messing around with my trig some more. Im hoping the problem is some small thing i missed (atleast i hope so). I appreciate your time. +10 bits |
|
|
|
|
|
Delos
|
Posted: Sun Nov 07, 2004 10:51 am Post subject: (No subject) |
|
|
I'm not sure if you've read these or not, but here's a link to a number of tutorials on the topic. |
|
|
|
|
|
Cervantes
|
Posted: Sun Nov 07, 2004 10:58 am Post subject: (No subject) |
|
|
Random wrote: You'd use sine theta because for x because that is equal to adjacent / hypoteneuse, and the adjacent is horizontal. Then I'd use cosine theta because that is opposite / hypoteneuse, and the opposite is vertical. That make sence?
isn't it the other way around?
SOHCAHTOA |
|
|
|
|
|
Random
|
Posted: Sun Nov 07, 2004 11:12 am Post subject: (No subject) |
|
|
Quote: isn't it the other way around?
SOHCAHTOA
Wow... dont i feel like an idiot. I guess you found my stupid mistake before me. Thank you so much!
And thanks for the link to the tutorials delos. I did read them, but thanks. |
|
|
|
|
|
|
|