Box2D lua binding and Usage

來源:互聯網
上載者:User

Box2D lua binding pkg and c++ files: LuaBox2D.zip

Usage:

    local PTM_RATIO = 32.0    local gravity = b2Vec2(0.0, -10.0)    local _world = b2World:new_local(gravity)        -- Create edges around the entire screen    local groundBodyDef = b2BodyDef:new_local()    groundBodyDef.position = b2Vec2(0, 0)    local groundBody = _world:CreateBody(groundBodyDef)    local groundEdge = b2EdgeShape:new_local()    local boxShapeDef = b2FixtureDef:new_local()    boxShapeDef.shape = groundEdge    groundEdge:Set(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0))    groundBody:CreateFixture(boxShapeDef)    groundEdge:Set(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO))    groundBody:CreateFixture(boxShapeDef)    groundEdge:Set(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO))    groundBody:CreateFixture(boxShapeDef)    groundEdge:Set(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0))    groundBody:CreateFixture(boxShapeDef)

Create body and shape

        -- Create body and shape        local bodyDef = b2BodyDef:new_local()        bodyDef.type = b2_dynamicBody        bodyDef.position:Set(x/PTM_RATIO, y/PTM_RATIO)        bodyDef.userData = bodySprite        local _body = _world:CreateBody(bodyDef)         local circle = b2CircleShape:new_local()        circle.m_radius = 26.0/PTM_RATIO         shapeDef = b2FixtureDef:new_local()        shapeDef.shape = circle        shapeDef.density = 1.0        shapeDef.friction = 0.2        shapeDef.restitution = 0.8        _body:CreateFixture(shapeDef)        table.insert(body_list, _body)

tick

    -- tick    local function tick(dt)        _world:Step(dt, 10, 10)        if body_list ~= nil then            for i, body in ipairs(body_list) do                    local sprite = sprite_list[i]                    sprite:setPosition(b2Vec2(body:GetPosition().x * PTM_RATIO, body:GetPosition().y * PTM_RATIO))                    sprite:setRotation(-1 * CC_RADIANS_TO_DEGREES(body:GetAngle()))            end        end    end    CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(tick, 0, false)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.