# How to create lines from point A to B?

#1

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.

Thanks.

#2

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.

#3

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

#4

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

#5

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.

#6

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

Yeah, that’s what I said

#7

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’.

#8

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

#9

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

#10

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

#11

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

#12

#13

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

#15

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)

#16

@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!

#17

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

#18

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

#19

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!

#20

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.