Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Box2d DstanceJoint — Gideros Forum

Box2d DstanceJoint

gaboritalygaboritaly Member
edited August 2012 in General questions
Hi ,
I little modified the Arturs code http://appcodingeasy.com/Gideros-Mobile/Dragging-Box2d-object-in-Gideros-Mobile
Added the distancejoint, but not work well how I waited, isn't fluid and isn't maintain the distance (it's similar to rope joint, what I would like in the future :) ).
I want to doing the examples what I found here, if you click on the photo will ope the live Flash examples.
http://blog.allanbishop.com/box2d-2-1a-tutorial-part-2-joints/
I would learn ad doing that effect in Gideros.
My questions are:
How can I set the distance between 2 object?
How can I fix the the distance between 2 objects without enlarge ?
When I click on the circle why has a strange animation with choppy ?
In the reference manual I found 2 groups in manual :
1)
Distance Joint

Distance joint definition. This requires defining an anchor point on both bodies and the non-zero length of the distance joint. The definition uses local anchor points so that the initial configuration can violate the constraint slightly. This helps when saving and loading a game.

type: (number) b2.DISTANCE_JOINT
bodyA: (b2.Body) The first attached body.
bodyB: (b2.Body) The second attached body.
collideConnected: (boolean) Set this flag to true if the attached bodies should collide.
localAnchorA: (table) The local anchor point relative to bodyA’s origin.
localAnchorB: (table) The local anchor point relative to bodyB’s origin.
length: (number) The natural length between the anchor points. Do not use a zero or short length.
frequencyHz: (number) The mass-spring-damper frequency in Hertz.
dampingRatio: (number) The damping ratio. 0 = no damping, 1 = critical damping.
2)
b2.createDistanceJointDef(bodyA, bodyB, anchorAx, anchorAy, anchorBx, anchorBy)

What is the difference 2 chapter, and how can I use well them ?

Sorry for a lot of questions, but for me is it really hard to learn without code examples in the documentations.
And thank you in advance to be patience and help.

this my code
require "box2d"
 
local physicsScale = 30
b2.setScale(physicsScale)
 
local world = b2.World.new(0, 10, true)
 
-- for creating objects using shape
-- as example - bounding walls
function wall(x, y, width, height, name)
	local wall = Shape.new()
	--define wall shape
	wall:beginPath()
	--we make use (0;0) as center of shape,
	--thus we have half of width and half of height in each direction
	wall:moveTo(-width/2,-height/2)
	wall:lineTo(width/2, -height/2)
	wall:lineTo(width/2, height/2)
	wall:lineTo(-width/2, height/2)
	wall:closePath()
	wall:endPath()
	wall:setPosition(x,y)
 
	--create box2d physical object
	local body = world:createBody{type = b2.STATIC_BODY}
	body:setPosition(wall:getX(), wall:getY())
	body.name= name
	--body:setAngle(wall:getRotation() * math.pi/180)
	local poly = b2.PolygonShape.new()
	poly:setAsBox(wall:getWidth()/2, wall:getHeight()/2)
	local fixture = body:createFixture{shape = poly, density = 1.0, 
	friction = 0.1, restitution = 0.8}
	wall.body = body
	wall.body.type = "wall"
 
	--add to scene
	stage:addChild(wall)
 
	--return created object
	return wall
end
 
local screenH = application:getContentHeight()
local screenW= application:getContentWidth()
 
--create bounding walls outside the scene
wall(-dx,screenH/2,10,screenH) --left
wall(screenW/2,-dy,screenW+2*dx,10) --top
wall(screenW+dx,screenH/2,10,screenH)--right
wall(screenW/2,screenH,screenW+2*dx,10) --bottom
 
--create empty box2d body for joint
--since mouse cursor is not a body
--we need dummy body to create joint
local ground = world:createBody({})
 
--joint with dummy body
local mouseJoint = nil
local distanceJoint = nil
	-- create a mouse joint on mouse down
function onMouseDown(event)
	if ball:hitTestPoint(event.x, event.y) then
			--ball.body:setPosition(event.x, event.y)
		local jointDef = b2.createMouseJointDef(ground, ball.body, event.x, event.y, 100000)
		mouseJoint = world:createJoint(jointDef)	
	end
end
 
	-- update the target of mouse joint on mouse move
function onMouseMove(event)
	if mouseJoint ~= nil then
		mouseJoint:setTarget(event.x, event.y)
	end
end
 
	-- destroy the mouse joint on mouse up
function onMouseUp(event)
	if mouseJoint ~= nil then
		world:destroyJoint(mouseJoint)
		mouseJoint = nil
	end
end
-- for creating objects using image
-- as example - ball
function createBall(x, y)
	--create ball bitmap object from ball graphic
	local ball = Bitmap.new(Texture.new("ball.png"))
	--reference center of the ball for positioning
	ball:setAnchorPoint(0.5,0.5)
 
	ball:setPosition(x,y)
 
	--get radius
	local radius = ball:getWidth()/2
 
	--create box2d physical object
	local body = world:createBody{type = b2.DYNAMIC_BODY}
	body.name ='ball'
	print(ball:getX()..'    '.. ball:getY())
	body:setPosition(ball:getX(), ball:getY())
	body:setAngle(ball:getRotation() * math.pi/180)
	local circle = b2.CircleShape.new(0, 0, radius)
	local fixture = body:createFixture{shape = circle, density = 1.0, 
	friction = 0.1, restitution = 0.8}
	ball.body = body
	ball.body.type = "ball"
 
	--add to scene
	stage:addChild(ball)
 
	--return created object
	return ball
end
 
ball =createBall(application:getContentWidth()/2, 100)
ball1 =createBall(200, 200)
 
local dj = b2.createDistanceJointDef(ball.body, ball1.body, 0, 0, 0, 0, 100)
distanceJoint = world:createJoint(dj)
--distanceJoint.setDampingRatio(0) not work 
 
 
function onEnterFrame() 
	-- edit the step values if required. These are good defaults!
	world:step(1/60, 8, 3)
	--iterate through all child sprites
	for i = 1, stage:getNumChildren() do
		--get specific sprite
		local sprite = stage:getChildAt(i)
		-- check if sprite HAS a body (ie, physical object reference we added)
		if sprite.body then
			--update position to match box2d world object's position
			--get physical body reference
			local body = sprite.body
			--get body coordinates
			local bodyX, bodyY = body:getPosition()
			--apply coordinates to sprite
			sprite:setPosition(bodyX, bodyY)
			--apply rotation to sprite
			sprite:setRotation(body:getAngle() * 180 / math.pi)
		end
	end
end
 
local debugDraw = b2.DebugDraw.new()
world:setDebugDraw(debugDraw)
stage:addChild(debugDraw)
 
 
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
-- register for mouse events
stage:addEventListener(Event.MOUSE_DOWN, onMouseDown)
stage:addEventListener(Event.MOUSE_MOVE, onMouseMove)
stage:addEventListener(Event.MOUSE_UP, onMouseUp)

Likes: nick, Maloolam

+1 -1 (+2 / -0 )Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited August 2012
    Hello,
    this line:
    local dj = b2.createDistanceJointDef(ball.body, ball1.body, 0, 0, 0, 0, 100)
    should be:
    local dj = b2.createDistanceJointDef(ball.body, ball1.body, application:getContentWidth()/2, 100, 200, 200)
    taken from:
    ball =createBall(application:getContentWidth()/2, 100)
    ball1 =createBall(200, 200)
    As stated in b2.createDistanceJointDef definition docs

    anchorAx: (number) the x coordinate of the world anchor point of bodyA
    anchorAy: (number) the y coordinate of the world anchor point of bodyA
    anchorBx: (number) the x coordinate of the world anchor point of bodyB
    anchorBy: (number) the y coordinate of the world anchor point of bodyB

    All of them are the coordinates of the world

    Other definition is a definition of DISTANCE_JOINT object which is returned by
    distanceJoint = world:createJoint(dj)
    these are probably it's properties and somewhere in docs there are also methods defined.

    You provided a really interesting link, I'll create similar examples with Gideros when I'll have time ;)

    Edited:
    Ok, other definition if you do not want to create joint definition using b2.createDistanceJointDef but create table with described properties and pass it to world:createJoint your self. Don't know, never tried and used it. I always prefer to use proper createSomeJoint method ;)
  • You are BIG :)>- :)>- :)>- :)>- :)>- :)>- :)>- :)>-
    Thank you @ar2rsawseen, Im thinking work similar to setAnchorpoint ..
    Well now learning box2d, and probe doing that examples.
    If you want, I can post here ,and you can control it.
    I'm really sure, I'll need some times your knowledge ;))

  • Sure post what you have here, and if any obstacles, we'll try to figure out together ;)
Sign In or Register to comment.