as we know,using cocos2d-x in lua,we must register cocos2d-x’s class to lua,the job cocos2d-x did.But ,if we want to use box2d in lua ,wo should register box2d to lua ourself?
So sorry that we haven’t finish the binding of box2d library to lua. You can refer to this pull request https://github.com/cocos2d/cocos2d-x/pull/471, which exports SimpleAudioEngine to lua layer.
The community will deeply appreciate if you would like to contribute the box2d binding
Hi, I just made one.
@ local PTM_RATIO = 32.0
local gravity = b2Vec2(0.0, –10.0)
local *world = b2World:new_local
— Create edges around the entire screen
local groundBodyDef = b2BodyDef:new_local
groundBodyDef.position = b2Vec2
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
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
table.insert
— tick
local function tick
*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)
reference: http://www.cnblogs.com/zhongzf/archive/2012/06/10/2544002.html
Thanks zhong zhaofeng, the bindings you provided work perfectly in cocos2d-2.0-x-2.0.3! For those that need more help:
-
Include LuaBox2D.cpp and LuaBox2D.h in your project. LuaBox2D.h is missing an include, be sure to
#include "tolua++.h"
there. -
Register the lua box2D extension by editing your Appdelegate.cpp file.
- Add the include:
#include "LuaBox2D.h"
- Add the following to the bottom of the
AppDelegate::applicationDidFinishLaunching()
function:tolua_LuaBox2D_open(pEngine->getLuaState());
- Add the include:
That’s it! You should be able to use the sample lua code provided by zhong above. There is a couple of variables missing, but it’s easy to figure out.
I’m working an a ContactListener implementation that allows you to listen for contact events from lua, will hopefully post this in a few days.
As promised, a LuaContactListener implementation is attached.
LUA sample code:
local listener = CContactListener:new_local()
listener:registerScriptContactHandler(function (type, contact)
print("LUA: Contact")
if type == BEGIN_CONTACT then
print('begin')
else
print('end')
end
-- copy out contact data
local function printPos(fixture)
local pos = fixture:GetBody():GetPosition();
print(pos.x, pos.y)
end
printPos(contact:GetFixtureA())
printPos(contact:GetFixtureB())
end)
self.world:SetContactListener(listener)
Simon - wrote:
Thanks zhong zhaofeng, the bindings you provided work perfectly in cocos2d-2.0-x-2.0.3! For those that need more help:
>
* Include LuaBox2D.cpp and LuaBox2D.h in your project. LuaBox2D.h is missing an include, be sure to#include "tolua++.h"
there.
>
* Register the lua box2D extension by editing your Appdelegate.cpp file.
* Add the include:#include "LuaBox2D.h"
>* Add the following to the bottom of theAppDelegate::applicationDidFinishLaunching()
function:tolua_LuaBox2D_open(pEngine->getLuaState());
>
That’s it! You should be able to use the sample lua code provided by zhong above. There is a couple of variables missing, but it’s easy to figure out.
>
I’m working an a ContactListener implementation that allows you to listen for contact events from lua, will hopefully post this in a few days.
This not works for me. My cocos2dx version is 2.1.4,I think that’s the problem. Is there any way for me to generate the API for lua myself.