Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How can I create a physics object? — Gideros Forum

How can I create a physics object?

MaxMax Member
edited April 2014 in General questions
Hello,
I'm new at programming with gideros, but I really like it :)
I'm reading the book Gideros Mobile Game Development and I really like it, but I don't understand how to create a physics object. How can I do this?
I hope you could answer my question :)
Thanks in advance for answers

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited April 2014
    Hello @Max ;)
    Sure
    First you include box2d library:
    --include box2d library
    require "box2d"
    Let's create the world:
    local world = b2.World.new(0, 10, true)
    Then you define the Graphical object you want to attach body to:
    --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)
    --add to scene or stage
    stage:addChild(ball)
    Then you create a box2d body:
    --get radius
    local radius = ball:getWidth()/2
     
    --create box2d physical object
    local body = world:createBody{type = b2.DYNAMIC_BODY}
    body:setPosition(ball:getX(), ball:getY())
    body:setAngle(ball:getRotation() * math.pi/180)
    local circle = b2.CircleShape.new(0, 0, radius)
    local fixture = body:createFixture{shape = circle, density = 1.0, 
    friction = 0.1, restitution = 0.2}
     
    --add body reference to ball
     ball.body = body
    Move the world and make ball move with physics body
    on each frame
    stage:addEventListener(Event.ENTER_FRAME, function()
        -- edit the step values if required. These are good defaults!
        world:step(1/60, 8, 3)
        --update position to match box2d world object's position
        --get physical body reference
        local body = ball.body
        --get body coordinates
        local bodyX, bodyY = body:getPosition()
        --apply coordinates to sprite
        ball:setPosition(bodyX, bodyY)
        --apply rotation to sprite
        ball:setRotation(math.deg(body:getAngle()))
    end)
    Then you would need to define world boundaries, for example using chain shape:
    local body = world:createBody{type = b2.STATIC_BODY}
    body:setPosition(0, 0)
    local width = application:getContentWidth()
    local height = application:getContentHeight()
    --set surroungind box
    local chain = b2.ChainShape.new()
    chain:createLoop(
        0,0,
        0, height,
        width, height,
        width, 0
    )
    body:createFixture{shape = chain, density = 1.0, 
    friction = 1, restitution = 0.3}
    If something does not seem to work as expected, you can always use debug draw to draw where actually physics bodies are:
    local debugDraw = b2.DebugDraw.new()
    world:setDebugDraw(debugDraw)
    stage:addChild(debugDraw)
    That should be about it to get you started :)
  • MaxMax Member
    Thanks for your fast answer and sorry for my Late one. :)
    I typed in everything you wrote, but my image isn't moving :(
    I don't get errors.
    I hope you can help me again.
    Thanks in advance
  • john26john26 Maintainer
    Maybe try removing all "local" commands to begin with as this can be a source of confusion. Eg

    local x=5

    function foo()
    local x
    print (x) -- nil
    end

    Here the intent was to access the outer x set to 5 but because of the inner local x, this value is hidden. It is not an error to access x but its value is nil when printed.
  • ar2rsawseenar2rsawseen Maintainer
    Attached the example project with the code above (well little modification to restitution to make it jump infinitely)


    ;)
    zip
    zip
    Box2dTest.zip
    12K
  • MaxMax Member
    edited April 2014
    Thanks to both :)
    I'll try it later

    I have another question.
    I wanted to know if I can program that every 1 second the player sprite moves up a little bit (or something like this)?
  • MaxMax Member
    @ar2rsawseen
    It works now :)
    Thanks for your help!

    I have another question:
    How can I make, that my player object is rotating around itself while you touch it?
    If I do this with set Rotation, it rotates, but with every touch the sprite walks closer to the end of the screen and then outside of it.
    I hope you can help me again :)
  • ar2rsawseenar2rsawseen Maintainer
    you could try
    --on mouse down
    ball.body:setAngularVelocity(3) --radians/second
     
    --on mouse up
    ball.body:setAngularVelocity(0) --radians/second
  • MaxMax Member
    Thanks for your help, but I always get the error: Player.lua attempt to Index field 'body' (a Nil value) :(
    Does anyone know why?
  • ar2rsawseenar2rsawseen Maintainer
    Did you add the reference to the ball?
    --add body reference to ball
     ball.body = body
  • MaxMax Member
    I think, that I did.
    It worked after I used:
    body:setAngularVelocity(3)

    Thanks for your help :)
    Is there a way to let the sprite only rotate, if you "draw" circles on the touchscreen with your finger? I tried it, but I can't figure out, how I have to do it.
    Thanks in advance :)
  • ar2rsawseenar2rsawseen Maintainer
    You can use Gesture recognition lib:
    http://appcodingeasy.com/Gideros-Mobile/Detecting-Gestures-in-Gideros

    And when you detect circle, apply angular velocity ;)
  • MaxMax Member
    edited April 2014
    Thanks :) :)
    I'll try it later.

    My problem is the gideros player. It doesn't work. If I click play, I always get the message, that it doesn't react and asks me if I want to close it. It worked yesterday evening and it works fine with any other projects. :( :((
    I hope you can help me
  • ar2rsawseenar2rsawseen Maintainer
    I assume you mean the Desktop player
    Well it is possible you ran into some C++ mess associated with Box2d underneath.
    So something you added to the code breaks it, you can either try backing back until you see it works or you can post your code/project for us to check :)
  • MaxMax Member
    edited April 2014
    I found out what it was and now it works :)
    Thanks ar2rsawseen for your great help.
Sign In or Register to comment.