Difference between revisions of "love.physics.newMouseJoint"

 
m (It's MouseJoint even in 0.7)
 
(11 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 +
Create a joint between a body and the mouse.
  
 +
This joint actually connects the body to a fixed point in the world. To make it follow the mouse, the fixed point must be updated every timestep (example below).
 +
 +
The advantage of using a MouseJoint instead of just changing a body position directly is that collisions and reactions to other joints are handled by the physics engine.
  
 
== Function ==
 
== Function ==
Line 7: Line 11:
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|body|body|The body to attach to the mouse.}}
+
{{param|Body|body|The body to attach to the mouse.}}
 
{{param|number|x|The x position of the connecting point.}}
 
{{param|number|x|The x position of the connecting point.}}
 
{{param|number|y|The y position of the connecting point.}}
 
{{param|number|y|The y position of the connecting point.}}
 +
 
=== Returns ===
 
=== Returns ===
{{param|Joint|joint|The new mouse joint.}}
+
{{param|MouseJoint|joint|The new mouse joint.}}
 
== Examples ==
 
== Examples ==
===  ===
 
 
<source lang="lua">
 
<source lang="lua">
 
function love.load()
 
function love.load()
  w=love.physics.newWorld(1000,1000)
+
    world = love.physics.newWorld(0, 0)
  b=love.physics.newBody(w,love.mouse.getPosition())
+
    body = love.physics.newBody(world, love.mouse.getX(), love.mouse.getY(), "dynamic")
  s=love.physics.newCircleShape(b,0,0,20)
+
    shape = love.physics.newCircleShape(20)
  b:setMassFromShapes()
+
    fixture = love.physics.newFixture(body, shape)
  j=love.physics.newMouseJoint(b,love.mouse.getPosition())
+
    joint = love.physics.newMouseJoint(body, love.mouse.getPosition())
 
end
 
end
  
 
function love.draw()
 
function love.draw()
  love.graphics.circle("fill",b:getX(),b:getY(),s:getRadius(),20)
+
    love.graphics.circle("fill", body:getX(), body:getY(), shape:getRadius())
 
end
 
end
  
 
function love.update(dt)
 
function love.update(dt)
  j:setTarget(love.mouse.getPosition())
+
    joint:setTarget(love.mouse.getPosition())
  w:update(dt)
+
    world:update(dt)
 
end
 
end
 
</source>
 
</source>
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::love.physics]]
 
* [[parent::love.physics]]
 +
* [[Constructs::MouseJoint]]
 +
* [[Constructs::Joint]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=}}
+
{{#set:Description=Create a joint between a body and the mouse.}}
 +
{{#set:Since=000}}
 +
== Other Languages ==
 +
{{i18n|love.physics.newMouseJoint}}

Latest revision as of 19:03, 26 December 2016

Create a joint between a body and the mouse.

This joint actually connects the body to a fixed point in the world. To make it follow the mouse, the fixed point must be updated every timestep (example below).

The advantage of using a MouseJoint instead of just changing a body position directly is that collisions and reactions to other joints are handled by the physics engine.

Function

Synopsis

joint = love.physics.newMouseJoint( body, x, y )

Arguments

Body body
The body to attach to the mouse.
number x
The x position of the connecting point.
number y
The y position of the connecting point.

Returns

MouseJoint joint
The new mouse joint.

Examples

function love.load()
    world = love.physics.newWorld(0, 0)
    body = love.physics.newBody(world, love.mouse.getX(), love.mouse.getY(), "dynamic")
    shape = love.physics.newCircleShape(20)
    fixture = love.physics.newFixture(body, shape)
    joint = love.physics.newMouseJoint(body, love.mouse.getPosition())
end

function love.draw()
    love.graphics.circle("fill", body:getX(), body:getY(), shape:getRadius())
end

function love.update(dt)
    joint:setTarget(love.mouse.getPosition())
    world:update(dt)
end

See Also


Other Languages