• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Box2D lua binding and Usage

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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)

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Nginx+lua控制实例发布时间:2022-07-22
下一篇:
Lua 程序设计 (Roberto,Ierusalimschy 著)发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap