How to get end point from a line

I’ve made a line via this.add.line() and have rotated it.

I need the direction the line points in after rotation, so I’m thinking to:

  • get the end point of line (e)
  • get the start point of line (s)
  • get vector ES by doing start - end
  • normalizing for the direction

When I create the line, I can set the start and end points, but I can’t seem to access those points from the line object.

How do I get the end point on the line I’ve made?!

Thanks!

:wave:

What were the start and end points?

Hey samme,

Let’s say start is 0,0 and end is 0, -50.

If you just need the rotated angle I would calculate the original angle (start–end) and then add the game object’s rotation.

If you need the rotated endpoints themselves it’s something like

const line2 = Phaser.Geom.Line.Clone(line.geom);

Phaser.Geom.Line.CenterOn(line2, line.x, line.y);
Phaser.Geom.Line.RotateAroundPoint(line2, line, line.rotation);

Ok, so it looks like there isn’t a way to get the end point from a GameObject.Line - you have the make a Geom.Line and duplicate any operations (rotations/translations) done to GO.Line, just so you can access the Geom.Line.x2/y2 properties. This is the way to go if you need an actual end point, though it seems a bit heavy handed.

Since I only needed the end point in order to find the direction from start to end of the line, I won’t do the above as there is another way.

The GameObject.Line class already provides an angle property, which is a value in degrees, as well as a rotation property, which is a value in radians. The normalised direction of the line can be found using rotation by:

const dir = new Vec2(
  Math.cos(line.rotation) - Math.PI / 2,
  Math.sin(line.rotation) - Math.PI / 2,
)

Which does the job, albeit less efficiently than (START - END).normalize().

Thanks for your input!