How to do collision detection for rotating objects?

edited May 2014 in How To...

In my program, I need it for rotating triangles colliding with points, but any example code would help. I'm to lazy to experiment and find out how to do this myself (this forum has spoiled me).

Answers

  • There is nothing special about rotating objects, you still test each line of one triangle to see if the lines of the other triangle intersect it. The only special case is when one triangle is completely inside the other.

    Note, I am assuming you are not using the rotate() function, that would not help the collision detection

  • edited May 2014

    @asimes I am using the rotate() function! I guess that's why I'm stuck. So how can I rotate stuff without that function?

    @Chrisir Thanks for the link

  • Answer ✓

    Using trig, here is a link to a Processing tutorial and a quick sketch: http://www.processing.org/tutorials/trig/

    float radius = 100;
    float theta = 0.0;
    PVector p;
    
    void setup() {
      size(400, 400);
      p = new PVector(width/2, height/2);
    }
    
    void draw() {
      background(255);
      beginShape();
      float angle = theta;
      for (int i = 0; i < 3; i++) {
        float x = width/2+cos(angle)*radius;
        float y = height/2+sin(angle)*radius;
        vertex(x, y);
        angle += TWO_PI/3.0;
      }
      endShape(CLOSE);
      theta += TWO_PI/300;
    }
    
  • Thanks!

Sign In or Register to comment.