Joints

Greetings Everyone,

I’m a beginner in Corona, and I’m now playing around with the physics engine.
I’m trying to make a car that has a shock absorber.

I came across this sample : http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/
And if I get it right, it’s using prismatic joints for the shock, revolute joints for the wheels.
I’m trying to make the same thing in Corona, but to no avail.
I used Piston Joints (correct me if I’m wrong, I saw in the documentation that it is the equivalent of Prismatic Joints in Corona) and pivot joints for the wheels.

The problem I have is that, the shock moves on both axes, instead of moving in just the Y Axis.
I’m stuck with this issue, and I’d really appreciate any help that comes my way.

Thanks in advance ~ [import]uid: 193472 topic_id: 34479 reply_id: 334479[/import]

Can you post your code, as simple as possible, to show what you’re building? [import]uid: 8271 topic_id: 34479 reply_id: 137086[/import]

Hi !

Thanks for the reply.

Here is what I have at the moment.

local physics = require "physics"  
physics.start()  
physics.setDrawMode("hybrid");  
display.setDefault("fillColor", 135, 203, 105, 200);  
  
local carBody = display.newRect(50, 50, 100, 50);  
local wheelFront = display.newCircle(carBody.x + 30, carBody.y + 20, 20, 20);  
local wheelBack = display.newCircle(carBody.x - 30, carBody.y + 20, 20, 20);  
local shockBaseFront = display.newRect(carBody.x + 30, carBody.y - 10, 10, 20);  
local shockBaseBack = display.newRect(carBody.x - 40, carBody.y - 10, 10, 20);  
local shockFront = display.newRect(carBody.x + 25, carBody.y, 5, 25);  
local shockBack = display.newRect(carBody.x - 35, carBody.y, 5, 25);  
  
physics.addBody(carBody, {density = 1.0, friction = 0, bounce = 0});  
physics.addBody(wheelFront, {density = 1.0, friction = 4.0, bounce = 0.2, radius = 20});  
physics.addBody(wheelBack, {density = 1.0, friction = 4.0, bounce = 0.2, radius = 20});  
physics.addBody(shockBaseFront, {density = 1.0, friction = 0, bounce = 0});  
physics.addBody(shockBaseBack, {density = 1.0, friction = 0, bounce = 0});  
physics.addBody(shockFront, {density = 1.0, friction = 0, bounce = 0});  
physics.addBody(shockBack, {density = 1.0, friction = 0, bounce = 0});  
  
local shockBaseFrontJoint = physics.newJoint("weld", carBody, shockBaseFront, shockBaseFront.x, shockBaseFront.y);  
local shockBaseBackJoint = physics.newJoint("weld", carBody, shockBaseBack, shockBaseBack.x, shockBaseBack.y);  
local shockFrontJoint = physics.newJoint("piston", shockBaseFront, shockFront, shockBaseFront.x, shockBaseFront.y, 0, 1);  
local shockBackJoint = physics.newJoint("piston", shockBaseBack, shockBack, shockBaseBack.x, shockBaseBack.y, 0, 1);  
local wheelFrontJoint = physics.newJoint("pivot", shockFront, wheelFront, wheelFront.x, wheelFront.y);  
local wheelBackJoint = physics.newJoint("pivot", shockBack, wheelBack, wheelBack.x, wheelBack.y);  
  
wheelFrontJoint.isMotorEnabled = true;  
wheelFrontJoint.motorSpeed = 100;  
wheelFrontJoint.maxMotorTorque = 100000;  
   
wheelBackJoint.isMotorEnabled = true;  
wheelBackJoint.motorSpeed = 100;  
wheelBackJoint.maxMotorTorque = 100000;  
  
local terrain = display.newRect(0, 250, 480, 20);  
physics.addBody(terrain, "static", {density = 1.0, friction = 0.3, bounce = 0.2});  

Is the problem in the way I’m defining the joints ?
Thanks in advance ~ [import]uid: 193472 topic_id: 34479 reply_id: 137091[/import]

Sorry, I’ve not had a chance to look at this all day until now. It is very strange. I’ll let you know if I figure out anything. [import]uid: 8271 topic_id: 34479 reply_id: 137116[/import]

Ok, I think the problem was that the shock absorbers don’t need to be two piece units. Once I removed the base component I realised that the wheels were colliding with the body of the vehicle and the collision masking needed to be set. I used the easy route and simply set the collision filter groupIndex value. Take a look…

[lua]-- shock absorber

http://developer.coronalabs.com/content/game-edition-collision-detection#Collision_categories_masking_and_groups
http://developer.coronalabs.com/content/game-edition-physics-joints#Piston_joint

local physics = require “physics”
physics.start()
physics.setDrawMode(“hybrid”);
display.setDefault(“fillColor”, 135, 203, 105, 200)

local anchor = display.newCircle(-100,-100,10)
local terrain = display.newRect(0, 250, 480, 20)
local carBody = display.newRect(50, 50, 100, 50);
local shockFront = display.newRect(carBody.x + 25, carBody.y, 5, 25);
local shockBack = display.newRect(carBody.x - 35, carBody.y, 5, 25);
local wheelFront = display.newCircle(carBody.x + 30, carBody.y + 20, 20, 20);
local wheelBack = display.newCircle(carBody.x - 30, carBody.y + 20, 20, 20);

physics.addBody(anchor,“static”)
physics.addBody(terrain, {density = 1.0, friction = 0.3, bounce = 0.2})
physics.addBody(carBody, {density = 1.0, friction = 0.3, bounce = 0, filter={groupIndex=-1}});
physics.addBody(shockFront, {density = 1.0, friction = 0.3, bounce = 0});
physics.addBody(shockBack, {density = 1.0, friction = 0.3, bounce = 0});
physics.addBody(wheelFront, {density = 1.0, friction = 4.0, bounce = 0.2, radius = 20, filter={groupIndex=-1}});
physics.addBody(wheelBack, {density = 1.0, friction = 4.0, bounce = 0.2, radius = 20, filter={groupIndex=-1}});

local groundweld = physics.newJoint(“weld”,anchor,terrain,terrain.x,terrain.y)
local shockFrontJoint = physics.newJoint(“piston”, carBody, shockFront, shockFront.x, shockFront.y, 0, 10);
local shockBackJoint = physics.newJoint(“piston”, carBody, shockBack, shockBack.x, shockBack.y, 0, 10);
local wheelFrontJoint = physics.newJoint(“pivot”, shockFront, wheelFront, wheelFront.x, wheelFront.y);
local wheelBackJoint = physics.newJoint(“pivot”, shockBack, wheelBack, wheelBack.x, wheelBack.y);

shockFrontJoint.isLimitEnabled = true
shockFrontJoint:setLimits( 5, 15 )

wheelFrontJoint.isMotorEnabled = true;
wheelFrontJoint.motorSpeed = 200;
wheelFrontJoint.maxMotorTorque = 100000;

shockBackJoint.isLimitEnabled = true
shockBackJoint:setLimits( 5, 15 )

wheelBackJoint.isMotorEnabled = true;
wheelBackJoint.motorSpeed = 200;
wheelBackJoint.maxMotorTorque = 100000;[/lua] [import]uid: 8271 topic_id: 34479 reply_id: 137130[/import]

Wow, awesome, Sir.
Never thought of removing the base of the shock (having a base for the shock should’ve made sense though, XD)

Thanks ~ [import]uid: 193472 topic_id: 34479 reply_id: 137145[/import]

Can you post your code, as simple as possible, to show what you’re building? [import]uid: 8271 topic_id: 34479 reply_id: 137086[/import]

Hi !

Thanks for the reply.

Here is what I have at the moment.

local physics = require "physics"  
physics.start()  
physics.setDrawMode("hybrid");  
display.setDefault("fillColor", 135, 203, 105, 200);  
  
local carBody = display.newRect(50, 50, 100, 50);  
local wheelFront = display.newCircle(carBody.x + 30, carBody.y + 20, 20, 20);  
local wheelBack = display.newCircle(carBody.x - 30, carBody.y + 20, 20, 20);  
local shockBaseFront = display.newRect(carBody.x + 30, carBody.y - 10, 10, 20);  
local shockBaseBack = display.newRect(carBody.x - 40, carBody.y - 10, 10, 20);  
local shockFront = display.newRect(carBody.x + 25, carBody.y, 5, 25);  
local shockBack = display.newRect(carBody.x - 35, carBody.y, 5, 25);  
  
physics.addBody(carBody, {density = 1.0, friction = 0, bounce = 0});  
physics.addBody(wheelFront, {density = 1.0, friction = 4.0, bounce = 0.2, radius = 20});  
physics.addBody(wheelBack, {density = 1.0, friction = 4.0, bounce = 0.2, radius = 20});  
physics.addBody(shockBaseFront, {density = 1.0, friction = 0, bounce = 0});  
physics.addBody(shockBaseBack, {density = 1.0, friction = 0, bounce = 0});  
physics.addBody(shockFront, {density = 1.0, friction = 0, bounce = 0});  
physics.addBody(shockBack, {density = 1.0, friction = 0, bounce = 0});  
  
local shockBaseFrontJoint = physics.newJoint("weld", carBody, shockBaseFront, shockBaseFront.x, shockBaseFront.y);  
local shockBaseBackJoint = physics.newJoint("weld", carBody, shockBaseBack, shockBaseBack.x, shockBaseBack.y);  
local shockFrontJoint = physics.newJoint("piston", shockBaseFront, shockFront, shockBaseFront.x, shockBaseFront.y, 0, 1);  
local shockBackJoint = physics.newJoint("piston", shockBaseBack, shockBack, shockBaseBack.x, shockBaseBack.y, 0, 1);  
local wheelFrontJoint = physics.newJoint("pivot", shockFront, wheelFront, wheelFront.x, wheelFront.y);  
local wheelBackJoint = physics.newJoint("pivot", shockBack, wheelBack, wheelBack.x, wheelBack.y);  
  
wheelFrontJoint.isMotorEnabled = true;  
wheelFrontJoint.motorSpeed = 100;  
wheelFrontJoint.maxMotorTorque = 100000;  
   
wheelBackJoint.isMotorEnabled = true;  
wheelBackJoint.motorSpeed = 100;  
wheelBackJoint.maxMotorTorque = 100000;  
  
local terrain = display.newRect(0, 250, 480, 20);  
physics.addBody(terrain, "static", {density = 1.0, friction = 0.3, bounce = 0.2});  

Is the problem in the way I’m defining the joints ?
Thanks in advance ~ [import]uid: 193472 topic_id: 34479 reply_id: 137091[/import]

Sorry, I’ve not had a chance to look at this all day until now. It is very strange. I’ll let you know if I figure out anything. [import]uid: 8271 topic_id: 34479 reply_id: 137116[/import]

Ok, I think the problem was that the shock absorbers don’t need to be two piece units. Once I removed the base component I realised that the wheels were colliding with the body of the vehicle and the collision masking needed to be set. I used the easy route and simply set the collision filter groupIndex value. Take a look…

[lua]-- shock absorber

http://developer.coronalabs.com/content/game-edition-collision-detection#Collision_categories_masking_and_groups
http://developer.coronalabs.com/content/game-edition-physics-joints#Piston_joint

local physics = require “physics”
physics.start()
physics.setDrawMode(“hybrid”);
display.setDefault(“fillColor”, 135, 203, 105, 200)

local anchor = display.newCircle(-100,-100,10)
local terrain = display.newRect(0, 250, 480, 20)
local carBody = display.newRect(50, 50, 100, 50);
local shockFront = display.newRect(carBody.x + 25, carBody.y, 5, 25);
local shockBack = display.newRect(carBody.x - 35, carBody.y, 5, 25);
local wheelFront = display.newCircle(carBody.x + 30, carBody.y + 20, 20, 20);
local wheelBack = display.newCircle(carBody.x - 30, carBody.y + 20, 20, 20);

physics.addBody(anchor,“static”)
physics.addBody(terrain, {density = 1.0, friction = 0.3, bounce = 0.2})
physics.addBody(carBody, {density = 1.0, friction = 0.3, bounce = 0, filter={groupIndex=-1}});
physics.addBody(shockFront, {density = 1.0, friction = 0.3, bounce = 0});
physics.addBody(shockBack, {density = 1.0, friction = 0.3, bounce = 0});
physics.addBody(wheelFront, {density = 1.0, friction = 4.0, bounce = 0.2, radius = 20, filter={groupIndex=-1}});
physics.addBody(wheelBack, {density = 1.0, friction = 4.0, bounce = 0.2, radius = 20, filter={groupIndex=-1}});

local groundweld = physics.newJoint(“weld”,anchor,terrain,terrain.x,terrain.y)
local shockFrontJoint = physics.newJoint(“piston”, carBody, shockFront, shockFront.x, shockFront.y, 0, 10);
local shockBackJoint = physics.newJoint(“piston”, carBody, shockBack, shockBack.x, shockBack.y, 0, 10);
local wheelFrontJoint = physics.newJoint(“pivot”, shockFront, wheelFront, wheelFront.x, wheelFront.y);
local wheelBackJoint = physics.newJoint(“pivot”, shockBack, wheelBack, wheelBack.x, wheelBack.y);

shockFrontJoint.isLimitEnabled = true
shockFrontJoint:setLimits( 5, 15 )

wheelFrontJoint.isMotorEnabled = true;
wheelFrontJoint.motorSpeed = 200;
wheelFrontJoint.maxMotorTorque = 100000;

shockBackJoint.isLimitEnabled = true
shockBackJoint:setLimits( 5, 15 )

wheelBackJoint.isMotorEnabled = true;
wheelBackJoint.motorSpeed = 200;
wheelBackJoint.maxMotorTorque = 100000;[/lua] [import]uid: 8271 topic_id: 34479 reply_id: 137130[/import]

Wow, awesome, Sir.
Never thought of removing the base of the shock (having a base for the shock should’ve made sense though, XD)

Thanks ~ [import]uid: 193472 topic_id: 34479 reply_id: 137145[/import]