Lines between Physical objects

I have created to display objects and between the have set up a physics joint, type “distance”. Gravity is all set to 0.

The central object (brown circle) is static and sits in the middle of the screen.

The green circle can be dragged around the screen and returns back towards the static object.

As per the screenshot what I would like to do is to draw a straight line between the two objects and that updates as they move, exactly as you can see when in hybrid mode.

Is there anyway to display this line when in normal mode or any easy way to code this? I do not need the line to have any physical influence to the scene I just want to represent it?

I have seen the rope effect of chaining lots of objects together but what I want to create is a straight line, like a rod that pivots on the center points and stretches and shortens accordingly.

exampleline.jpg

Thanks

just psuedocode to give an idea, adjust as necessary to fit your actual use

local theLine = nil local function onEnterFrame(event)   if (theLine) then -- remove old line if it exists     display.remove(theLine)     theLine = nil   end   theLine = display.newLine(circle1.x, circle1.y, circle2.x, circle2.y) end Runtime:addEventListener("enterFrame", onEnterFrame)

that’s the “easy way” though it thrashes a lot.

(better would be to translate, scale and rotate a single persistent line, trickier to code but no thrashing)

I agree it does thrash things a lot but for the purposes i need it for i.e. Creating a demo it will do the job.

I was thinking about doing something clever to scale rotate etc but it is over engineering for what I need it for at this time. That is not the purpose of what I am working for.

Thanks Dave :slight_smile:

Hi @jeremy52,

Yes, this is a valid (if slightly inefficient) solution. If you don’t want to continually remove and replace the line, you could also set up a very thin rectangle object and then do some basic math in the dragging routine to get the angle between the two objects, and the center point between them along the line, and position/rotate the rectangle via those values.

Brent

Thanks Brent,

If i have to do it for real I will switch to that, go back to the old school maths :wink:

http://code.coronalabs.com/code/mathliblua

It’s not that tricky and you’ll find some useful functions to do the heavy lifting in that library.

fwiw, here’s psuedocode for the parenthetical in my first reply

-- once: local theLine = display.newLine(0,0,1,0) -- repeat: theLine.x, theLine.y = circle1.x, circle1.y local dx = circle2.x - circle1.x local dy = circle2.y - circle1.y theLine.rotation = math.deg(math.atan2(dy,dx)) theLine.xScale = math.sqrt(dx\*dx+dy\*dy)

That works perfectly, that is really nice and neat, I did not think it would be such a tidy solution to do it. It is very smooth as well  B)

just psuedocode to give an idea, adjust as necessary to fit your actual use

local theLine = nil local function onEnterFrame(event)   if (theLine) then -- remove old line if it exists     display.remove(theLine)     theLine = nil   end   theLine = display.newLine(circle1.x, circle1.y, circle2.x, circle2.y) end Runtime:addEventListener("enterFrame", onEnterFrame)

that’s the “easy way” though it thrashes a lot.

(better would be to translate, scale and rotate a single persistent line, trickier to code but no thrashing)

I agree it does thrash things a lot but for the purposes i need it for i.e. Creating a demo it will do the job.

I was thinking about doing something clever to scale rotate etc but it is over engineering for what I need it for at this time. That is not the purpose of what I am working for.

Thanks Dave :slight_smile:

Hi @jeremy52,

Yes, this is a valid (if slightly inefficient) solution. If you don’t want to continually remove and replace the line, you could also set up a very thin rectangle object and then do some basic math in the dragging routine to get the angle between the two objects, and the center point between them along the line, and position/rotate the rectangle via those values.

Brent

Thanks Brent,

If i have to do it for real I will switch to that, go back to the old school maths :wink:

http://code.coronalabs.com/code/mathliblua

It’s not that tricky and you’ll find some useful functions to do the heavy lifting in that library.

fwiw, here’s psuedocode for the parenthetical in my first reply

-- once: local theLine = display.newLine(0,0,1,0) -- repeat: theLine.x, theLine.y = circle1.x, circle1.y local dx = circle2.x - circle1.x local dy = circle2.y - circle1.y theLine.rotation = math.deg(math.atan2(dy,dx)) theLine.xScale = math.sqrt(dx\*dx+dy\*dy)

That works perfectly, that is really nice and neat, I did not think it would be such a tidy solution to do it. It is very smooth as well  B)