determine collision angle on a rotating body
Posted
by
jorb
on Game Development
See other posts from Game Development
or by jorb
Published on 2014-05-06T05:07:45Z
Indexed on
2014/06/08
15:46 UTC
Read the original article
Hit count: 294
update: new diagram and updated description
I have a contact listener set up to try and determine the side that a collision happened at relative to the a bodies rotation. One way to solve this is to find the value of the yellow angle between the red and blue vectors drawn above. The angle can be found by taking the arc cosine of the dot product of the two vectors (Evan pointed this out). One of my points of confusion is the difference in domain of the atan2 function html canvas coordinates and the Box2d rotation information. I know I have to account for this somehow... SS below
questions:
- Does Box2D provide these angles more directly in the collision information?
- Am I even on the right track? If so, any hints?
I have the following javascript so far:
Ship.prototype.onCollide = function (other_ent,cx,cy) {
var pos = this.body.GetPosition();
//collision position relative to body
var d_cx = pos.x - cx;
var d_cy = pos.y - cy;
//length of initial vector
var len = Math.sqrt(Math.pow(pos.x -cx,2) + Math.pow(pos.y-cy,2));
//body angle - can over rotate hence mod 2*Pi
var ang = this.body.GetAngle() % (Math.PI * 2);
//vector representing body's angle - same magnitude as the first
var b_vx = len * Math.cos(ang);
var b_vy = len * Math.sin(ang);
//dot product of the two vectors
var dot_prod = d_cx * b_vx + d_cy * b_vy;
//new calculation of difference in angle - NOT WORKING!
var d_ang = Math.acos(dot_prod);
var side;
if (Math.abs(d_ang) < Math.PI/2 )
side = "front";
else
side = "back";
console.log("length",len);
console.log("pos:",pos.x,pos.y);
console.log("offs:",d_cx,d_cy);
console.log("body vec",b_vx,b_vy);
console.log("body angle:",ang);
console.log("dot product",dot_prod);
console.log("result:",d_ang);
console.log("side",side);
console.log("------------------------");
}
© Game Development or respective owner