Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (595) Games in Android Showcase (168) games submitted by our members Games in WIP (646) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 circle - line collision and bump  (Read 3161 times) 0 Members and 1 Guest are viewing this topic.
teletubo
Global Moderator

JGO Wizard

Medals: 66
Projects: 4
Exp: 8 years

 « Posted 2009-12-19 21:14:43 »

Hello .
I am currently developing some basic physics for an android game , but I've been for days trying to do a decent collision for a moving circle and a static line .

For the circle I have its center (a point cx,cy)  the radius and it velocity vx and vy .
For the line I have the points Pa and Pb that describe the line .

I can already detect when a collision occurs .

But what I'm suffering to find is the final velocity vx and vy after the collision .

Any hints ?

Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #1 - Posted 2009-12-19 21:27:24 »

Couldn't you get the perpendicular at the point of collision then calculate a reflection vector over that?

See my work:
OTC Software
teletubo
Global Moderator

JGO Wizard

Medals: 66
Projects: 4
Exp: 8 years

 « Reply #2 - Posted 2009-12-19 21:41:22 »

well the idea of what shoul be done I also have

the problem is how to make this really work

Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #3 - Posted 2009-12-19 22:09:20 »

well the idea of what shoul be done I also have

the problem is how to make this really work

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `collided(Circle circle, Line line){    //Get the vector for the line and compute a normalized inverse.    Vector2 lineVector = new Vector2(line.x2 - line.x1, line.y2 - line.y1);    lineVector.normalize();    float temp = lineVector.x;    lineVector.x = -lineVector.y;    lineVector.y = temp;    //Find the measure of the angle between the circle and the inverse.    float angle = circle.velocity.normalized().dot(lineVector);    //Reflect the circle's velocity.    float circleMag = circle.velocity.magnitude();    circle.velocity.x += Math.cos(-2*angle) * circleMag;    circle.velocity.y += Math.sin(-2*angle) * circleMag;}`

I'm pretty sure that's not right at all, but I can't really think about it right now. It should give you the idea, though.

See my work:
OTC Software
pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « Reply #4 - Posted 2009-12-20 15:19:36 »

I'm pretty sure that's not right at all, but I can't really think about it right now. It should give you the idea, though.
The normal calculation is correct. As for the rest: v' = v - (1 + e) (v . N) N where e is the coefficient of restitution.
Bonbon-Chan

JGO Coder

Medals: 12

 « Reply #5 - Posted 2009-12-21 08:01:19 »

I don't like to pass through angles

For a bump with a line (not a segment, if it is a segment you have to do special case for the end) :

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 `collided(Circle circle, Line line){    //Get the tangente of the line    Vector2 lineVector = new Vector2(line.x2 - line.x1, line.y2 - line.y1);    lineVector.normalize();    //Project velocity on tangent and normal    float vTangent = circle.velocity.x*lineVector.x+circle.velocity.y*lineVector.y;    float vNormal = circle.velocity.y*circle.velocity.x- circle.velocity.x*lineVector.y;    // Restitution coef    vNormal = e * vNormal;    //Calculate the new velocity by flipping the normal velocity        circle.velocity.x = vTangent*lineVector.x + vNormal * lineVector.y;    circle.velocity.y = vTangent*lineVector.y -  vNormal * lineVector.x;}`

I'm not 100% sure for the signs. But you avoid cos/sin.

Quote
float angle = circle.velocity.normalized().dot(lineVector);

I'm pretty sure that it is wrong. The dot product give the cos(angle) not the angle.
jojoh

JGO Knight

Medals: 5
Projects: 7

games4j.com

 « Reply #6 - Posted 2009-12-21 11:21:11 »

Some helper function I had laying around:

 1  2  3 `   public static float bounceAgainst(float normal, float heading) {      return 2*normal - (heading + (float)Math.PI);   }`

I think I use it in a bunch of places, so it should work. Heading is just the heading (direction) of the colliding object. Normal is a normal of the line bouncing against. Returns the new heading after bounce.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `public class LLine2D extends Line2D.Float{   .   .   .   public float getDX() {      return x2-x1;   }   public float getDY() {      return y2-y1;   }      public float getDirection() {      return (float)Math.atan2(getDY(), getDX());   }}`

Add half a pie (PI/2) to the getDirection() of the line to get the normal.

Whatever works best depends on if you have angle and velocity or dx and dy for your movement.

Hope it helps

Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #7 - Posted 2009-12-21 16:03:52 »

I'm pretty sure that it is wrong. The dot product give the cos(angle) not the angle.
Ah, yup yup. I always forget that until my equation doesn't work and I have to go back and remember why. Thanks.

See my work:
OTC Software
teletubo
Global Moderator

JGO Wizard

Medals: 66
Projects: 4
Exp: 8 years

 « Reply #8 - Posted 2009-12-21 20:43:42 »

Thank you all for the help .

I ended up implementing something like Bonbon Chan described ... but a question for you alll : what the hell are those Cricle and Line classes ?? At least on my JSE they're not there , (or I havent searched in the right place) but you guys seem to know them thoroughly ... so what's up with them ??

zoto

Senior Devvie

Medals: 4

 « Reply #9 - Posted 2009-12-21 21:47:57 »

Those shapes are in java.awt.geom, note the circle is a type of ellipse. They probably just mean the user defined classes like you have though.
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #10 - Posted 2009-12-21 23:03:06 »

Thank you all for the help .

I ended up implementing something like Bonbon Chan described ... but a question for you alll : what the hell are those Cricle and Line classes ?? At least on my JSE they're not there , (or I havent searched in the right place) but you guys seem to know them thoroughly ... so what's up with them ??

Haha, yeah, I think I started that. I only used that as a reference point so you could see what was happening, then hook that into your code. It's just easier than having like lineX1, lineX2, lineY1, lineY2 etc. etc. being the values. Assumedly you'll have your own ways of implementing circle and line and they'll have their own ways of being accessed and mutated.

See my work:
OTC Software
Bonbon-Chan

JGO Coder

Medals: 12

 « Reply #11 - Posted 2009-12-22 07:55:13 »

Haha, yeah, I think I started that. I only used that as a reference point so you could see what was happening, then hook that into your code. It's just easier than having like lineX1, lineX2, lineY1, lineY2 etc. etc. being the values. Assumedly you'll have your own ways of implementing circle and line and they'll have their own ways of being accessed and mutated.

I have just copied it  Like it is writen, it is easy to understand.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 deepthought (47 views) 2015-06-30 15:39:44 deepthought (50 views) 2015-06-30 15:39:09 deepthought (62 views) 2015-06-30 15:36:52 Za\'Anzabar (37 views) 2015-06-29 05:44:54 TritonDreyja (46 views) 2015-06-24 17:10:40 CopyableCougar4 (46 views) 2015-06-23 00:34:45 BurntPizza (52 views) 2015-06-21 20:36:46 cookiecompiler (93 views) 2015-06-11 15:42:53 cookiecompiler (55 views) 2015-06-11 15:41:14 NegativeZero (80 views) 2015-06-11 09:49:18
 princec 29x wessles 22x BurntPizza 19x CopyableCougar4 19x opiop65 17x ags1 15x nsigma 15x EgonOlsen 14x Riven 13x theagentd 12x KaiHH 12x SauronWatchesYou 11x KevinWorkman 11x Archive 11x sunburn 11x NegativeZero 10x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org