Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  circle - line collision and bump  (Read 2919 times)
0 Members and 1 Guest are viewing this topic.
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
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 ?

Offline 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
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
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 Smiley

the problem is how to make this really work

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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 Smiley

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
Offline pjt33
« 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.
Offline Bonbon-Chan

JGO Coder


Medals: 12



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

I don't like to pass through angles  Tongue

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.
Offline 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

Offline 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. Smiley

See my work:
OTC Software
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
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 ??

Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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
Offline 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  Wink 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (69 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!