How to create lines from point A to B?

I understand there is a Raycast render line, however it is only 1px long. Can’t seem to find or figure out the math needed to create a rectangle/cylinder from point A to B.

Any advice?

Thanks.

For example you could create your own geometry by script (define vertices around point A and point B to make it a very long cylinder/rectangle/etc) and set its material to not have any lighting (just pure white color) so it does look like a line without any shading.

1 Like

Thanks for your reply. How would I go about defining vertices around A & B? Could you give me some sort of example?

Well, the easiest solution (but very sub-optimal and very case-specific) would be to calculate the middle point between points A and B, create a entity there (box for example), and scale it on the right axis so it starts and ends in points A and B.

The better solution which I suggested above would be to create your own geometry - these links should give you a general idea: Link 1 Link 2

1 Like

Thanks, I will have a go at both. Just to have something temp in place for now, I may try the first option to begin with.

This was an idea I had aswell, but if I am right, can’t you only scale both sides at once? Would I have to find the middle then scale until points A/B? Hopefully you know what I mean.

Would I have to find the middle then scale until points A/B?

Yeah, that’s what I said :slight_smile:

Is this basically what you want where it only scales from one end?

https://playcanvas.com/editor/scene/803553

The way I’ve set it up is that if you position the Line Root at point A and scale the Z to be the length of the line (point B - point A), you get a ‘line’ from point A to point B. The X and Y scale of the Line Root represent the thickness of the ‘line’.

1 Like

Oooo interesting. That would be ideal. I will give it a go :slight_smile:

Slightly confused why mine isn’t working like yours. When I scale the line root entity, it scales both sides.

The child box is probably not offset in position in yours.

Sorry - I must be being stupid. I don’t see any offset option?

1 Like

Figured out what you meant as I said it! Apologies. Ok cool, thanks! I will have a play around :slight_smile:

If you scale the child like this, then the positions wont match anymore (the wanted behaviour stops working), because the child will get scaled differently.

You would have to nest the child again. So the hiearchy would be:

  • Outer wrapper (scale 1, 1, 10)
    • Inner wrapper (scale 0.1, 0.1, 1.0)
      • Box model (Local position 0, 0, 0.5)
1 Like

@Karel_Otruba @yaustar regarding this example. How would I go about adding rotations in? For example I have 2 positions. One is (10, 10, 10), The other is (0,0,0). How do I calculate the rotation to rotate accordingly to the second position + programatically?

Thanks!

Place the Line Root at point A, have it look at point B, change the Z local scale to be the distance between A and B, place the Line Root at point B.

https://developer.playcanvas.com/en/api/pc.Entity.html#lookAt

1 Like

Running example drawing a line between the cube and the sphere: https://playcanvas.com/editor/scene/803553
image

2 Likes

Ahh wow. I tried so hard to do this! It works perfectly. One question, how come you set the lineRootEntity at both Point A & B? What’s the logic behind that? Thank you so much!

Positive Z is backwards in the coordinate system that PlayCanvas uses so when you use LookAt, it makes the local negative Z point towards the point.

1 Like