Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Is there a very easy physics tutorial? — Gideros Forum

Is there a very easy physics tutorial?

Tom2012Tom2012 Guru
edited September 2012 in General questions
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

  • Thanks, yep saw your tutorials but for me they're still a bit complex.

    Is there a simple class to quickly set up physics and add bodies?

    It's perhaps hard for beginners to grasp how physics works.
  • Hi thanks for the help.

    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!
  • There's a reasonably understandable explanation of the step parameters here http://www.iforce2d.net/b2dtut/worlds
    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!).
  • Thanks Pete - much appreciated!!
  • Well it's really hard to describe if you understand how it all works, on the other hand its hard to describe if you are learning and don't know much. Thus it's hard to create great tutorial. :)

    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? ;)
  • Hey I love your tutorials - I'm just a bit slower than most :-)

    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)
  • ar2rsawseenar2rsawseen Maintainer
    edited September 2012
    Hmm, I guess I got it. Maybe I put too much sense on code, where I could emphasize things in text more. For example, this same Box2d basic tutorial includes how to create physics object both from Shape and Bitmap, using both circle and rectangle. But I guess it's not mentioned in text.

    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 ;)
  • BJGBJG Member
    Hi Tom2012 -

    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. :-/
  • @BJG

    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

    :)
  • Hi Tom2012 -

    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. :-/
    Hello

    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 :D
    --include box2d library
    require "box2d"
     
    -- Setup box2D world
    stage.world = b2.World.new(0, 10, true)
     
    -- Create image
     
    local testImage = Bitmap.new(Texture.new("block.png"))
    testImage:setAnchorPoint(0.5,0.5)
    testImage:setPosition(50,50)
    stage:addChild(testImage);
     
    -- Set up enterframe loop
     
    local function onEnterFrame()
     
    	-- edit the step values if required. These are good defaults!
    	stage.world:step(1/60, 8, 3)
     
    	--iterate through all child sprites
    	for i = 1, stage:getNumChildren() do
     
    	--get specific sprite
    	local sprite = stage: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
     
     
    -- Create box2D body
     
    local body = stage.world:createBody{type = b2.DYNAMIC_BODY}
     
    body:setPosition(testImage:getX(), testImage:getY());
     
    body:setAngle(testImage:getRotation() * math.pi/180);
     
    local poly = b2.PolygonShape.new()
     
    poly:setAsBox(testImage:getWidth()/2, testImage:getHeight()/2)
     
    local fixture = body:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.2}
     
    testImage.body = body
     
    --set up debug drawing
    -- has to go at end
     
    local debugDraw = b2.DebugDraw.new()
    stage.world:setDebugDraw(debugDraw)
    stage:addChild(debugDraw)
     
    -- Function to update box2D world each frame
     
    --run world
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
  • BJGBJG Member
    (Thanks guys...)
  • JaviJavi Member
    edited January 2013
    Next month will be published "Introduction to Game Physics with Box2D" by Ian Parberry. I have most of the books of this author and they are all really great, I'm sure it'll be a good reference text if you need it.

    http://www.amazon.com/Introduction-Game-Physics-Box2D-Parberry/dp/1466565764
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.