CollisionDetection icon indicating copy to clipboard operation
CollisionDetection copied to clipboard

point of collision

Open ghost opened this issue 3 years ago • 4 comments
trafficstars

Hello ! How can i found position of collision when is it true ?

also i need normal direction .

ghost avatar Mar 02 '22 07:03 ghost

Getting the point of collision would be super easy for some of these (point/point, for example) but very difficult for most of them. The math gets way more complex and the code would be quite a bit slower, which is why the positions of collision isn't included here, sorry!

For normal direction, you mean the direction that the object was moving when it collided with another?

jeffThompson avatar Mar 02 '22 13:03 jeffThompson

When I saw this example

Line - Line Or Line Rectangle

image

I could not find the code for the red points, so I asked this question. It would be helpful if I could talk about what I want to make!

image

Ray cast hit : in 2D I want to find out between two points with a line which is the closest object and where this point collides and what is its direction.

I think this method will have a quick result

The red dot source code is very useful to me and solve all

:1st_place_medal: Your code is very simple to learn and great!

ghost avatar Mar 02 '22 14:03 ghost

Ah, those will actually be really easy! If you look in the collision functions, they give us the x/y coordinates. I don't send them back to the main draw() but you could definitely modify the function to return a vector instead of true/false.

For example, here's line/line:

// LINE/LINE
boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {

  // calculate the distance to intersection point
  float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
  float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));

  // if uA and uB are between 0-1, lines are colliding
  if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {

    // optionally, draw a circle where the lines meet
    float intersectionX = x1 + (uA * (x2-x1));
    float intersectionY = y1 + (uA * (y2-y1));
    fill(255,0,0);
    noStroke();
    ellipse(intersectionX,intersectionY, 20,20);

    return true;
  }
  return false;
}

And this is the part you want!

float intersectionX = x1 + (uA * (x2-x1));
float intersectionY = y1 + (uA * (y2-y1));

jeffThompson avatar Mar 02 '22 16:03 jeffThompson

Thank you very much, even very simple things can be useful 😃👍

On Wed, Mar 2, 2022, 8:04 PM Jeff Thompson @.***> wrote:

Ah, those will actually be really easy! If you look in the collision functions, they give us the x/y coordinates. I don't send them back to the main draw() but you could definitely modify the function to return a vector instead of true/false.

For example, here's line/line:

// LINE/LINE boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {

// calculate the distance to intersection point float uA = ((x4-x3)(y1-y3) - (y4-y3)(x1-x3)) / ((y4-y3)(x2-x1) - (x4-x3)(y2-y1)); float uB = ((x2-x1)(y1-y3) - (y2-y1)(x1-x3)) / ((y4-y3)(x2-x1) - (x4-x3)(y2-y1));

// if uA and uB are between 0-1, lines are colliding if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {

// optionally, draw a circle where the lines meet
float intersectionX = x1 + (uA * (x2-x1));
float intersectionY = y1 + (uA * (y2-y1));
fill(255,0,0);
noStroke();
ellipse(intersectionX,intersectionY, 20,20);

return true;

} return false; }

And this is the part you want!

float intersectionX = x1 + (uA * (x2-x1)); float intersectionY = y1 + (uA * (y2-y1));

— Reply to this email directly, view it on GitHub https://github.com/jeffThompson/CollisionDetection/issues/29#issuecomment-1057126700, or unsubscribe https://github.com/notifications/unsubscribe-auth/APJCR4QA3P3XKRONL3GNA43U56KCVANCNFSM5PWQ6W3Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

ghost avatar Mar 02 '22 19:03 ghost