Rotating Rectangle Collision

Currently I am using a script to handle box collisions, however I an realizing I need my box collider to a rectangle, and to be rotatable, as to better simulate the movements of a vaguely oblong tank without things clipping into other things. Unfortunately, I think this would either involve having to calculate the rotation vector and add it into the acceleration or possibly use two colliders with linked velocities and I am wondering if there's a simpler solution that I am missing.

This is the current code I'm using:


GML:
collisionVeloX= cos(angleDir*2*pi/360)*velocity;
collisionVeloY= -sin(angleDir*2*pi/360)*velocity;

xVelo = velocity;
yVelo = velocity;

if(place_meeting(x + collisionVeloX, y, obj_ow_nopass)){
  while(!place_meeting(x+ collisionVeloX,y,obj_ow_nopass))
  {
    x+=collisionVeloX;
  }
  xVelo = 0;
}

if(place_meeting(x, y + collisionVeloY, obj_ow_nopass)){
  while(!place_meeting(x, y+collisionVeloY,obj_ow_nopass))
  {
    y+=collisionVeloY;
  }
  yVelo = 0;
}

x+=cos(angleDir*2*pi/360)*xVelo;
y+=-sin(angleDir*2*pi/360)*yVelo;

I think I would need to do something like:

GML:
point_direction(oldAngle.x,oldAngle.y,newAngle.x,newAngle.y);
and then add that in to the current acceleration for the collision check.

I'm open to any solutions though.
 
R

robproctor83

Guest
Not sure what your trying to do exactly, but there is an option for rotating masks. Where you set the mask to be precise, there is an option for rotated.
 
R

relic1882

Guest
Am I missing something or is there a reason you can't set the collision mask on the tank to Manual, then Rectangle with rotation? That way you could specify the rectangle within your tank sprite you want to be the collision mask.
 
Doing some quick tests, when rotating, the rectangle will just phase through the block instead of colliding. I'm assuming changing image_angle also changes the collision_mask when using rectangle with rotation ?
 

MD_Wade

Member
Doing some quick tests, when rotating, the rectangle will just phase through the block instead of colliding. I'm assuming changing image_angle also changes the collision_mask when using rectangle with rotation ?
Changing image_angle will affect the bounding box if it's not precise, or the overall collision if it is. To avoid "phasing" through the blocks, I'd suggest having a separate mask and just changing the way the object is drawn, rather than rotating it with image_angle.
 
Changing image_angle will affect the bounding box if it's not precise, or the overall collision if it is. To avoid "phasing" through the blocks, I'd suggest having a separate mask and just changing the way the object is drawn, rather than rotating it with image_angle.
That would defeat the purpose of using a rotating rectangle collider though.
 

MD_Wade

Member
That would defeat the purpose of using a rotating rectangle collider though.
Yeah, sorry, I'm at a loss for that. I tried to come up with a solution when I first started doing top-down shooters a few years ago and my conclusion was that I can deal with some graphics clipping into walls as the player doesn't really care about that. Your example may vary from that, but I'm gonna watch this thread to see if a solution is found, because it has my interest.
 
Could you get away with doing something like this whenever the tank's angle changes? It might not be the most physically accurate, but it should work pretty well.

GML:
var col = instance_place(x, y, obj_ow_nopass);
if (col > -1) {
    dir = point_direction(col.x, col.y, x, y);
    x += lengthdir_x(1, dir);
    y += lengthdir_y(1, dir);
}
rotation-rect-coll.gif
 
Top