Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

point of collision #29

Open
ghost opened this issue Mar 2, 2022 · 4 comments
Open

point of collision #29

ghost opened this issue Mar 2, 2022 · 4 comments

Comments

@ghost
Copy link

ghost commented Mar 2, 2022

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

also i need normal direction .

@jeffThompson
Copy link
Owner

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?

@ghost
Copy link
Author

ghost commented Mar 2, 2022

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

🥇 Your code is very simple to learn and great!

@jeffThompson
Copy link
Owner

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));

@ghost
Copy link
Author

ghost commented Mar 2, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant