Collision detection with rotated objects

I am trying to detect collision between a rotated and non-rotated object. I’m using the p5 rotate function, and the objects are not in the same coordinate space. Thanks for your help!

   translate(width/2,height/2);
   t.drawTarget();
   a = atan2(mouseY - height / 2, mouseX - width / 2);
   push();
   rotate(a);
   me.drawMe();
   me.moveMe();
   for (let i = 0; i < arrow.length; i++) {
        arrow[i].drawArrow();
        arrow[i].moveArrow();
        arrow[i].hitTarget();
      }
    pop();

link to the full code

2 Likes

My suggestion is that - if you need to do collision - don’t rely on rotate() to determine the position of your things. Instead, track the actual positions of your things, along with their actual velocities and actual accelerations. It’s fine to track the X- and Y- components of these things! Then any forces you apply can be broken into X- and Y- components with a little trig…

3 Likes

Hello,
Many of Daniel Shiffman’s examples on youtube use rotate() to rotate a vector object, such as this . When I replicate this code on my computer, the rectangles pass through the obstacle, due to this rotate difference.

How do I rotate my object without using rotate(this.vel.heading())? That’s a pretty convenient function because it allows me to tether the direction of my movement and my object’s direction.

I’m not so strong on vector math so I’d appreciate some help on how to rotate my object according to this.vel.heading.

In case anyone is searching these old threads, I eventually learned not to use rotate() for collision detection and instead to rotate the objects using polar coordinates. My students run into this problem a lot, and now I always point them toward this nice tutorial https://www.youtube.com/watch?v=N633bLi_YCw (by the great Daniel Shiffman of course)

In pseudocode you are trying use one loop to do everything -

for each shape
    render the shape
    update the shape's position
    perform collision detection

This is not a good idea it is far better to separate these into different functions like this

perform 1:1 collision detection between all shapes
for each shape
    update the shape's  position
for each shape
    render the shape

This simplifies the programming because you split the code into useful functions that can be written and debugged independently.

1 Like