B2.MouseJoint: Difference between revisions

From GiderosMobile
Created page with "__NOTOC__ '''Supported platforms:''' <br/> '''Available since:''' Gideros 2011.6<br/> === Description === <br /> A mouse joint is used to make a point on a body track a specif..."
 
No edit summary
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
'''Supported platforms:''' <br/>
<!-- GIDEROSOBJ:b2.MouseJoint -->
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
'''Available since:''' Gideros 2011.6<br/>
'''Available since:''' Gideros 2011.6<br/>
'''Inherits from:''' [[b2.Joint]]<br/>
=== Description ===
=== Description ===
<br />
A mouse joint is used to make a point on a body track a specified world point. This is a soft constraint with a maximum force. This allows the constraint to stretch without applying huge forces.
A mouse joint is used to make a point on a body track a specified world point. This a soft constraint with a maximum force. This allows the constraint to stretch and without applying huge forces.<br />
 
<br />
=== Examples ===
'''Mouse joint'''
<syntaxhighlight 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
</syntaxhighlight>
 
{|-
{|-
| style="width: 50%;"|
| style="width: 50%; vertical-align:top;"|
=== Methods ===
=== Methods ===
| style="width: 50%;"|
[[b2.MouseJoint:getDampingRatio]] ''returns the damping ratio''<br/><!--GIDEROSMTD:b2.MouseJoint:getDampingRatio() returns the damping ratio-->
[[b2.MouseJoint:getFrequency]] ''returns the response frequency in Hertz''<br/><!--GIDEROSMTD:b2.MouseJoint:getFrequency() returns the response frequency in Hertz-->
[[b2.MouseJoint:getMaxForce]] ''returns the maximum force in N''<br/><!--GIDEROSMTD:b2.MouseJoint:getMaxForce() returns the maximum force in N-->
[[b2.MouseJoint:getTarget]] ''returns the x and y coordinates of the target point''<br/><!--GIDEROSMTD:b2.MouseJoint:getTarget() returns the x and y coordinates of the target point-->
[[b2.MouseJoint:setDampingRatio]] ''sets the damping ratio (0 = no damping, 1 = critical damping)''<br/><!--GIDEROSMTD:b2.MouseJoint:setDampingRatio(ratio) sets the damping ratio (0 = no damping, 1 = critical damping)-->
[[b2.MouseJoint:setFrequency]] ''sets the response frequency in Hertz''<br/><!--GIDEROSMTD:b2.MouseJoint:setFrequency(frequency) sets the response frequency in Hertz-->
[[b2.MouseJoint:setMaxForce]] ''sets the maximum force in N''<br/><!--GIDEROSMTD:b2.MouseJoint:setMaxForce(force) sets the maximum force in N-->
[[b2.MouseJoint:setTarget]] ''updates the target point''<br/><!--GIDEROSMTD:b2.MouseJoint:setTarget(x,y) updates the target point-->
 
| style="width: 50%; vertical-align:top;"|
=== Events ===
=== Events ===
=== Constants ===
=== Constants ===
|}
|}
----
*'''[[B2.Joint]]'''
*'''[[LiquidFun]]'''

Latest revision as of 00:57, 1 December 2023

Supported platforms:
Available since: Gideros 2011.6
Inherits from: b2.Joint

Description

A mouse joint is used to make a point on a body track a specified world point. This is a soft constraint with a maximum force. This allows the constraint to stretch without applying huge forces.

Examples

Mouse joint

--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

Methods

b2.MouseJoint:getDampingRatio returns the damping ratio
b2.MouseJoint:getFrequency returns the response frequency in Hertz
b2.MouseJoint:getMaxForce returns the maximum force in N
b2.MouseJoint:getTarget returns the x and y coordinates of the target point
b2.MouseJoint:setDampingRatio sets the damping ratio (0 = no damping, 1 = critical damping)
b2.MouseJoint:setFrequency sets the response frequency in Hertz
b2.MouseJoint:setMaxForce sets the maximum force in N
b2.MouseJoint:setTarget updates the target point

Events

Constants