I'm trying to work my way through the examples and I've seen a couple of tutorials but they're way over my head. Is there a super-easy one for newbs?
For example - how do you add a box that falls down the screen? And then add a surface to stop it's movement?
I looked at the sleeping bodies example but it was a bit complex for me.
Thanks
Tom
Comments
http://giderosmobile.com/forum/discussion/1387/box2d-tutorial#Item_6
Is there a simple class to quickly set up physics and add bodies?
It's perhaps hard for beginners to grasp how physics works.
http://www.giderosmobile.com/forum/discussion/373/display-object-physics-helper-library/p1
But there is also an idea to create abstraction layer as talked about here:
http://www.giderosmobile.com/forum/discussion/1780/extending-gideros
I'll look into those.
I'm talking mega basic. Like I didn't even know you had to run an onEventEnterframe to update the box2D objects each frame. :-)
Getting the hang of it now.
One thing I really don't understand is:
-- edit the step values if required. These are good defaults!
self.world:step(1/60, 8, 3)
What do those 3 values do?
Thank you!
The step bit is in the second half. Hope that helps make some sense of them (I didn't know what they did until I read that just now!).
But to improve my skills, I'd like to get feedback. For example, in http://appcodingeasy.com/Gideros-Mobile/Gideros-Box2D-basics
There is a paragraph which describes that you need to move Gideros Sprites along with box2d objects and there is an example function, which is onEnterFrame event handler.
So, in your opinion, how could I improve it, so the next one that is trying to learn something, could understand it?
Something for me that would have helped would have been a small paragraph at the start.
Physics in Gideros consists of 4 steps:
1) Creating the physics world (setting gravity etc)
2) Creating physics bodies and attaching them to sprites
3) Using a function to update objects each frame
4) Running the world
As a newb, it would really have helped to have a tutorial before this one on just adding a sprite and adding a physics body to it. Perhaps how to add a circle and how to add a box.
I played about with your code this afternoon and was able to make myself a simple project with a falling box with a body. (code below).
This has really helped me out.
Thanks again and keep up the good work.
level1 = Core.class(Sprite)
function level1:init()
-- Setup box2D world
self.world = b2.World.new(0, 10, true)
-- Create image
local happyBox = Bitmap.new(Texture.new("happy-box.png"))
happyBox:setAnchorPoint(0.5,0.5)
happyBox:setPosition(50,50)
self:addChild(happyBox);
-- Create box2D body
local body = self.world:createBody{type = b2.DYNAMIC_BODY}
body:setPosition(happyBox:getX(), happyBox:getY());
body:setAngle(happyBox:getRotation() * math.pi/180);
local poly = b2.PolygonShape.new()
poly:setAsBox(happyBox:getWidth()/2, happyBox:getHeight()/2)
local fixture = body:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.2}
happyBox.body = body
happyBox.body.type = "happyBox";
--set up debug drawing
-- has to go at end
local debugDraw = b2.DebugDraw.new()
self.world:setDebugDraw(debugDraw)
self:addChild(debugDraw)
-- Function to update box2D world each frame
local function onEnterFrame()
-- edit the step values if required. These are good defaults!
self.world:step(1/60, 8, 3)
--iterate through all child sprites
for i = 1, self:getNumChildren() do
--get specific sprite
local sprite = self:getChildAt(i)
-- check if sprite HAS a body (ie, physical object reference we added)
if sprite.body then
--update position to match box2d world object's position
--get physical body reference
local body = sprite.body
--get body coordinates
local bodyX, bodyY = body:getPosition()
--apply coordinates to sprite
sprite:setPosition(bodyX, bodyY)
--apply rotation to sprite
sprite:setRotation(body:getAngle() * 180 / math.pi)
end
end
end
--run world
self:addEventListener(Event.ENTER_FRAME, onEnterFrame)
I'm myself rarely read whats written on website, and usually jump to code snippets and try to understand what there is, and that's probably reflects in my tutorials
You on the other hand are more of a reading type. I will take note of that and try to improve in further blog posts. Thank you so much for feedback
As a complete novice I was interested to try your code. I tried pasting it into main.lua, but when I run it I get:
main.lua:76: 'end' expected (to close 'function' at line 3) near ''
main.lua:76: 'end' expected (to close 'function' at line 3) near ''
main.lua:76: 'end' expected (to close 'function' at line 3) near ''
main.lua:76: 'end' expected (to close 'function' at line 3) near ''
I'm obviously doing something dumb. :-/
as you are complete novice it is recommended that you should start with something which is fully running
start from this tutorials for physics
http://appcodingeasy.com/Gideros-Mobile/Debug-drawing-for-physics-engine-in-Gideros-Mobile
http://appcodingeasy.com/Gideros-Mobile/Gideros-Box2D-basics
http://appcodingeasy.com/Gideros-Mobile/Gideros-Box2d-gravity-switch
also try to search tutorials from same site there are some others also
and you can also try to learn from example already shipped with Gideros installation
go with this
http://giderosmobile.com/forum/discussion/1387/box2d-tutoria#Item_7
Apologies for the time taken to get back to you...
If you've not found how to do it yet then here's some code that should work...
Let me know if you get stuck
http://www.amazon.com/Introduction-Game-Physics-Box2D-Parberry/dp/1466565764
Likes: vitalitymobile