public Vector3 intersects(CollisionBox other) { float Xdist = Math.Abs(this.position.X - other.position.X); float Ydist = Math.Abs(this.position.Y - other.position.Y); float Zdist = Math.Abs(this.position.Z - other.position.Z); float Xdiff = this.dimensions.X + other.dimensions.X; float Ydiff = this.dimensions.Y + other.dimensions.Y; float Zdiff = this.dimensions.Z + other.dimensions.Z; //TODO: Fix this to account for rotation too if (Ydist > Ydiff && Xdist > Xdiff && Zdist < Zdiff) { Vector3 thing = new Vector3(Xdist - Xdiff, Ydist - Ydiff, Zdist - Zdiff); return Vector3.Zero;//TODO FIXMEERROR } return Vector3.Zero; }
public Vector3 intersects(CollisionBox other) { float yDiff = this.position.Y - other.position.Y; float combinedHeight = this.Height + other.Height; Vector2 combinedDimensions = new Vector2(this.Radius + other.Dimensions.X, this.Radius + other.Dimensions.Z); //first check if height intersects if (Math.Abs(yDiff) < combinedHeight) { Vector2 xzDiff = new Vector2(this.position.X - other.position.X, this.position.Z - other.position.Z); float rot = MathHelper.Clamp(other.rotation, (float)-Math.PI, (float)Math.PI);//was pi/2 to -pi/2; //rotate by other if (other.rotation != 0) { xzDiff = Vector2.Transform(xzDiff, Matrix.CreateRotationZ(rot)); } if (Math.Abs(xzDiff.X) < combinedDimensions.X && Math.Abs(xzDiff.Y) < combinedDimensions.Y) { float intersectY = combinedHeight - Math.Abs(yDiff); Vector2 intersectXZ = new Vector2(combinedDimensions.X - Math.Abs(xzDiff.X), combinedDimensions.Y - Math.Abs(xzDiff.Y)); float intersectXZMin = intersectXZ.X < intersectXZ.Y ? intersectXZ.X : intersectXZ.Y; if (intersectY < intersectXZMin) { return new Vector3(0f, intersectY * Math.Sign(yDiff), 0f); } else { if (intersectXZ.X > intersectXZ.Y) { intersectXZ.X = 0f; } else { intersectXZ.Y = 0f; } intersectXZ = new Vector2(intersectXZ.X * Math.Sign(xzDiff.X), intersectXZ.Y * Math.Sign(xzDiff.Y)); intersectXZ = Vector2.Transform(intersectXZ, Matrix.CreateRotationZ(-rot)); xzDiff = Vector2.Transform(xzDiff, Matrix.CreateRotationZ(-rot)); return new Vector3(intersectXZ.X, 0f, intersectXZ.Y); } } } return Vector3.Zero; }