walter jr
walter jr

Reputation: 25

How I can calculate the endpoint of a line, if I have the starting point, the angle and length of the line?

Point 1, say it is (0, 0) and I have another point that should turn around at a distance of 10f. Then I'll adding degrees angle to make it rotate. I be wanting to know how to calculate that point that this turning around each other ..

I will use the raycasting, I need to rotate the ray (clockwise) to detect collisions

Upvotes: 1

Views: 614

Answers (2)

Alex S. Diaz
Alex S. Diaz

Reputation: 2667

So you say you have point1 and point2, both separated by a distance of 10f where point2 will be rotating around point1, and you want to know if between this separation an object at some point is intersecting them, something like the following image:

view

There are tutorials to get the maths for rotate a point arount another one in internet, like this one, and since you can't specify an origin for a Vector2, a translated version of the code proposed in the previews link to java should be something similar to:

public Vector2 rotatePoint(Vector2 center, Vector2 point, float angle) {
    angle = angle * MathUtils.degreesToRadians; // Convert to radians
    float rotatedX = MathUtils.cos(angle) * (point.x - center.x) 
            - MathUtils.sin(angle) * (point.y - center.y) + center.x;
    float rotatedY = MathUtils.sin(angle) * (point.x - center.x) 
            + MathUtils.cos(angle) * (point.y - center.y) + center.y;

    // rotated new position:
    return new Vector2(rotatedX, rotatedY);
}

As for the rest of the code (the intersection between objects), I guess you are looking for the RayCastCallback interface:

// initial position
Vector2 point1 = new Vector(0, 0);
// Max lenght of view
Vector2 point2 = new Vector(0, 10);
// Position of collision if occur
final Vector2 collisionPoint = new Vector();

@Override
public void render(float delta) {
    //...
    point2 = rotatePoint(point1, point2, 10); // rotate 10º
    // to detect if object at position point1 is seeing something
    world.rayCast(new RayCastCallback(){
        @Override
        public float reportRayFixture(Fixture fixture,  Vector2 point, 
                 Vector2 normal, float fraction) {
            // what do the object saw?     -> fixture
            // where do the object saw it? -> point

            collisionPoint.set(point);

            return 0; // <- return 0 to stop raycasting
        }
    }, point1, point2);
    //...  rotation and other stuffs...
}

The return parameter of reportRayFixture have this documentation:

Called for each fixture found in the query. You control how the ray cast proceeds by returning a float: return -1: ignore this fixture and continue return 0: terminate the ray cast return fraction: clip the ray to this point return 1: don't clip the ray and continue. The Vector2 instances passed to the callback will be reused for future calls so make a copy of them!

** Emphasis added.

Basically it says that you can check for all the intersections one by one, but if you care only for the first one, return 0 immediately. This is useful when you want to know if an object is being blocked by another one. In this case, I return 0 and copy the value of point to collisionPoint to let you do whatever you want to do with this value.

A very nice example can be found in this video.

Hope you find this useful.

Upvotes: 1

m.antkowicz
m.antkowicz

Reputation: 13581

You should consider using Intersector class to check if the line from your actor intersects with the body shape.

To calculate end of "sight" line use Vector2 that you will be rotating according to your actor rotation (which is actually answer for your question)

It should looks like:

    Vector2 sightVector = new Vector2(10f, 0); //the 10f is actually your sight max distance
    sightVector.rotate(actor.getRotation());
    
    ...
    
    @Override
    pblic void render(float delta) //it can be also act of the actor
    {
        sightVector.rotate(actor.getRotation());
    
        Vector2 endOfLine = new Vector2(actor.getX() + sightVector.x, actor.getY() + sightVector.y); //here you are calculating the end of line
    
        Polygon bodyShape = getBodyShape( theBody ); //you should create a method that will return your body shape
        
        if( Intersector.intersectLinePolygon(new Vector2(actor.getX(), actor.getY()), endOfLine, bodyShape) )
        {
            //do something
        }
            
        ...
        
    }

Intersector has method to check intersection with circles etc also so your body Shape doesn't need to be polygon

Upvotes: 0

Related Questions