New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Box2d Lua module #8712
Added Box2d Lua module #8712
Conversation
@@ -1305,6 +1305,7 @@ namespace dmEngine | |||
script_lib_context.m_HidContext = engine->m_HidContext; | |||
script_lib_context.m_GraphicsContext = engine->m_GraphicsContext; | |||
script_lib_context.m_JobThread = engine->m_JobThreadContext; | |||
script_lib_context.m_ConfigFile = engine->m_Config; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To allow for reading game properties, like physics.scale
void PushWorld(struct lua_State* L, class b2World* world) | ||
{ | ||
lua_pushlightuserdata(L, world); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: add script_box2d_world.cpp with proper user type.
dmGameObject::GetComponentUserDataFromLua(L, index, collection, COLLISION_OBJECT_EXT, (uintptr_t*)comp, 0, comp_world); | ||
} | ||
|
||
static int B2D_GetWorld(lua_State* L) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These parts should be moved to a world specific .cpp file
SETCONSTANT(b2BodyType, b2_staticBody); | ||
SETCONSTANT(b2BodyType, b2_kinematicBody); | ||
SETCONSTANT(b2BodyType, b2_dynamicBody); | ||
lua_setfield(L, -2, "b2BodyType"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They'll show up as box2d.b2bodyType.b2_staticBody
void* GetWorldContext2D(HWorld2D world); | ||
void* GetCollisionObjectContext2D(HCollisionObject2D collision_object); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to resolve the glue between our physics engine and box2d
//{"get_user_data", Body_GetUserData}, - could return the game object id ? | ||
//{"set_user_data", Body_SetUserData}, - could attach the body to a game object? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting the id of the game object is useful, and it should be possible to set the id as well, with some extra component functions to make sure that the game+physics objects life times are considered.
b2Body* body = dmGameSystem::CompCollisionObjectGetBox2DBody(component); | ||
|
||
if (body) | ||
PushBody(L, body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to make this more robust, as it currently contains an internal pointer, and the game object may be deleted directly after (and along with is the collision object).
E.g. if this has a game object id set, then we need to check it before doing any operations on the body.
{"__tostring", Body_tostring}, | ||
{"__eq", Body_eq}, | ||
|
||
{"get_position", Body_GetPosition}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous extension, uses the Box2D naming style: GetPosition()
.
Here I'm using the naming style in Defold.
Yay, or nay?
// {"get_mass_data", Body_GetMassData}, | ||
// {"set_mass_data", Body_SetMassData}, | ||
// {"reset_mass_data", Body_ResetMassData}, | ||
// {"synchronize_fixtures", SynchronizeFixtures}, | ||
// SynchronizeSingle(b2Shape* shape, int32 index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For mvp2
* @name b2body | ||
*/ | ||
|
||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied all documentation, but set some to "undocumented" /**
until they're implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it a lot!
A couple of small things should be fixed in docs.
- tests are still red.
* Set the position of the body's origin and rotation. | ||
* This breaks any contacts and wakes the other bodies. | ||
* Manipulating a body's transform may cause non-physical behavior. | ||
* @name body:set_transform |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
documentation still uses body:*
syntax
|
||
/*# Get the user data pointer that was provided in the body definition. | ||
* @name body:get_user_data | ||
* @return id [type: hash] the game object id this body is connected to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function isn't implemented, should be hidden from docs
/** Get the total force currently applied on this object | ||
* @name body:get_force | ||
* @note Defold Specific | ||
* @return force [type: vmath.vector3] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems like this function is already implemented but it's commented (as I understand /**
- comment out function, and /*#
- added function?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct.
// #define SETCONSTANT(NS, NAME) \ | ||
// lua_pushnumber(L, (lua_Number) NS :: NAME); \ | ||
// lua_setfield(L, -2, #NAME); | ||
|
||
|
||
// lua_newtable(L); | ||
// SETCONSTANT(b2Shape, e_circle); | ||
// SETCONSTANT(b2Shape, e_edge); | ||
// SETCONSTANT(b2Shape, e_polygon); | ||
// SETCONSTANT(b2Shape, e_chain); | ||
// lua_setfield(L, -2, "b2Shape"); | ||
|
||
// lua_newtable(L); | ||
// SETCONSTANT(b2JointType, e_unknownJoint) | ||
// SETCONSTANT(b2JointType, e_revoluteJoint) | ||
// SETCONSTANT(b2JointType, e_prismaticJoint) | ||
// SETCONSTANT(b2JointType, e_distanceJoint) | ||
// SETCONSTANT(b2JointType, e_pulleyJoint) | ||
// SETCONSTANT(b2JointType, e_mouseJoint) | ||
// SETCONSTANT(b2JointType, e_gearJoint) | ||
// SETCONSTANT(b2JointType, e_wheelJoint) | ||
// SETCONSTANT(b2JointType, e_weldJoint) | ||
// SETCONSTANT(b2JointType, e_frictionJoint) | ||
// SETCONSTANT(b2JointType, e_ropeJoint) | ||
// lua_setfield(L, -2, "b2JointType"); | ||
|
||
// #undef SETCONSTANT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this dead code, debug code, or a todo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kind of both. I'll remove it.
This allows you to get a physics body and manipulate its forces, velocities and other properties.
Example:
A full list of functions is available in the reference api for
b2d
andb2d.body
.Note that this was a first step. Next up is adding the missing structs, e.g. world, joints, fixtures and shapes support.
Fixes #8256
Fixes #8101
Fixes #5405
Fixes #3191
Fixes #3120
PR checklist
Example of a well written PR description:
### Technical changes
Technical changes:
Technical notes: