I've decided to create a class called SimplePhysics to take some of the work out of adding bodies to sprites.
Here's what I'd like to be able to do:
1) Add a sprite to a scene (no problems there)
2) Simply add:
SimplePhysics:addSquareBody(someSprite,density,friction,restitution,type);
3) My class would look a bit like:
physics.lua
SimplePhysics = Core.class(Sprite);
-- Create box2D body
function SimplePhysics:addSquareBody(imageObject)
local body = self.world:createBody{type = b2.DYNAMIC_BODY}
body:setPosition(imageObject:getX(), imageObject:getY());
body:setAngle(imageObject:getRotation() * math.pi/180);
local poly = b2.PolygonShape.new()
poly:setAsBox(imageObject:getWidth()/2, imageObject:getHeight()/2)
local fixture = body:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.2}
imageObject.body = body
imageObject.body.type = "someType";
end
... with methods for the other body types.
Does this sound possible?
Thanks
Tom
Comments
Actually on the context of extending the Gideros, I'm working myself on similar solution, that would add method like createRectangle or createCircle to b2.World class. But would love to hear more about what methods to include
Maybe be we could share our findings later
Currently, there is no documentation. Sorry :P
You can check my version on https://github.com/ar2rsawseen/GiderosInit in the end of init.lua file
More info on this thread: http://www.giderosmobile.com/forum/discussion/1780/extending-gideros
Very cool. Going to check out your solutions now.
Here's what I made (and I'm a serious newbie hehe) simplephysics.lua
Heres what it does:
Create a square body:
physics = {}
physics.imageObject = happyBox;
SimplePhysics:addSquareBody(physics);
Create a circle body
physics = {}
physics.imageObject = circleImage;
SimplePhysics:addCircleBody(physics);
Some options
physics.density = 1.0;
physics.friction = 0.1;
physics.restitution = 0.2;
physics.padding = 5; -- padding around the collision shape
Turn on debug mode:
--set up debug drawing
SimplePhysics:debugDraw(self);
It seems to work but if someone could let me know what to take out or if what I could improve, please let me know. :-)
SimplePhysics = Core.class(Sprite);
-- Create a square body
function SimplePhysics:addSquareBody()
local scene = physics.imageObject:getParent();
local body = scene.world:createBody{type = b2.DYNAMIC_BODY}
body:setPosition(physics.imageObject:getX(), physics.imageObject:getY());
body:setAngle(physics.imageObject:getRotation() * math.pi/180);
-- Set default values for if they are blank
if(physics.density == nil) then physics.padding = 1; end
if(physics.friction == nil) then physics.padding = 0.1; end
if(physics.restitution == nil) then physics.restitution = 0.2; end
if(physics.padding == nil) then physics.padding = 0; end
square = b2.PolygonShape.new();
square:setAsBox((physics.imageObject:getWidth()/2) - physics.padding, (physics.imageObject:getHeight()/2)-physics.padding)
local fixture = body:createFixture{shape = square, density = physics.density, friction = physics.friction, restitution = physics.restitution}
physics.imageObject.body = body;
physics.imageObject.body.type = "someType";
end
-- Create a circle body
function SimplePhysics:addCircleBody()
-- Set default values for if they are blank
if(physics.density == nil) then physics.padding = 1; end
if(physics.friction == nil) then physics.padding = 0.1; end
if(physics.restitution == nil) then physics.restitution = 0.2; end
if(physics.padding == nil) then physics.padding = 0; end
local scene = physics.imageObject:getParent();
local body = scene.world:createBody{type = b2.DYNAMIC_BODY}
body:setPosition(physics.imageObject:getX(), physics.imageObject:getY());
body:setAngle(physics.imageObject:getRotation() * math.pi/180);
local radius = ((physics.imageObject:getWidth()/2)-physics.padding);
local circle = b2.CircleShape.new(0, 0, radius);
local fixture = body:createFixture{shape = circle, density = physics.density, friction = physics.friction, restitution = physics.restitution}
physics.imageObject.body = body
physics.imageObject.body.type = "someType";
end
-- Turn on debug draw
function SimplePhysics:debugDraw(scene)
-- has to go at end
local debugDraw = b2.DebugDraw.new()
scene.world:setDebugDraw(debugDraw)
scene:addChild(debugDraw);
end