Difference between revisions of "B2.createMouseJointDef"

From GiderosMobile
(Created page with "__NOTOC__ '''Available since:''' Gideros 2011.6<br/> === Description === <br /> Creates and returns a mouse joint definition table with the bodies, world target point, maxForc...")
 
(6 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 +
<languages />
 
'''Available since:''' Gideros 2011.6<br/>
 
'''Available since:''' Gideros 2011.6<br/>
 +
'''Class:''' [[Special:MyLanguage/b2|b2]]<br/>
 +
 
=== Description ===
 
=== Description ===
<br />
+
Creates and returns a mouse joint definition table with the bodies, world target point, maxForce and optional frequencyHz and dampingRatio.
Creates and returns a mouse joint definition table with the bodies, world target point, maxForce and optional frequencyHz and dampingRatio.<br />
+
 
(Please refer to [[b2.World:createJoint]] function for more information about all the information needed to create a mouse joint).<br />
+
(Please refer to [[b2.World:createJoint]] function for more information about all the information needed to create a mouse joint).
<br />
 
 
<source lang="lua">
 
<source lang="lua">
(table), = b2.createMouseJointDef(bodyAbodyBtargetxtargetymaxForcefrequencyHzdampingRatio,)
+
(table) = b2.createMouseJointDef(bodyA,bodyB,targetx,targety,maxForce,frequencyHz,dampingRatio)
 
</source>
 
</source>
'''bodyA:''' (b2.Body) the first attached body ''''''<br/>
+
 
'''bodyB:''' (b2.Body) the second attached body ''''''<br/>
+
=== Parameters ===
'''targetx:''' (number) the x coordinate of the world target point ''''''<br/>
+
'''bodyA''': (b2.Body) the first attached body <br/>
'''targety:''' (number) the y coordinate of the world target point ''''''<br/>
+
'''bodyB''': (b2.Body) the second attached body <br/>
'''maxForce:''' (number) the maximum constraint force that can be exerted to move the candidate body ''''''<br/>
+
'''targetx''': (number) the x coordinate of the world target point <br/>
'''frequencyHz:''' (number, default = 5) the response speed ''''''<br/>
+
'''targety''': (number) the y coordinate of the world target point <br/>
'''dampingRatio:''' (number, default = 0.7) the damping ratio. 0 = no damping, 1 = critical damping ''''''<br/>
+
'''maxForce''': (number) the maximum constraint force that can be exerted to move the candidate body <br/>
 +
'''frequencyHz''': (number, default = 5) the response speed <br/>
 +
'''dampingRatio''': (number, default = 0.7) the damping ratio. 0 = no damping, 1 = critical damping <br/>
 +
 
 +
=== Return values ===
 
'''Returns''' (table) A new mouse joint definition table<br/>
 
'''Returns''' (table) A new mouse joint definition table<br/>
 +
 +
=== Examples ===
 +
<source lang="lua">
 +
--create empty box2d body for joint
 +
local ground = world:createBody({})
 +
 +
--joint with dummy body
 +
local mouseJoint = nil
 +
 +
-- create a mouse joint on mouse down
 +
function self:onMouseDown(event)
 +
local jointDef = b2.createMouseJointDef(ground, body, event.x, event.y, 100000)
 +
mouseJoint = world:createJoint(jointDef)
 +
end
 +
 +
-- update the target of mouse joint on mouse move
 +
function self:onMouseMove(event)
 +
if mouseJoint ~= nil then
 +
mouseJoint:setTarget(event.x, event.y)
 +
end
 +
end
 +
 +
-- destroy the mouse joint on mouse up
 +
function self:onMouseUp(event)
 +
if mouseJoint ~= nil then
 +
world:destroyJoint(mouseJoint)
 +
mouseJoint = nil
 +
end
 +
end
 +
</source>
 +
 +
{{B2}}

Revision as of 05:47, 17 February 2020


Available since: Gideros 2011.6
Class: b2

Description

Creates and returns a mouse joint definition table with the bodies, world target point, maxForce and optional frequencyHz and dampingRatio.

(Please refer to b2.World:createJoint function for more information about all the information needed to create a mouse joint).

(table) = b2.createMouseJointDef(bodyA,bodyB,targetx,targety,maxForce,frequencyHz,dampingRatio)

Parameters

bodyA: (b2.Body) the first attached body
bodyB: (b2.Body) the second attached body
targetx: (number) the x coordinate of the world target point
targety: (number) the y coordinate of the world target point
maxForce: (number) the maximum constraint force that can be exerted to move the candidate body
frequencyHz: (number, default = 5) the response speed
dampingRatio: (number, default = 0.7) the damping ratio. 0 = no damping, 1 = critical damping

Return values

Returns (table) A new mouse joint definition table

Examples

--create empty box2d body for joint
local ground = world:createBody({})

--joint with dummy body
local mouseJoint = nil

-- create a mouse joint on mouse down
function self:onMouseDown(event)
	local jointDef = b2.createMouseJointDef(ground, body, event.x, event.y, 100000)
	mouseJoint = world:createJoint(jointDef)
end

-- update the target of mouse joint on mouse move
function self:onMouseMove(event)
	if mouseJoint ~= nil then
		mouseJoint:setTarget(event.x, event.y)
	end
end

-- destroy the mouse joint on mouse up
function self:onMouseUp(event)
	if mouseJoint ~= nil then
		world:destroyJoint(mouseJoint)
		mouseJoint = nil
	end
end





LiquidFun