在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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) |
请发表评论