Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

10 years ago by Firegael

Hey guys, I am going to do research on this over the weekend, but I thought I'd ask here in case anyone has a good idea of how to do this already.

Essentially, I would like to find entities that are inside an area like the one below:

/><br />
<br />
I have done this using Box2D, but I would prefer not to use it in this instance as the mobile performance is apparently not that great, plus, getting it to play with impact++ is seeming to be a problem (probably something simple).<br />
<br />
In any case, we don't need the kind of physics that Box2D provides, but the shapes and detection are nice.			</div>
		</div>
			<div class=

10 years ago by Firegael

So, after solving the problem by writing some functions with the help of stack overflow, I also found a function which impact++ already has in it's intersection helper file; pointInPolygon(x, y, vertices).

I'll solve whether or not the following point is inside the following triangle using both methods anyhow, for thoroughness.

I should also note that Collin Hover's Impact++ method works for other polygons, not just triangles, where as mine is specific to triangles.

Problem:

point = 6,-1
point2 = 1,1

Given triangle with the following vertices, is p inside?

a = 0,0
b = 8, -4
c = 12, 0

/><br />
<br />
<strong>My solution:</strong><br />
<br />
<pre class= /** * I am unsure as to why this is called sign in every example I found. If someone could enlighten me that'd be great. * @param {Vector2} a Point which we are checking. * @param {Vector2} b One of three points of a triangle. * @param {Vector2} c One of three points of a triangle. * @return {Number} Returns >= 0 if outside. */ sign = function(a, b, c) { return (a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y); } /** * Finds whether or not a point lies inside a triangle. * @param {Vector2} point Point which we are checking. * @param {Vector2} a Point A. * @param {Vector2} b Point B. * @param {Vector2} c Point C. * @return {Boolean} Returns true if inside triangle, false if outside. */ pointInTriangle = function(point, a, b, c) { var b1 = sign(point, a, b) < 0; var b2 = sign(point, b, c) < 0; var b3 = sign(point, c, a) < 0; return ((b1 == b2) && (b2 == b3)); } pointInTriangle(point, a, b, c); // => true pointInTriangle(point2, a, b, c); // => true
Using Impact++ pointInPolygon:

pointInPolygon(point.x, point.y, [a, b, c]); // => true
pointInPolygon(point2.x, point2.y, [a, b, c]); // => false

10 years ago by Joncom

Neat. Thanks for sharing your solution. I didn't realize Impact++ had that function. Sounds pretty useful for situations like this.

10 years ago by Firegael

I just thought of another way to do this, which I think I will use.

We check the entities to see if they are within range for whatever we are doing with them. If they are, we then check to see if they are within the angle that we are looking for.

EG: (given entities and player)

for (var i = entities.length - 1; i >= 0; i--) {

    var distance = entity.distanceTo(player);

    // If the entity is outside the range, 
    // then don't bother doing the angle check.
    if (distance <= range) {

        var angle = entity.angleTo(player).toDeg();

        if (angle > 0 && angle <= 45) {

            // Do things!

        }

    }

}

Since the game I'm working on only has 8 distinct angles, I can do specific checks like this using static variables easily. If you need to calculate the angles on ability cast (or whatever you are doing), you'll just need to add those numbers instead of 0 and 45.

Edited for slight efficiency improvement.
Page 1 of 1
« first « previous next › last »