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
end
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)})
end
end
As you can see in the video, each time the hinge joint is created, the position of the object changes.