B2.Body

From GiderosMobile
Revision as of 14:20, 23 August 2018 by Hgy29 (talk | contribs)

Supported platforms: android, ios, mac, pc
Available since: Gideros 2011.6

Description


A rigid body. These are created via b2.World:createBody.

Examples

Creating Box2d body and moving Bitmap along the body

require "box2d"
local world = b2.World.new(0, 10, true)

--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(100,100)
 
--get radius
local radius = ball:getWidth()/2
 
--create box2d physical object
local body = world:createBody{type = b2.DYNAMIC_BODY}
local circle = b2.CircleShape.new(0, 0, radius)
local fixture = body:createFixture{shape = circle, density = 1.0, 
friction = 0.1, restitution = 0.2}
ball.body = body
 
--add to scene
stage:addChild(ball)

stage:addEventListener(Event.ENTER_FRAME, function()
	-- edit the step values if required. These are good defaults!
    world:step(1/60, 8, 3)
	ball:setPosition(ball.body:getPosition())
	ball:setRotation(math.rad(ball.body:getAngle()))
end)

Methods

b2.Body:applyAngularImpulse Template:Applies an angular impulse
b2.Body:applyForce Template:Applies a force at a world point
b2.Body:applyLinearImpulse Template:Applies an impulse at a point
b2.Body:applyTorque Template:Applies a torque
b2.Body:createFixture Template:Creates a fixture and attach it to this body
b2.Body:destroyFixture Template:Destroys a fixture
b2.Body:getAngle Template:Returns the current world rotation angle in radians
b2.Body:getAngularDamping Template:Returns the angular damping of the body
b2.Body:getAngularVelocity Template:Returns the angular velocity
b2.Body:getGravityScale Template:Returns the gravity scale of the body
b2.Body:getInertia Template:Returns the rotational inertia of the body about the local origin in kg-m^2
b2.Body:getLinearDamping Template:Returns the linear damping of the body
b2.Body:getLinearVelocity Template:Returns the linear velocity of the center of mass
b2.Body:getLocalCenter Template:Returns the local position of the center of mass
b2.Body:getLocalPoint {{}}
b2.Body:getLocalVector {{}}
b2.Body:getMass Template:Returns the total mass of the body in kilograms (kg)
b2.Body:getPosition Template:Returns the world body origin position
b2.Body:getWorldCenter Template:Returns the world position of the center of mass
b2.Body:getWorldPoint {{}}
b2.Body:getWorldVector {{}}
b2.Body:isActive Template:Returns the active state of the body
b2.Body:isAwake Template:Returns the sleeping state of the body
b2.Body:isBullet {{}}
b2.Body:isFixedRotation {{}}
b2.Body:isSleepingAllowed {{}}
b2.Body:setActive Template:Sets the active state of the body
b2.Body:setAngle {{}}
b2.Body:setAngularDamping Template:Sets the angular damping of the body
b2.Body:setAngularVelocity Template:Sets the angular velocity
b2.Body:setAwake Template:Sets the sleep state of the body
b2.Body:setBullet {{}}
b2.Body:setFixedRotation {{}}
b2.Body:setGravityScale Template:Sets the gravity scale of the body
b2.Body:setLinearDamping Template:Sets the linear damping of the body
b2.Body:setLinearVelocity Template:Sets the linear velocity of the center of mass
b2.Body:setPosition Template:Sets the world body origin position
b2.Body:setSleepingAllowed {{}}

Events

Constants