Difference between revisions of "B2.Body"
| Line 2: | Line 2: | ||
| <languages /> | <languages /> | ||
| <!-- GIDEROSOBJ:b2.Body --> | <!-- GIDEROSOBJ:b2.Body --> | ||
| − | ''' | + | '''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/> | 
| − | + | ||
| − | ===  | + | === Description === | 
| − | + | A rigid body. These are created via [[Special:MyLanguage/B2.World:createBody|B2.World:createBody]]. | |
| − | A rigid body. These are created via [[Special:MyLanguage/ | + | |
| − | + | === Examples === | |
| − | ===  | + | '''Creating Box2d body and moving Bitmap along the body''' | 
| − | '''Creating Box2d body and moving Bitmap along the body''' | + | <source lang="lua"> | 
| − | <source lang="lua">require "box2d" | + | require "box2d" | 
| local world = b2.World.new(0, 10, true) | local world = b2.World.new(0, 10, true) | ||
| Line 18: | Line 18: | ||
| --reference center of the ball for positioning | --reference center of the ball for positioning | ||
| ball:setAnchorPoint(0.5,0.5) | ball:setAnchorPoint(0.5,0.5) | ||
| − | |||
| ball:setPosition(100,100) | ball:setPosition(100,100) | ||
| Line 36: | Line 35: | ||
| stage:addEventListener(Event.ENTER_FRAME, function() | stage:addEventListener(Event.ENTER_FRAME, function() | ||
| 	-- edit the step values if required. These are good defaults! | 	-- edit the step values if required. These are good defaults! | ||
| − | + | 	world:step(1/60, 8, 3) | |
| 	ball:setPosition(ball.body:getPosition()) | 	ball:setPosition(ball.body:getPosition()) | ||
| 	ball:setRotation(math.rad(ball.body:getAngle())) | 	ball:setRotation(math.rad(ball.body:getAngle())) | ||
| − | end)</source> | + | end) | 
| + | </source> | ||
| + | |||
| {|- | {|- | ||
| | style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
| − | ===  | + | === Methods === | 
| − | [[Special:MyLanguage/b2.Body:applyAngularImpulse|b2.Body:applyAngularImpulse]] '' | + | [[Special:MyLanguage/b2.Body:applyAngularImpulse|b2.Body:applyAngularImpulse]] ''applies an angular impulse''<br/><!-- GIDEROSMTD:b2.Body:applyAngularImpulse(impulse) applies an angular impulse --> | 
| − | [[Special:MyLanguage/b2.Body:applyForce|b2.Body:applyForce]] '' | + | [[Special:MyLanguage/b2.Body:applyForce|b2.Body:applyForce]] ''applies a force at a world point''<br/><!-- GIDEROSMTD:b2.Body:applyForce(forcex,forcey,pointx,pointy) applies a force at a world point --> | 
| − | [[Special:MyLanguage/b2.Body:applyLinearImpulse|b2.Body:applyLinearImpulse]] '' | + | [[Special:MyLanguage/b2.Body:applyLinearImpulse|b2.Body:applyLinearImpulse]] ''applies an impulse at a point''<br/><!-- GIDEROSMTD:b2.Body:applyLinearImpulse(impulsex,impulsey,pointx,pointy) applies an impulse at a point --> | 
| − | [[Special:MyLanguage/b2.Body:applyTorque|b2.Body:applyTorque]] '' | + | [[Special:MyLanguage/b2.Body:applyTorque|b2.Body:applyTorque]] ''applies a torque''<br/><!-- GIDEROSMTD:b2.Body:applyTorque(torque) applies a torque --> | 
| − | [[Special:MyLanguage/b2.Body:createFixture|b2.Body:createFixture]] '' | + | [[Special:MyLanguage/b2.Body:createFixture|b2.Body:createFixture]] ''creates a fixture and attach it to this body''<br/><!-- GIDEROSMTD:b2.Body:createFixture(fixtureDef) creates a fixture and attach it to this body --> | 
| − | [[Special:MyLanguage/b2.Body:destroyFixture|b2.Body:destroyFixture]] '' | + | [[Special:MyLanguage/b2.Body:destroyFixture|b2.Body:destroyFixture]] ''destroys a fixture''<br/><!-- GIDEROSMTD:b2.Body:destroyFixture(fixture) destroys a fixture --> | 
| − | [[Special:MyLanguage/b2.Body:getAngle|b2.Body:getAngle]] '' | + | [[Special:MyLanguage/b2.Body:getAngle|b2.Body:getAngle]] ''returns the current world rotation angle in radians''<br/><!-- GIDEROSMTD:b2.Body:getAngle() returns the current world rotation angle in radians --> | 
| − | [[Special:MyLanguage/b2.Body:getAngularDamping|b2.Body:getAngularDamping]] '' | + | [[Special:MyLanguage/b2.Body:getAngularDamping|b2.Body:getAngularDamping]] ''returns the angular damping of the body''<br/><!-- GIDEROSMTD:b2.Body:getAngularDamping() returns the angular damping of the body --> | 
| − | [[Special:MyLanguage/b2.Body:getAngularVelocity|b2.Body:getAngularVelocity]] '' | + | [[Special:MyLanguage/b2.Body:getAngularVelocity|b2.Body:getAngularVelocity]] ''returns the angular velocity''<br/><!-- GIDEROSMTD:b2.Body:getAngularVelocity() returns the angular velocity --> | 
| − | [[Special:MyLanguage/b2.Body:getGravityScale|b2.Body:getGravityScale]] '' | + | [[Special:MyLanguage/b2.Body:getGravityScale|b2.Body:getGravityScale]] ''returns the gravity scale of the body''<br/><!-- GIDEROSMTD:b2.Body:getGravityScale() returns the gravity scale of the body --> | 
| − | [[Special:MyLanguage/b2.Body:getInertia|b2.Body:getInertia]] '' | + | [[Special:MyLanguage/b2.Body:getInertia|b2.Body:getInertia]] ''returns the rotational inertia of the body about the local origin in kg-m^2''<br/><!-- GIDEROSMTD:b2.Body:getInertia() returns the rotational inertia of the body about the local origin in kg-m^2 --> | 
| − | [[Special:MyLanguage/b2.Body:getLinearDamping|b2.Body:getLinearDamping]] '' | + | [[Special:MyLanguage/b2.Body:getLinearDamping|b2.Body:getLinearDamping]] ''returns the linear damping of the body''<br/><!-- GIDEROSMTD:b2.Body:getLinearDamping() returns the linear damping of the body --> | 
| − | [[Special:MyLanguage/b2.Body:getLinearVelocity|b2.Body:getLinearVelocity]] '' | + | [[Special:MyLanguage/b2.Body:getLinearVelocity|b2.Body:getLinearVelocity]] ''returns the linear velocity of the center of mass''<br/><!-- GIDEROSMTD:b2.Body:getLinearVelocity() returns the linear velocity of the center of mass --> | 
| − | [[Special:MyLanguage/b2.Body:getLocalCenter|b2.Body:getLocalCenter]] '' | + | [[Special:MyLanguage/b2.Body:getLocalCenter|b2.Body:getLocalCenter]] ''returns the local position of the center of mass''<br/><!-- GIDEROSMTD:b2.Body:getLocalCenter() returns the local position of the center of mass --> | 
| [[Special:MyLanguage/b2.Body:getLocalPoint|b2.Body:getLocalPoint]] <br/><!-- GIDEROSMTD:b2.Body:getLocalPoint(x,y)  --> | [[Special:MyLanguage/b2.Body:getLocalPoint|b2.Body:getLocalPoint]] <br/><!-- GIDEROSMTD:b2.Body:getLocalPoint(x,y)  --> | ||
| [[Special:MyLanguage/b2.Body:getLocalVector|b2.Body:getLocalVector]] <br/><!-- GIDEROSMTD:b2.Body:getLocalVector(x,y)  --> | [[Special:MyLanguage/b2.Body:getLocalVector|b2.Body:getLocalVector]] <br/><!-- GIDEROSMTD:b2.Body:getLocalVector(x,y)  --> | ||
| − | [[Special:MyLanguage/b2.Body:getMass|b2.Body:getMass]] '' | + | [[Special:MyLanguage/b2.Body:getMass|b2.Body:getMass]] ''returns the total mass of the body in kilograms (kg)''<br/><!-- GIDEROSMTD:b2.Body:getMass() returns the total mass of the body in kilograms (kg) --> | 
| − | [[Special:MyLanguage/b2.Body:getPosition|b2.Body:getPosition]] '' | + | [[Special:MyLanguage/b2.Body:getPosition|b2.Body:getPosition]] ''returns the world body origin position''<br/><!-- GIDEROSMTD:b2.Body:getPosition() returns the world body origin position --> | 
| − | [[Special:MyLanguage/b2.Body:getWorldCenter|b2.Body:getWorldCenter]] '' | + | [[Special:MyLanguage/b2.Body:getWorldCenter|b2.Body:getWorldCenter]] ''returns the world position of the center of mass''<br/><!-- GIDEROSMTD:b2.Body:getWorldCenter() returns the world position of the center of mass --> | 
| [[Special:MyLanguage/b2.Body:getWorldPoint|b2.Body:getWorldPoint]] <br/><!-- GIDEROSMTD:b2.Body:getWorldPoint(x,y)  --> | [[Special:MyLanguage/b2.Body:getWorldPoint|b2.Body:getWorldPoint]] <br/><!-- GIDEROSMTD:b2.Body:getWorldPoint(x,y)  --> | ||
| [[Special:MyLanguage/b2.Body:getWorldVector|b2.Body:getWorldVector]] <br/><!-- GIDEROSMTD:b2.Body:getWorldVector(x,y)  --> | [[Special:MyLanguage/b2.Body:getWorldVector|b2.Body:getWorldVector]] <br/><!-- GIDEROSMTD:b2.Body:getWorldVector(x,y)  --> | ||
| − | [[Special:MyLanguage/b2.Body:isActive|b2.Body:isActive]] '' | + | [[Special:MyLanguage/b2.Body:isActive|b2.Body:isActive]] ''returns the active state of the body''<br/><!-- GIDEROSMTD:b2.Body:isActive() returns the active state of the body --> | 
| − | [[Special:MyLanguage/b2.Body:isAwake|b2.Body:isAwake]] '' | + | [[Special:MyLanguage/b2.Body:isAwake|b2.Body:isAwake]] ''returns the sleeping state of the body''<br/><!-- GIDEROSMTD:b2.Body:isAwake() returns the sleeping state of the body --> | 
| [[Special:MyLanguage/b2.Body:isBullet|b2.Body:isBullet]] <br/><!-- GIDEROSMTD:b2.Body:isBullet()  --> | [[Special:MyLanguage/b2.Body:isBullet|b2.Body:isBullet]] <br/><!-- GIDEROSMTD:b2.Body:isBullet()  --> | ||
| [[Special:MyLanguage/b2.Body:isFixedRotation|b2.Body:isFixedRotation]] <br/><!-- GIDEROSMTD:b2.Body:isFixedRotation()  --> | [[Special:MyLanguage/b2.Body:isFixedRotation|b2.Body:isFixedRotation]] <br/><!-- GIDEROSMTD:b2.Body:isFixedRotation()  --> | ||
| [[Special:MyLanguage/b2.Body:isSleepingAllowed|b2.Body:isSleepingAllowed]] <br/><!-- GIDEROSMTD:b2.Body:isSleepingAllowed()  --> | [[Special:MyLanguage/b2.Body:isSleepingAllowed|b2.Body:isSleepingAllowed]] <br/><!-- GIDEROSMTD:b2.Body:isSleepingAllowed()  --> | ||
| − | [[Special:MyLanguage/b2.Body:setActive|b2.Body:setActive]] '' | + | [[Special:MyLanguage/b2.Body:setActive|b2.Body:setActive]] ''sets the active state of the body''<br/><!-- GIDEROSMTD:b2.Body:setActive(flag) sets the active state of the body --> | 
| [[Special:MyLanguage/b2.Body:setAngle|b2.Body:setAngle]] <br/><!-- GIDEROSMTD:b2.Body:setAngle(angle)  --> | [[Special:MyLanguage/b2.Body:setAngle|b2.Body:setAngle]] <br/><!-- GIDEROSMTD:b2.Body:setAngle(angle)  --> | ||
| − | [[Special:MyLanguage/b2.Body:setAngularDamping|b2.Body:setAngularDamping]] '' | + | [[Special:MyLanguage/b2.Body:setAngularDamping|b2.Body:setAngularDamping]] ''sets the angular damping of the body''<br/><!-- GIDEROSMTD:b2.Body:setAngularDamping(angularDamping) sets the angular damping of the body --> | 
| − | [[Special:MyLanguage/b2.Body:setAngularVelocity|b2.Body:setAngularVelocity]] '' | + | [[Special:MyLanguage/b2.Body:setAngularVelocity|b2.Body:setAngularVelocity]] ''sets the angular velocity''<br/><!-- GIDEROSMTD:b2.Body:setAngularVelocity(omega) sets the angular velocity --> | 
| − | [[Special:MyLanguage/b2.Body:setAwake|b2.Body:setAwake]] '' | + | [[Special:MyLanguage/b2.Body:setAwake|b2.Body:setAwake]] ''sets the sleep state of the body''<br/><!-- GIDEROSMTD:b2.Body:setAwake(awake) sets the sleep state of the body --> | 
| [[Special:MyLanguage/b2.Body:setBullet|b2.Body:setBullet]] <br/><!-- GIDEROSMTD:b2.Body:setBullet(flag)  --> | [[Special:MyLanguage/b2.Body:setBullet|b2.Body:setBullet]] <br/><!-- GIDEROSMTD:b2.Body:setBullet(flag)  --> | ||
| [[Special:MyLanguage/b2.Body:setFixedRotation|b2.Body:setFixedRotation]] <br/><!-- GIDEROSMTD:b2.Body:setFixedRotation(flag)  --> | [[Special:MyLanguage/b2.Body:setFixedRotation|b2.Body:setFixedRotation]] <br/><!-- GIDEROSMTD:b2.Body:setFixedRotation(flag)  --> | ||
| − | [[Special:MyLanguage/b2.Body:setGravityScale|b2.Body:setGravityScale]] '' | + | [[Special:MyLanguage/b2.Body:setGravityScale|b2.Body:setGravityScale]] ''sets the gravity scale of the body''<br/><!-- GIDEROSMTD:b2.Body:setGravityScale(scale) sets the gravity scale of the body --> | 
| − | [[Special:MyLanguage/b2.Body:setLinearDamping|b2.Body:setLinearDamping]] '' | + | [[Special:MyLanguage/b2.Body:setLinearDamping|b2.Body:setLinearDamping]] ''sets the linear damping of the body''<br/><!-- GIDEROSMTD:b2.Body:setLinearDamping(linearDamping) sets the linear damping of the body --> | 
| − | [[Special:MyLanguage/b2.Body:setLinearVelocity|b2.Body:setLinearVelocity]] '' | + | [[Special:MyLanguage/b2.Body:setLinearVelocity|b2.Body:setLinearVelocity]] ''sets the linear velocity of the center of mass''<br/><!-- GIDEROSMTD:b2.Body:setLinearVelocity(x,y) sets the linear velocity of the center of mass --> | 
| − | [[Special:MyLanguage/b2.Body:setPosition|b2.Body:setPosition]] '' | + | [[Special:MyLanguage/b2.Body:setPosition|b2.Body:setPosition]] ''sets the world body origin position''<br/><!-- GIDEROSMTD:b2.Body:setPosition(x,y) sets the world body origin position --> | 
| [[Special:MyLanguage/b2.Body:setSleepingAllowed|b2.Body:setSleepingAllowed]] <br/><!-- GIDEROSMTD:b2.Body:setSleepingAllowed(flag)  --> | [[Special:MyLanguage/b2.Body:setSleepingAllowed|b2.Body:setSleepingAllowed]] <br/><!-- GIDEROSMTD:b2.Body:setSleepingAllowed(flag)  --> | ||
| + | |||
| | style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
| − | ===  | + | === Events === | 
| − | ===  | + | === Constants === | 
| |} | |} | ||
| − | + | ||
| + | ---- | ||
| + | *'''[[LiquidFun]]''' | ||
Revision as of 05:19, 17 February 2020
Supported platforms: 






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)
| Methodsb2.Body:applyAngularImpulse applies an angular impulse | EventsConstants | 
