Difference between revisions of "B2.World"
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
(19 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | '''Supported platforms:''' android | + | <!-- GIDEROSOBJ:b2.World --> |
+ | '''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:''' [[EventDispatcher]]<br/> | ||
+ | |||
=== Description === | === Description === | ||
− | The | + | The '''b2.World''' class inherits from [[EventDispatcher]]. |
− | + | ||
− | The | + | The '''b2.World''' class manages all physics entities and dynamic simulation. It is possible to create and manage more than one '''b2.World''' instance. |
− | < | + | |
− | | style="width: 50%;"| | + | === Examples === |
+ | '''Creating Box2d body and moving Bitmap along the body''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | 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) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | '''Detecting collisions between bodies''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | --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) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | {|- | ||
+ | | style="width: 50%; vertical-align:top;"| | ||
=== Methods === | === Methods === | ||
− | [[b2.World.new]] | + | [[b2.World.new]] creates a new physics world<br/><!-- GIDEROSMTD:b2.World.new(gravityx,gravityy,doSleep) creates a new physics world--> |
− | [[b2.World:clearForces]] | + | [[b2.World:clearForces]] ''call this after you are done with time steps to clear the forces''<br/><!-- GIDEROSMTD:b2.World:clearForces() call this after you are done with time steps to clear the forces --> |
− | [[b2.World:createBody]] | + | [[b2.World:createBody]] ''creates a rigid body given a definition''<br/><!-- GIDEROSMTD:b2.World:createBody(bodyDef) creates a rigid body given a definition --> |
− | [[b2.World:createJoint]] | + | [[b2.World:createJoint]] ''creates a joint given a definition''<br/><!-- GIDEROSMTD:b2.World:createJoint(jointDef) creates a joint given a definition --> |
− | [[b2.World:createParticleSystem]] | + | [[b2.World:createParticleSystem]] ''create particle system''<br/><!-- GIDEROSMTD:b2.World:createParticleSystem(particleSysDef) create particle system --> |
− | [[b2.World:destroyBody]] | + | [[b2.World:destroyBody]] ''destroys a rigid body''<br/><!-- GIDEROSMTD:b2.World:destroyBody(body) destroys a rigid body --> |
− | [[b2.World:destroyJoint]] | + | [[b2.World:destroyJoint]] ''destroys a joint''<br/><!-- GIDEROSMTD:b2.World:destroyJoint(joint) destroys a joint --> |
− | [[b2.World:getGravity]] | + | [[b2.World:getGravity]] ''returns the gravity vector''<br/><!-- GIDEROSMTD:b2.World:getGravity() returns the gravity vector --> |
− | [[b2.World:queryAABB]] | + | [[b2.World:queryAABB]] ''query the world for all fixtures that potentially overlap the provided AABB''<br/><!-- GIDEROSMTD:b2.World:queryAABB(minx,miny,maxx,maxy) query the world for all fixtures that potentially overlap the provided AABB --> |
− | [[b2.World:rayCast]] | + | [[b2.World:rayCast]] ''raycast the world for all fixtures in the path of the ray''<br/><!-- GIDEROSMTD:b2.World:rayCast(x1,y1,x2,y2,listener,data) raycast the world for all fixtures in the path of the ray --> |
− | [[b2.World:setDebugDraw]] | + | [[b2.World:setDebugDraw]] ''registers a b2.DebugDraw instance for debug drawing''<br/><!-- GIDEROSMTD:b2.World:setDebugDraw() registers a b2.DebugDraw instance for debug drawing --> |
− | [[b2.World:setGravity]] | + | [[b2.World:setGravity]] ''sets the gravity vector''<br/><!-- GIDEROSMTD:b2.World:setGravity(gravityx,gravityy) sets the gravity vector --> |
− | [[b2.World:step]] | + | [[b2.World:step]] ''takes a time step''<br/><!-- GIDEROSMTD:b2.World:step(timeStep,velocityIterations,positionIterations) takes a time step --> |
− | | style="width: 50%;"| | + | |
+ | | style="width: 50%; vertical-align:top;"| | ||
=== Events === | === Events === | ||
− | [[Event.BEGIN_CONTACT]]<br/> | + | [[Event.BEGIN_CONTACT]]<br/><!-- GIDEROSEVT:Event.BEGIN_CONTACT beginContact--> |
− | [[Event.BEGIN_CONTACT_PARTICLE]]<br/> | + | [[Event.BEGIN_CONTACT_PARTICLE]]<br/><!-- GIDEROSEVT:Event.BEGIN_CONTACT_PARTICLE beginContactParticle--> |
− | [[Event.END_CONTACT]]<br/> | + | [[Event.END_CONTACT]]<br/><!-- GIDEROSEVT:Event.END_CONTACT endContact--> |
− | [[Event.POST_SOLVE]]<br/> | + | [[Event.POST_SOLVE]]<br/><!-- GIDEROSEVT:Event.POST_SOLVE postSolve--> |
− | [[Event.PRE_SOLVE]]<br/> | + | [[Event.PRE_SOLVE]]<br/><!-- GIDEROSEVT:Event.PRE_SOLVE preSolve--> |
=== Constants === | === Constants === | ||
|} | |} | ||
+ | |||
+ | ---- | ||
+ | *'''[[LiquidFun]]''' |
Latest revision as of 14:27, 13 July 2023
Supported platforms:
Available since: Gideros 2011.6
Inherits from: EventDispatcher
Description
The b2.World class inherits from 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 creates a new physics world |
EventsEvent.BEGIN_CONTACT Constants |