mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-07-04 05:55:47 -07:00
Fix computational errors in Damage.js for testing collision of a point with the square or the cirle footprint of an entity. Logic reviewed by Itms, style reviewed by elexis.
This was SVN commit r18811.
This commit is contained in:
parent
a5b399de29
commit
bb0924bb6c
1 changed files with 12 additions and 8 deletions
|
|
@ -40,24 +40,28 @@ Damage.prototype.TestCollision = function(ent, point, lateness)
|
|||
let targetPosition = this.InterpolatedLocation(ent, lateness);
|
||||
if (!targetPosition)
|
||||
return false;
|
||||
|
||||
|
||||
let cmpFootprint = Engine.QueryInterface(ent, IID_Footprint);
|
||||
if (!cmpFootprint)
|
||||
return false;
|
||||
|
||||
|
||||
let targetShape = cmpFootprint.GetShape();
|
||||
|
||||
if (!targetShape)
|
||||
return false;
|
||||
|
||||
if (targetShape.type === 'circle')
|
||||
// Use VectorDistanceSquared and square targetShape.radius to avoid square roots.
|
||||
return targetPosition.horizDistanceTo(point) < (targetShape.radius * targetShape.radius);
|
||||
if (targetShape.type == "circle")
|
||||
return targetPosition.horizDistanceToSquared(point) < targetShape.radius * targetShape.radius;
|
||||
|
||||
let angle = Engine.QueryInterface(ent, IID_Position).GetRotation().y;
|
||||
let distance = Vector2D.from3D(Vector3D.sub(point, targetPosition)).rotate(-angle);
|
||||
if (targetShape.type == "square")
|
||||
{
|
||||
let angle = Engine.QueryInterface(ent, IID_Position).GetRotation().y;
|
||||
let distance = Vector2D.from3D(Vector3D.sub(point, targetPosition)).rotate(-angle);
|
||||
return Math.abs(distance.x) < targetShape.width / 2 && Math.abs(distance.y) < targetShape.depth / 2;
|
||||
}
|
||||
|
||||
return distance.x < Math.abs(targetShape.width/2) && distance.y < Math.abs(targetShape.depth/2);
|
||||
warn("TestCollision called with an invalid footprint shape");
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue