Howdy, Stranger!

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

Codea Physics Sneak Preview

SimeonSimeon Admin Mod
edited November 2011 in Code Sharing Posts: 5,054

Hi everyone

Here's a little preview of what's coming. (Should Apple ever approve the 1.1.2 update :) )

Link:

John has been creating a Box2D physics library for Codea. He's a physics coding expert, and his main goal has been:

  • Make it easy to use

I think he's achieved that. Physics will be located in a physics global table when it's added to Codea. The main features will be circle, rectangle and polygonal bodies (using automatic convex decomposition), easy physics debug drawing, and full joint support.

He spent a day recreating our current favourite flash physics game PoleRiders - You can see this in the video. The original game is here: http://www.foddy.net/PoleRiders.html

His demo uses Nat's video game controller library (https://github.com/npryce/codea-controllers). The Minter control is used to combine two analog sticks. Side note: John has actually written a two-player split screen version using a new GL scissor test API he added to Codea for just this purpose, and four analog sticks combined with the Minter controller.

I'll get John to post more about the API and sample code so you can see what it will be like to code.

Comments

  • BortelsBortels Mod
    Posts: 1,557

    1) W00t!
    2) I love the little pole vault guy.
    3) I'd love to see source and/or API docs so I can start salivating now (and thinking about the API, and how to fit it in with my current shenanigans). I'm assuming the API is slightly-modified Box2D.

  • NatNat
    Posts: 143

    Wonderful!

  • Posts: 437

    Oh, I saw a similar flash game the other day, cant find the url right now, but i love the one in the video,...suggestion: you can change the object jumped for cars or something else :)
    First game to try with physics...angry birds? XD

  • JohnJohn Admin Mod
    edited November 2011 Posts: 587

    Hi guys,

    I'm going to start writing API documentation soon. The physics mirrors Box2D to a certain extent, and exposes most of its features.

    Here's some example code:

        -- all units are in pixels, 32 pixels = 1 meter in physics world
        -- physics.setPixelToMeterRatio(ratio) will be exposed if you want a different conversion
    
        -- create a circle shaped rigidbody
        circle = physics.rigidbody(CIRCLE, radius)
        circle.x = 10
        circle.y = 20
        circle.position = vec2(10,20)
        circle.radius = 30
        -- can change density at any time
        circle.density = 2 
       
        -- create a polygon, p1, ... p4 are the points that make up the shape
        -- codea will automatically decompose non-convex shapes for you
        poly = physics.rigidbody(POLYGON, p1, p2, p3, p4)
        -- can turn off rotation at any time for things like chracters
        poly.fixedRotation = true
    
        -- you can change the collision filter settings to control collisions
        -- filter indices need to be between 0-15
        PLAYER = 1
        ENEMY = 2
        COIN = 3
    
        -- this means the player rigidbody would only collide with enemies and coins
        player:setFilterCategories(PLAYER)
        player:setFilterMask(ENEMY, COIN)
    
        -- this means the enemy rigidbody would only collide with players, and not coins
        enemy:setFilterCategories(ENEMY)
        enemy:setFilterMask(PLAYER)
        
        -- coins only collide with the player
        coin:setFilterCategories(COIN)
        coin:setFilterMask(PLAYER)
    
        -- joints are easy to create
        -- we plan to support all current box2d joints
        -- REVOLUTE, DISTANCE, PRISMATIC, WELD, PULLEY, GEAR, ROPE, FRICTION, WHEEL
        joint = physics.joint(REVOLUTE, body1, body2, anchor)
        -- you can use limits
        joint.enableLimits = true
        joint.lowerLimit = -45
        joint.upperLimit = 45
        -- you can enable motors too
        joint.enableMotor = true
        joint.maxMotorTorque = 100
        joint.motorSpeed = 100
    
        -- contact processing is similar to touch handling
        function collide(contact)
            if contact.state == BEGAN and contact.touching then
                if contact.bodyA == body or contact.bodyB == body then
                    body:destroy()
                    body = nil
                end
            end
        end
    
        -- contacts give you access to other information such as:
        -- position == point of contact (average if its a polygon manifold) 
        -- normal == normal of contact
        -- normalImpulse == solver impulse in direction of impact
        -- tangentImpulse == solver impulse in tangent to impact 
        -- touching == whether contact is currently touching or not (side effect of box2d)
    
    

    This is basically what we have working right now, with more to come, such as raycasting, and compound shapes.

  • BortelsBortels Mod
    Posts: 1,557

    Can the collision stuff be used independently of physics? I guess I mean if I set x and y and make bodies massless and no gravity, etc - will the collisions still work, or are they emergent from the equations of motion used by the physics engine? Just wondering - I have a thing I was working on where I wanted sprite collision, and "real" physics collision would probably work fine...

  • JohnJohn Admin Mod
    edited November 2011 Posts: 587

    @Bortels You can make bodies massless but you would need to make them static or kinematic, which will prevent them from colliding with each other. You can use sensors (which i plan to add), but they wont provide you with contact info. I'll need to check if sensors will collide with each other when set to static.

    Edit:

    Just figured out a way. What you can do is make all your bodies dynamic sensors, then turn off gravity for them (there's a property called gravityScale that you can set to zero to selectively alter gravity). Then all you need to do is move them around manually and you'll get reports on collisions. Still no contact data though.

  • BortelsBortels Mod
    Posts: 1,557

    I'm only looking for "this touched that" - no detail on where or how necessary. Just co-opting physics for video games :-)

    I'd just do circles and distances, but they're not all circular, and as a player I hate it when you go "wait, they didn't even touch!".

  • beebee
    Posts: 381

    It's all great except that we can't use it! Arrggghhh... c'mon Apple! :(

  • SimeonSimeon Admin Mod
    Posts: 5,054

    @Bortels - sensors should cover that use case easily.

    @bee I know! It's quite frustrating for us too. Still "In Review," and all of my queries by email get the same canned response.

  • Where does this awesome feature sit priority-wise on Codea's roadmap? I'd love to use some robust physics for my current prototype, and I'm trying to decide between waiting for this Box2D implementation or trying to roll my own.

  • SimeonSimeon Admin Mod
    Posts: 5,054

    Probably version 1.3 - it's in there at the moment. John has written most of the documentation and examples, but it needs a bit more testing.

Sign In or Register to comment.