Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Tutorial 11 - Physics 101 and attaching a sprite to a physics object.

edited July 2012 in Examples Posts: 563

A quick tute on how to use the physics engine in your project. http://codeatuts.blogspot.com.au/

It is very easy to use once you understand the functions available.

As usual, comments welcome...

Tagged:

Comments

  • Posts: 266

    how do I make the object fall at set intervals from different x positions.

  • Posts: 563

    @veeeralp: as follows (from Minesweeper) in the Main class -

    draw()
    
        if gameState ~= stateMenu then
            -- reinitialise variables if not in the Menu screen
            menuTime = 0
            menuMineCtr = 0
        end
    
        if gameState == stateSplash then
            splashScreen:draw()
        elseif gameState == stateMenu then
            drawMenu()
        elseif ...
    
        end
    
    end
    
    drawMenu()
    
        menuTime = menuTime + DeltaTime
    
        if menuTime > 1 then
            -- create a mine physics object at a random spot at the top of the screen
            createCircle(math.random(50, WIDTH - 50, HEIGHT, 32)
            -- keep track of how many mines we have created. In retrospect I could have
            -- just counted the items in the PhysicsDebugDraw bodies table.
            menuMineCtr = MenuMineCtr + 1
            menuTime = 0
        end
    
        if menuMineCtr > 50 then
    
            -- reset the physics body table and re-initialise
            -- I originally put this in when the ground was there.
            -- I'm not sure how many objects you can have on the
            -- screen at once without slowing down appreciably or
            -- eventually crashing due to low memory. 50 seemed safe.
    
            menuMineCtr = 0
            physicsDraw:clear()
    
        end
    
        physicsDraw:draw()
    
        -- do the rest of the menu drawing...
    
    end
    
  • Posts: 266

    @Reefwing I'm getting really low fps when I add the physics objects. Any ideas? I'm using a modified version if your menu state with six buttons.

  • Posts: 266

    @Reefwing. Nevermind got it. When I make the physics objects fall down, they are colliding with a physics object in another state. Any help? It's being drawn but is invisible. I need to be in the run state only but it only needs to be called once. Not once every frame. So what I'm asking is is there a way to call do an initial setup for a gameState. Right now I'm calling it the regular function setup

  • Posts: 563

    @veeeralp - sorry I have been travelling. Good to hear you worked it out.

Sign In or Register to comment.