Difference between revisions of "B2.World"
Line 5: | Line 5: | ||
'''<translate>Available since</translate>:''' Gideros 2011.6<br/> | '''<translate>Available since</translate>:''' Gideros 2011.6<br/> | ||
'''<translate>Inherits from</translate>:''' [[Special:MyLanguage/EventDispatcher|EventDispatcher]]<br/> | '''<translate>Inherits from</translate>:''' [[Special:MyLanguage/EventDispatcher|EventDispatcher]]<br/> | ||
+ | |||
=== <translate>Description</translate> === | === <translate>Description</translate> === | ||
− | + | The [[Special:MyLanguage/b2.World|b2.World]] class inherits from the following class: [[Special:MyLanguage/EventDispatcher|EventDispatcher]]. | |
− | + | ||
− | The [[Special:MyLanguage/b2.World|b2.World]] class manages all physics entities and dynamic simulation. It is possible to create and manage more than one [[Special:MyLanguage/b2.World|b2.World]] instance. | + | The [[Special:MyLanguage/b2.World|b2.World]] class manages all physics entities and dynamic simulation. It is possible to create and manage more than one [[Special:MyLanguage/b2.World|b2.World]] instance. |
− | + | ||
=== <translate>Examples</translate> === | === <translate>Examples</translate> === | ||
− | '''Creating Box2d body and moving Bitmap along the body''' | + | '''Creating Box2d body and moving Bitmap along the body''' |
− | <source lang="lua">require "box2d" | + | <source lang="lua"> |
+ | require "box2d" | ||
local world = b2.World.new(0, 10, true) | local world = b2.World.new(0, 10, true) | ||
Line 40: | Line 42: | ||
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) |
− | '''Detecting collisions between bodies''' | + | </source> |
− | <source lang="lua">--add collision event listener | + | |
+ | '''Detecting collisions between bodies''' | ||
+ | <source lang="lua"> | ||
+ | --add collision event listener | ||
world:addEventListener(Event.BEGIN_CONTACT, function(e) | world:addEventListener(Event.BEGIN_CONTACT, function(e) | ||
--getting contact bodies | --getting contact bodies | ||
Line 87: | Line 92: | ||
--additionally get collision force | --additionally get collision force | ||
print(event.maxImpulse) | print(event.maxImpulse) | ||
− | end)</source> | + | end) |
+ | </source> | ||
+ | |||
{|- | {|- | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
Line 104: | Line 111: | ||
[[Special:MyLanguage/b2.World:setGravity|b2.World:setGravity]] ''<translate>sets the gravity vector</translate>''<br/><!-- GIDEROSMTD:b2.World:setGravity(gravityx,gravityy) sets the gravity vector --> | [[Special:MyLanguage/b2.World:setGravity|b2.World:setGravity]] ''<translate>sets the gravity vector</translate>''<br/><!-- GIDEROSMTD:b2.World:setGravity(gravityx,gravityy) sets the gravity vector --> | ||
[[Special:MyLanguage/b2.World:step|b2.World:step]] ''<translate>takes a time step</translate>''<br/><!-- GIDEROSMTD:b2.World:step(timeStep,velocityIterations,positionIterations) takes a time step --> | [[Special:MyLanguage/b2.World:step|b2.World:step]] ''<translate>takes a time step</translate>''<br/><!-- GIDEROSMTD:b2.World:step(timeStep,velocityIterations,positionIterations) takes a time step --> | ||
+ | |||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
=== <translate>Events</translate> === | === <translate>Events</translate> === | ||
Line 113: | Line 121: | ||
=== <translate>Constants</translate> === | === <translate>Constants</translate> === | ||
|} | |} | ||
+ | |||
+ | ---- | ||
+ | *'''[[LiquidFun]]''' |
Revision as of 15:46, 18 February 2020
Supported platforms:
Available since: Gideros 2011.6
Inherits from: EventDispatcher
Description
The b2.World class inherits from the following class: EventDispatcher.
The b2.World class manages all physics entities and dynamic simulation. It is possible to create and manage more than one b2.World instance.
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)
Detecting collisions between bodies
--add collision event listener
world:addEventListener(Event.BEGIN_CONTACT, function(e)
--getting contact bodies
local fixtureA = e.fixtureA
local fixtureB = e.fixtureB
local bodyA = fixtureA:getBody()
local bodyB = fixtureB:getBody()
--do what you need with colliding bodies
end)
--add collision event listener
world:addEventListener(Event.END_CONTACT, function(e)
--getting contact bodies
local fixtureA = e.fixtureA
local fixtureB = e.fixtureB
local bodyA = fixtureA:getBody()
local bodyB = fixtureB:getBody()
--do what you need with colliding bodies
end)
--add collision event listener
world:addEventListener(Event.PRE_SOLVE, function(e)
--getting contact bodies
local fixtureA = e.fixtureA
local fixtureB = e.fixtureB
local bodyA = fixtureA:getBody()
local bodyB = fixtureB:getBody()
--do what you need with colliding bodies
end)
--add collision event listener
world:addEventListener(Event.POST_SOLVE, function(e)
--getting contact bodies
local fixtureA = e.fixtureA
local fixtureB = e.fixtureB
local bodyA = fixtureA:getBody()
local bodyB = fixtureB:getBody()
--do what you need with colliding bodies
--additionally get collision force
print(event.maxImpulse)
end)
Methodsb2.World.new |
EventsEvent.BEGIN_CONTACT Constants |