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
Merged
Merged
Added Box2d Lua module #8712
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b2cc818
Added first version of a box2d specific Lua module
JCash 823b2b3
Added object methods to the b2body representation
JCash 97f4090
Added some more functions
JCash 1201393
Added more functions
JCash 5087b2d
Moved functions into _body.cpp
JCash f332390
Added documentation
JCash 8636bf4
path fix for installation of box2d headers
JCash f44a54f
typo
JCash 031f986
Renamed scripting api after review discussion
JCash ae93009
Added more robust check if the body's game object is still alive
JCash 0cc3dff
Moved box2d script files into separate folder
JCash 7a63b31
Updated documentation to b2d.body. format
JCash cdd2034
review fixes
JCash 1e6dc8a
test fix
JCash c64d604
Fix forward declaration for b2World/b2Body to use correct (no) namespace
JCash File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
engine/gamesys/src/gamesys/scripts/box2d/script_box2d.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// Copyright 2020-2024 The Defold Foundation | ||
// Copyright 2014-2020 King | ||
// Copyright 2009-2014 Ragnar Svensson, Christian Murray | ||
// Licensed under the Defold License version 1.0 (the "License"); you may not use | ||
// this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License, together with FAQs at | ||
// https://www.defold.com/license | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed | ||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations under the License. | ||
|
||
#include <stdio.h> | ||
|
||
#include <Box2D/Dynamics/b2Body.h> | ||
#include <Box2D/Dynamics/Joints/b2Joint.h> | ||
|
||
#include <dlib/log.h> | ||
#include <gameobject/script.h> | ||
|
||
#include "gamesys.h" | ||
#include "gamesys_private.h" | ||
|
||
#include "components/comp_collision_object.h" | ||
|
||
#include "script_box2d.h" | ||
|
||
extern "C" | ||
{ | ||
#include <lua/lauxlib.h> | ||
#include <lua/lualib.h> | ||
} | ||
|
||
namespace dmGameSystem | ||
{ | ||
////////////////////////////////////////////////////////////////////////////// | ||
|
||
static float g_PhysicsScale = 1.0f; | ||
static float g_InvPhysicsScale = 1.0f / g_PhysicsScale; | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
// b2Vec2 | ||
|
||
b2Vec2 CheckVec2(lua_State* L, int index, float scale) | ||
{ | ||
dmVMath::Vector3* v = dmScript::CheckVector3(L, index); | ||
return b2Vec2(v->getX() * scale, v->getY() * scale); | ||
} | ||
|
||
dmVMath::Vector3 FromB2(const b2Vec2& p, float inv_scale) | ||
{ | ||
return dmVMath::Vector3(p.x * inv_scale, p.y * inv_scale, 0); | ||
} | ||
|
||
void PushWorld(struct lua_State* L, class b2World* world) | ||
{ | ||
lua_pushlightuserdata(L, world); | ||
} | ||
|
||
void SetPhysicsScale(float scale) | ||
{ | ||
g_PhysicsScale = scale; | ||
g_InvPhysicsScale = 1.0f / g_PhysicsScale; | ||
} | ||
|
||
float GetPhysicsScale() | ||
{ | ||
return g_PhysicsScale; | ||
} | ||
|
||
float GetInvPhysicsScale() | ||
{ | ||
return g_InvPhysicsScale; | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
|
||
static void GetCollisionObject(lua_State* L, int index, dmGameObject::HCollection collection, dmMessage::URL* url, void** comp, void** comp_world) | ||
{ | ||
dmGameObject::GetComponentUserDataFromLua(L, index, collection, COLLISION_OBJECT_EXT, (uintptr_t*)comp, url, comp_world); | ||
} | ||
|
||
static int B2D_GetWorld(lua_State* L) | ||
{ | ||
DM_LUA_STACK_CHECK(L, 1); | ||
|
||
dmGameObject::HCollection collection = dmGameObject::GetCollection(CheckGoInstance(L)); | ||
uint32_t component_type_index = dmGameObject::GetComponentTypeIndex(collection, COLLISION_OBJECT_EXT_HASH); | ||
void* comp_world = dmGameObject::GetWorld(collection, component_type_index); | ||
b2World* world = dmGameSystem::CompCollisionObjectGetBox2DWorld(comp_world); | ||
|
||
if (world) | ||
PushWorld(L, world); | ||
else | ||
lua_pushnil(L); | ||
return 1; | ||
} | ||
|
||
static int B2D_GetBody(lua_State* L) | ||
{ | ||
DM_LUA_STACK_CHECK(L, 1); | ||
|
||
dmGameObject::HCollection collection = dmGameObject::GetCollection(CheckGoInstance(L)); | ||
dmMessage::URL url; | ||
void* component = 0; | ||
GetCollisionObject(L, 1, collection, &url, &component, 0); | ||
|
||
b2Body* body = dmGameSystem::CompCollisionObjectGetBox2DBody(component); | ||
|
||
if (body) | ||
PushBody(L, body, collection, url.m_Path); | ||
else | ||
lua_pushnil(L); | ||
return 1; | ||
} | ||
|
||
static const luaL_reg BOX2D_FUNCTIONS[] = | ||
{ | ||
{"get_world", B2D_GetWorld}, | ||
{"get_body", B2D_GetBody}, | ||
|
||
{0, 0} | ||
}; | ||
|
||
void ScriptBox2DInitialize(const ScriptLibContext& context) | ||
{ | ||
float physics_scale_default = 1.0f; | ||
float physics_scale = context.m_ConfigFile ? dmConfigFile::GetFloat(context.m_ConfigFile, "physics.scale", physics_scale_default) : physics_scale_default; | ||
SetPhysicsScale(physics_scale); | ||
|
||
lua_State* L = context.m_LuaState; | ||
|
||
luaL_register(L, "b2d", BOX2D_FUNCTIONS); | ||
|
||
ScriptBox2DInitializeBody(L); | ||
|
||
lua_pop(L, 1); // pop the lua module | ||
} | ||
|
||
void ScriptBox2DFinalize(const ScriptLibContext& context) | ||
{ | ||
(void)context; | ||
} | ||
} | ||
|
||
|
||
/*# Box2D documentation | ||
* | ||
* Functions for interacting with Box2D. | ||
* | ||
* @document | ||
* @name b2d | ||
*/ | ||
|
||
/** Get the Box2D world from the current collection | ||
* @name b2d.get_world | ||
* @return world [type: b2World] the world if successful. Otherwise `nil`. | ||
*/ | ||
|
||
/*# Get the Box2D body from a collision object | ||
* @name b2d.get_body | ||
* @param url [type: url] the url to the game object collision component | ||
* @return body [type: b2Body] the body if successful. Otherwise `nil`. | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2020-2024 The Defold Foundation | ||
// Copyright 2014-2020 King | ||
// Copyright 2009-2014 Ragnar Svensson, Christian Murray | ||
// Licensed under the Defold License version 1.0 (the "License"); you may not use | ||
// this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License, together with FAQs at | ||
// https://www.defold.com/license | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed | ||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations under the License. | ||
|
||
#ifndef DM_GAMESYS_SCRIPT_BOX2D_H | ||
#define DM_GAMESYS_SCRIPT_BOX2D_H | ||
|
||
#include <dmsdk/dlib/hash.h> | ||
#include <dmsdk/dlib/vmath.h> | ||
#include <Box2D/Common/b2Math.h> | ||
|
||
namespace dmGameObject | ||
{ | ||
typedef struct CollectionHandle* HCollection; | ||
} | ||
|
||
namespace dmGameSystem | ||
{ | ||
void ScriptBox2DInitialize(const struct ScriptLibContext& context); | ||
void ScriptBox2DFinalize(const struct ScriptLibContext& context); | ||
|
||
// internal | ||
|
||
float GetPhysicsScale(); | ||
float GetInvPhysicsScale(); | ||
|
||
b2Vec2 CheckVec2(struct lua_State* L, int index, float scale); | ||
dmVMath::Vector3 FromB2(const b2Vec2& p, float inv_scale); | ||
|
||
void PushWorld(struct lua_State* L, class b2World* world); | ||
|
||
void PushBody(struct lua_State* L, class b2Body* body, dmGameObject::HCollection collection, dmhash_t gameobject_id); | ||
class b2Body* CheckBody(lua_State* L, int index); | ||
|
||
void ScriptBox2DInitializeBody(struct lua_State* L); | ||
} | ||
|
||
#endif // DM_GAMESYS_SCRIPT_BOX2D_H |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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