Hinge joint changes position of dynamic object

Video Example

I’m trying to make something swing by creating a joint object connecting it to another object, and I feel like I’m doing it wrong, because almost each time the joint is created, the dynamic object’s position changes and I don’t want that to happen.

This is the code for it:

--This finds the relative position of object 2 from object 1
function getlocalpos(obj1, obj2)
    local pos1 = go.get_position(obj1)
    local pos2 = go.get_position(obj2)
    local fpos = vmath.vector3()
    fpos.x = pos2.x - pos1.x
    fpos.y = pos2.y - pos1.y
    fpos.z = pos3.z - pos3.z
    return fpos

function on_input(self, action_id, action)
    if action.pressed then
			local colob = "/grappleto#collisionobject"
			physics.create_joint(physics.JOINT_TYPE_HINGE, "/player#collisionobject", "swingjoint", getlocalpos(".", colob:gsub("#collisionobject", "")), colob, vmath.vector3(0, -16, 0))
		elseif action.released then
			physics.destroy_joint("/player#collisionobject", "swingjoint")
			msg.post("#collisionobject", "apply_force", {position = go.get_position(), force = vmath.vector3(0, 500000, 0)})


As you can see in the video, each time the hinge joint is created, the position of the object changes.

There’s a Hinge Joint example here: Hinge joint physics

It was created by @Pawel during Hacktoberfest. Pawel, do you recall running into similar problems as @mchlkpng ?

1 Like

Hi! No, I didn’t have such issues, but I see the calculation of relative position looks suspicious here. Could you zip the project and upload it here? Would be easier to debug it, otherwise I will try to reproduce it after work :wink:

pos3 ?

Interesting way to get a path to component ) btw you can not get a position of collider component inside go.


I only made that function because I thought for some reason you can’t subtract vectors, I don’t know if I was wrong.

That’s just a typo, it’s supposed to be pos 2 and 1.

The second thing, a realized that for some reason gsub didn’t allow me to use the objects name so I just created a different variable with the objects name that replaced that.

Did you try it?

print(vmath.vector3(1,1,1) - vmath.vector3(0.5,0.5,0.5))

Yeah, a few days after posting this I figured it out.