Jump to content
  • entries
    10
  • comments
    31
  • views
    7,556

Charrua

3,694 views

 Share

Recently i posted a simple car made with joints without too much explanations... so

What is a joint?

This video shows joints in action. At the end, the stand alone executable and the complete project.

 

The following text and figure is from the "ode-latest-userguide", figures are also form the JV-ODE documentation:

In real life a joint is something like a hinge, that is used to connect two objects.
It is a relationship that is enforced between two bodies so that they can only have
certain positions and orientations relative to each other. This relationship is called a constraint, 
the words joint and constraint are often used interchangeably.

The figure shows three different constraint types.

The first is a ball and socket joint that constraints the “ball” of one body to be in the same location as
the “socket” of another body. 

The second is a hinge joint that constraints the two parts of the hinge to be in
the same location and to line up along the hinge axle. 

The third is a slider joint that constraints the “piston”
and “socket” to line up, and additionally constraints the two bodies to have the same orientation.

Each time the integrator takes a step all the joints are allowed to apply constraint forces to the bodies
they affect. These forces are calculated such that the bodies move in such a way to preserve all the joint
relationships.

Each joint has a number of parameters controlling its geometry. An example is the position of the balland-
socket point for a ball-and-socket joint. The functions to set joint parameters all take global coordinates,
not body-relative coordinates. A consequence of this is that the rigid bodies that a joint connects must be
positioned correctly before the joint is attached.

Figure: types of joints

joints.jpg.d27cf596833bfd36db899b18649d03ec.jpg

 

The "Integrator step" is the part of the physics engine that does all the calculations.
Based on the current state, force applied, constrain parameters, collisions etc... recalculates the next position and rotation of every affected physical obejct.
Leadwerks then, does the render of the objects acordingly to it's new positions, or that is what is supposed to do... (Josh should correct me if I'm wrong).

A joint normally is placed between two objects, one is called Parent and the other Child. The child should be NULL, and in this case
the joint is between one body and the world or scene.

In the case of a door, you need a Door and a Frame, the Frame should be the Parent body, the Door the child body.
In real life you have to decide where the Hinge (or more than one) will be placed and use some screws to attach both parts of the hinge to the frame and the door.

In software, things are practically the same :)

In other words, we have to have 3 things clear:
    Who is the parent and where it should be positioned and oriented.
    Who is the child and where it should be poitioned and oriented.
    Which type of joint you need, where it should be positioned and oriented.

Normally we create and place the two bodies first and then, at joint creation we say where the joint will be and how it should be oriented, who the parent is and who child is, if any.

After that, joints normally has some other properties that should be adjusted, used, controlled, limited, enabled... etc, depending on the joint type.

Joints have two "limits":

If the joint is a Hinge, the limits are the Start Angle and End Angle "the door should turn".
If the joint is a Slider, the limits are the Start Position and End Position the piston should extend.
If the joint is a Ball, the limits are the cone angle and twist or torsion... keep reading :)

Joints (hinge and slider) should have a "motor" if not, as a normal door, we need external forces to "open the door", if we place a door on a frame attached with hinges and the door has no lock at all, then
the wind should open/close it, or some body that collides with the door should move it. As we use a hinge, the door do not fall, do not take an upward motion, the motion is well known and 
constrainded tho the clasic movement we all know.
But if, for instance we have a "motorized door" then the open/close operation is started in some way (press of a button?) and a motor does the job. Normally it stops automatically, because there are end-run switches or the like installed and working for us.
A sofware motorized joint is exactly that, we set where we want the joint go (setTargetAngle), and start a motor (enableMotor).

Ball Joint

A ball is used when we need a "cone" movement freedom, think that you have a rope attached at the roof of your room and you hang a box on it.
If the rope is made of a rigid material, the box should made a pendulum movement not so large and probably the box orientations should not change too much.
How much we let the rope to move is dictated by the first limit and how much the object attached should change orientation (twist/torsion) is dictated by the second limit.

ball-and-socket.jpg.bc47afe4b48f4051ec51fdd4451aacd9.jpg

A rope is a nice looking example, all you have to do is place N cubes (shape is not important) and place ball joints in the middle of each pair.
You may have the first box joined to the world (attached to a fixed 3d position) and then a link of bodies and ball joints:
    Joint N has body N as parent and body N+1 as child

Leadwerks came with a simple ball.lua script:

function Script:Start()
    local pos = self.entity:GetPosition(true)
    self.joint = Joint:Ball(pos.x, pos.y, pos.z, self.entity, self.entity:GetParent())
    self.joint:SetFriction(10)
    self.entity:SetDamping(0,0)
end

If you have a chain of parented objetcts, then you  may set this script and you have a rope build with a few clicks of your mouse on the editor.
Procedure:
    Create some boxes, set prop collision and a mass not zero (1 perhaps), place then in a vertical row somewhat separated.
    In the editor drag the second box to the first, the third to the second building a hierachy (parent/child relationship)
    Select all (one by one with ctrl click on each box) and then apply the ball script. Voila! You have a rope with no limits: cone angle is the maximum the engine lets you to be and the same for torsion.
    Collide the rope with some object or, place the boxes horizontally (instead of vetically) and let gravity do it's job.
    
I made another ball script: ballEnhaced:

Script.parent = nil--Entity "Parent"
Script.child = nil--Entity "Child"
Script.useLimits=false--bool "Use limits"
Script.coneAngle = 45--Float "Cone Angle"
Script.torsion = 45--Float "Torsion"
Script.hide = true--bool "Hide entity"

function Script:Start()
    local pos = self.entity:GetPosition(true)
    if self.parent~=nil then
        self.joint = Joint:Ball(pos.x, pos.y, pos.z, self.parent, self.child)
        self.joint:SetLimits(self.coneAngle, self.torsion)
        if self.useLimits then
            self.joint:EnableLimits()
            System:Print("limits:"..self.coneAngle..", "..self.torsion)
        end
        if self.hide then
            self.entity:Hide()
        end
    end
end

if using this script, you have to create a pivot or box (collision type: none, and no mass) and tell the script ho the parent is and ho the child is, if no child, then the parent body
will be attached to the environement, fixed at this 3d point.

In the map included in this blog there are two ropes, one made with the leadwerks ball.lua and one with the ballEnhaced.lua script, in this script you may tell how to constrain 
the ball cone anlgle and trosion. Look the video, and you will see the difference on how the cubes behave when hitted by the car.

A rope suspension bridge should be made with both ends attached to the world and a double chain of joints... doing two unions between each part of the brige.
It's nice so see the car passing over it!

The attached video shows at first the car passing over the bridge... the mass of the car is very little, if not, the joints get broken.. as always there are some other things to consider.

Hinje Joint
As I used a hinge for some explanation, i guess there is not to much to say here, I use some hinges for doors in the map attached and also used them for the steer and wheels of the car.
One door have a motor and the other not, so the second one moves as the car collides it. 

hinge.jpg.99a239f97a5609ef9e01f2ee0d098f74.jpg

I wrote two scripts: hingeEnhaced.lua

Script.parent = nil --entity "parent"
Script.child = nil --entity "hild"
Script.hide = true--bool "Hide entity"
Script.pin = Vec3(0,0,1) --Vec3 "Hinge Pin"
Script.limitsenabled=false--bool "Enable limits"
Script.limits = Vec2(-45,45) --Vec2 "Limits"
Script.friction = 0--float "Friction"

function Script:Start()
    local pos = self.entity:GetPosition(true)
    if self.parent~=nil then
        self.joint = Joint:Hinge(pos.x, pos.y, pos.z, self.pin.x, self.pin.y, self.pin.z, self.parent, self.child)
        if self.limitsenabled then self.joint:EnableLimits() end
        self.joint:SetLimits(self.limits.x,self.limits.y)
        self.joint:SetFriction(self.friction)
        self.entity:SetDamping(0,0)
        if self.hide then
            self.entity:Hide()
        end
    end
end

As in the case of a ball joint, with this script you may set parent, child and limits

hingeMotorized.lua

Script.parent = nil --entity "parent"
Script.child = nil --entity "hild"
Script.hide = true--bool "Hide entity"
Script.pin = Vec3(0,0,1) --Vec3 "Hinge Pin"
Script.limitsenabled=false--bool "Enable limits"
Script.limits = Vec2(-45,45) --Vec2 "Limits"
Script.tOpenClose = 15 --Int "Open/Close time"
Script.movespeed = 60 --Int "Speed"
Script.startTime = 0
Script.action = 1    --1 open, 0 close

function Script:Start()
    local pos = self.entity:GetPosition(true)
    if self.parent~=nil then
        self.joint = Joint:Hinge(pos.x, pos.y, pos.z, self.pin.x, self.pin.y, self.pin.z, self.parent, self.child)
        if self.limitsenabled then self.joint:EnableLimits() end
        self.joint:SetLimits(self.limits.x,self.limits.y)
        self.joint:SetTargetAngle(self.limits.x)
        self.joint:EnableMotor()
        self.startTime = Time:GetCurrent()
        self.joint:SetMotorSpeed(self.movespeed)
        if self.hide then
            self.entity:Hide()
        end
    end
end

function Script:UpdatePhysics()

    local time_ = Time:GetCurrent()
    if time_ - self.startTime > self.tOpenClose*1000 then
        
        self.action = 1-self.action    --toggle action
        if self.action == 1 then
            self.joint:SetTargetAngle(self.limits.x)
        else
            self.joint:SetTargetAngle(self.limits.y)
        end
        self.startTime = Time:GetCurrent()
    end

end

This scripts creates the joint, then set a target angle, enables the motor and apply a velocity, so the "door" starts going to the first limit.
After the tOpenClose time, the variable action is toggled and based on it the TargetAngle will be first limit or second limit, in this way, the door continously open and then close
after tOpenClose time


Slider Joint

slider.jpg.e1abef3780477e2c7b3f5dca404c3339.jpg


For a slider joint, in this map i wrote one script: sliderMotorized.lua

Script.child = nil --entity "hild"
Script.hide = true--bool "Hide entity"
Script.pin = Vec3(0,0,1) --Vec3 "Hinge Pin"
ScrScript.parent = nil --entity "parent"
ipt.limitsenabled=false--bool "Enable limits"
Script.limits = Vec2(-45,45) --Vec2 "Limits"
Script.tOpenClose = 15 --Int "Open/Close time"
Script.movespeed = 60 --Int "Speed"
Script.startTime = 0
Script.action = 1    --1 open, 0 close

function Script:Start()
    local pos = self.entity:GetPosition(true)
    if self.parent~=nil then
        self.joint = Joint:Slider(pos.x, pos.y, pos.z, self.pin.x, self.pin.y, self.pin.z, self.parent, self.child)
        if self.limitsenabled then self.joint:EnableLimits() end
        self.joint:SetLimits(self.limits.x,self.limits.y)
        self.joint:SetTargetAngle(self.limits.x)
        self.joint:EnableMotor()
        self.startTime = Time:GetCurrent()
        self.joint:SetMotorSpeed(self.movespeed)
        if self.hide then
            self.entity:Hide()
        end
    end
end

function Script:UpdatePhysics()

    local time_ = Time:GetCurrent()
    if time_ - self.startTime > self.tOpenClose*1000 then
        
        self.action = 1-self.action    --toggle action
        if self.action == 1 then
            self.joint:SetTargetAngle(self.limits.x)
        else
            self.joint:SetTargetAngle(self.limits.y)
        end
        self.startTime = Time:GetCurrent()
    end

end

This script is 99% the same as the hingeMotorized.lua, the only difference is that a slider joint is created instead of a hinge joint.
Note that the limis are not angles, they are offsets from the initial position of the plataform.

Stand alone executable:

jointsworldDistro.zip

Project:

jointsworldProject.zip

Enjoy

Juan

  • Like 4
  • Thanks 1
  • Upvote 1
 Share

8 Comments


Recommended Comments

I can understand many things but I can't apply them correctly, I want to make a simple door with a hinge, but it seems that the center point is at the global level of the scene and not in the door frame.

-- Puerta = Door.
-- self.entity  = Frame
-- Bisagra = Hinge
bisagra = Joint:Hinge( 0, 0, 0, 0, 1, 0, self.puerta, self.entity ) 
    bisagra:EnableLimits()

 

 

 




 

Link to comment

 

bisagra = Joint:Hinge( 0, 0, 0, 0, 1, 0, self.puerta, self.entity )

you are creating the joint at 0,0,0 so it will be at this position (bisagra = Joint:Hinge( 0, 0, 0, 0, 1, 0, self.puerta, self.entity )

you have to place the hinge in the correct position, to the left of the door. If you decide to create the hinge by code, then, look on the editor whic are the x,y,z world coodinates at the left of the door and use these values on your code.

Link to comment
30 minutes ago, Charrua said:

 


bisagra = Joint:Hinge( 0, 0, 0, 0, 1, 0, self.puerta, self.entity )

you are creating the joint at 0,0,0 so it will be at this position (bisagra = Joint:Hinge( 0, 0, 0, 0, 1, 0, self.puerta, self.entity )

you have to place the hinge in the correct position, to the left of the door. If you decide to create the hinge by code, then, look on the editor whic are the x,y,z world coodinates at the left of the door and use these values on your code.

Okay, thank you very much, I thought if the father was set, he'd take the father's coordinates to set the hinge coordinates. 

Now then I just have to create the hinge in the middle of the door, this would work like a revolving door, am I right?, 

is for the purpose of creating a tire. 



    bisagra = Joint:Hinge( self.entity:GetPosition().x, self.entity:GetPosition().y, self.entity:GetPosition().z, 0, 1, 0,  self.puerta , self.entity) 
    bisagra:EnableLimits()

Link to comment

yes, for a tire you have to place the hinge at the same position of the tire (which is it's center also)

  • Thanks 1
Link to comment
2 minutes ago, Charrua said:

yes, for a tire you have to place the hinge at the same position of the tire (which is it's center also)

And now the question is, how do I give the tire the power it needs to have engine effect in the car?  

When I activate the engine, I see it only goes in one direction. 

Link to comment

look in my previous blog: simple car, in it you have all that you need.

look at the wheel.lua script and see how do I control the velocity and in steer.lua how do I control steer.

read it carefully, try to learn how the whole project works.. and you will find what you need. The zip file included on the blog has the entire project which is basen on Lua exclusively so any Leadwerks user might use it.

 

  • Like 1
Link to comment

Ok, I've already understood something, and I really appreciate your help, as the google translator isn't that helpful with the documentation. 


With this broken hinge.
 

 

if self.motorspeed>0 then
        bisagra:SetAngle(bisagra:GetAngle()+100)
    

    else
        bisagra:SetAngle(bisagra:GetAngle()-100)
    end



And with this I set the speed of rotation. 

 

bisagra:SetMotorSpeed(self.motorspeed)

I feel that I have learned something new today, tomorrow will be another day and I will realize that there are things that I still don't know. 

Thank you very much. 

Link to comment

If you get lost, you may PM me in spanish, but I do prefere to continue the therad in english, so, it keeps being usefull for the rest of the members.

As you may know from my username, I'm from Uruguay, Montevideo, the southest Capital of South America :)

Charruas was the ones that habit these lands before european came.

Juan

 

  • Like 1
Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...