Skip to content

Commit

Permalink
Issue 3191 expose isbullet (#5905)
Browse files Browse the repository at this point in the history
* Added 'bullet' property to go and underlying glue code with box2d - ref #3191

* Issue 3191: getter/setter test for collision-object bullet property - ref #3191

* Issue 3191: proper support for 'bullet' property in the engine

that is, besided interfacing with the box2d engine

* Issue 3191: Added 'bullet' property to editor - ref #3191

* Issue 3191: Added bullet tests for null physics implementation - ref #3191

Also removed obsolete comment

* Issue 3191: Small fix for IsBullet() test - ref #3191
  • Loading branch information
otsakir committed Jul 19, 2021
1 parent ef7a165 commit b3ac504
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 13 deletions.
8 changes: 6 additions & 2 deletions editor/src/clj/editor/collision_object.clj
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@
:mask (some->> (:mask co) (string/join ", "))
:linear-damping (:linear-damping co)
:angular-damping (:angular-damping co)
:locked-rotation (:locked-rotation co))
:locked-rotation (:locked-rotation co)
:bullet (:bullet co))
(g/connect self :collision-group-node project :collision-group-nodes)
(g/connect project :collision-groups-data self :collision-groups-data)
(g/connect project :settings self :project-settings)
Expand Down Expand Up @@ -407,7 +408,7 @@

(g/defnk produce-pb-msg
[collision-shape-resource type mass friction restitution
group mask angular-damping linear-damping locked-rotation
group mask angular-damping linear-damping locked-rotation bullet
shapes]
{:collision-shape (resource/resource->proj-path collision-shape-resource)
:type type
Expand All @@ -419,6 +420,7 @@
:linear-damping linear-damping
:angular-damping angular-damping
:locked-rotation locked-rotation
:bullet bullet
:embedded-collision-shape (produce-embedded-collision-shape shapes)})

(defn build-collision-object
Expand Down Expand Up @@ -524,6 +526,8 @@
(default 0))
(property locked-rotation g/Bool
(default false))
(property bullet g/Bool
(default false))

(property group g/Str)
(property mask g/Str)
Expand Down
1 change: 1 addition & 0 deletions engine/gamesys/proto/gamesys/physics_ddf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ message CollisionObjectDesc
optional float linear_damping = 9 [default=0];
optional float angular_damping = 10 [default=0];
optional bool locked_rotation = 11 [default=false];
optional bool bullet = 12 [default=false];
}

/*# Collision object physics API documentation
Expand Down
22 changes: 22 additions & 0 deletions engine/gamesys/src/gamesys/components/comp_collision_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace dmGameSystem
static const dmhash_t PROP_LINEAR_VELOCITY = dmHashString64("linear_velocity");
static const dmhash_t PROP_ANGULAR_VELOCITY = dmHashString64("angular_velocity");
static const dmhash_t PROP_MASS = dmHashString64("mass");
static const dmhash_t PROP_BULLET = dmHashString64("bullet");


struct CollisionComponent;
struct JointEndPoint;
Expand Down Expand Up @@ -321,6 +323,8 @@ namespace dmGameSystem
out_data.m_LinearDamping = ddf->m_LinearDamping;
out_data.m_AngularDamping = ddf->m_AngularDamping;
out_data.m_LockedRotation = ddf->m_LockedRotation;
out_data.m_LockedRotation = ddf->m_LockedRotation;
out_data.m_Bullet = ddf->m_Bullet;
out_data.m_Enabled = enabled;
for (uint32_t i = 0; i < 16 && resource->m_Mask[i] != 0; ++i)
{
Expand Down Expand Up @@ -1160,6 +1164,14 @@ namespace dmGameSystem
out_value.m_Variant = dmGameObject::PropertyVar(dmPhysics::GetMass2D(component->m_Object2D));
}
return dmGameObject::PROPERTY_RESULT_OK;
} else if (params.m_PropertyId == PROP_BULLET) {
if (physics_context->m_3D) {
dmLogWarning("'bullet' property not supported in 3d physics mode");
return dmGameObject::PROPERTY_RESULT_NOT_FOUND;
} else {
out_value.m_Variant = dmGameObject::PropertyVar(dmPhysics::IsBullet2D(component->m_Object2D));
}
return dmGameObject::PROPERTY_RESULT_OK;
} else if (params.m_PropertyId == PROP_LINEAR_DAMPING) {
if (physics_context->m_3D) {
out_value.m_Variant = dmGameObject::PropertyVar(dmPhysics::GetLinearDamping3D(component->m_Object3D));
Expand Down Expand Up @@ -1201,6 +1213,16 @@ namespace dmGameSystem
dmPhysics::SetAngularVelocity2D(physics_context->m_Context2D, component->m_Object2D, Vectormath::Aos::Vector3(params.m_Value.m_V4[0], params.m_Value.m_V4[1], params.m_Value.m_V4[2]));
}
return dmGameObject::PROPERTY_RESULT_OK;
} else if (params.m_PropertyId == PROP_BULLET) {
if (params.m_Value.m_Type != dmGameObject::PROPERTY_TYPE_BOOLEAN)
return dmGameObject::PROPERTY_RESULT_TYPE_MISMATCH;
if (physics_context->m_3D) {
dmLogWarning("'bullet' property not supported in 3d physics mode");
return dmGameObject::PROPERTY_RESULT_NOT_FOUND;
} else {
dmPhysics::SetBullet2D(component->m_Object2D, params.m_Value.m_Bool);
}
return dmGameObject::PROPERTY_RESULT_OK;
} else if (params.m_PropertyId == PROP_LINEAR_DAMPING) {
if (params.m_Value.m_Type != dmGameObject::PROPERTY_TYPE_NUMBER)
return dmGameObject::PROPERTY_RESULT_TYPE_MISMATCH;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
collision_shape: ""
type: COLLISION_OBJECT_TYPE_DYNAMIC
mass: 1.0
friction: 0.1
restitution: 0.5
group: "default"
mask: "default"
embedded_collision_shape {
shapes {
shape_type: TYPE_BOX
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
index: 0
count: 3
}
data: 10.0
data: 10.0
data: 10.0
}
linear_damping: 0.0
angular_damping: 0.0
locked_rotation: false
bullet: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
components {
id: "properties-script"
component: "/collision_object/properties.script"
}
components {
id: "properties-co"
component: "/collision_object/properties.collisionobject"
}
20 changes: 20 additions & 0 deletions engine/gamesys/src/gamesys/test/collision_object/properties.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- This is the test script for collision object properties. Starting with 'bullet' property. More to come...

function test_bullet_property()
-- test getters/setters of bullet property
assert(go.get("go#properties-co", "bullet") == false)
go.set("go#properties-co", "bullet", true)
assert(go.get("go#properties-co", "bullet"))
end

function init(self)
physics.set_gravity(vmath.vector3(0, -10, 0))

test_bullet_property()
end

tests_done = false -- flag end of test to C level

function update(self, dt)
tests_done = true
end
38 changes: 37 additions & 1 deletion engine/gamesys/src/gamesys/test/test_gamesys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ TEST_F(GamepadConnectedTest, TestGamepadConnectedInputEvent)
dmGameSystem::FinalizeScriptLibs(scriptlibcontext);
}

TEST_F(Sleeping2DCollisionObjectTest, WakingCollisionObjectTest)
TEST_F(CollisionObject2DTest, WakingCollisionObjectTest)
{
dmHashEnableReverseHash(true);
lua_State* L = dmScript::GetLuaState(m_ScriptContext);
Expand Down Expand Up @@ -1486,6 +1486,42 @@ TEST_F(Sleeping2DCollisionObjectTest, WakingCollisionObjectTest)
ASSERT_TRUE(dmGameObject::Final(m_Collection));
}

// Test case for collision-object properties
TEST_F(CollisionObject2DTest, PropertiesTest)
{
dmHashEnableReverseHash(true);
lua_State* L = dmScript::GetLuaState(m_ScriptContext);

dmGameSystem::ScriptLibContext scriptlibcontext;
scriptlibcontext.m_Factory = m_Factory;
scriptlibcontext.m_Register = m_Register;
scriptlibcontext.m_LuaState = L;
dmGameSystem::InitializeScriptLibs(scriptlibcontext);

// a 'base' gameobject works as the base for other dynamic objects to stand on
const char* path_go = "/collision_object/properties.goc";
dmhash_t hash_go = dmHashString64("/go");
// place the base object so that the upper level of base is at Y = 0
dmGameObject::HInstance properties_go = Spawn(m_Factory, m_Collection, path_go, hash_go, 0, 0, Point3(0, 0, 0), Quat(0, 0, 0, 1), Vector3(1, 1, 1));
ASSERT_NE((void*)0, properties_go);

// iterate until the lua env signals the end of the test of error occurs
bool tests_done = false;
while (!tests_done)
{
ASSERT_TRUE(dmGameObject::Update(m_Collection, &m_UpdateContext));
ASSERT_TRUE(dmGameObject::PostUpdate(m_Collection));

// check if tests are done
lua_getglobal(L, "tests_done");
tests_done = lua_toboolean(L, -1);
lua_pop(L, 1);
}

ASSERT_TRUE(dmGameObject::Final(m_Collection));
}


/* Physics joints */
TEST_F(ComponentTest, JointTest)
{
Expand Down
6 changes: 3 additions & 3 deletions engine/gamesys/src/gamesys/test/test_gamesys.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ class GamesysTest : public CustomizableGamesysTest<T>
dmHashTable64<void*> m_Contexts;
};

class Sleeping2DCollisionObjectTest : public GamesysTest<const char*>
// sets up test context for various 2D physics/collision-object tests
class CollisionObject2DTest : public GamesysTest<const char*>
{
public:
Sleeping2DCollisionObjectTest() {
CollisionObject2DTest() {
// override configuration values specified in GamesysTest()
m_projectOptions.m_MaxCollisionCount = 32;
m_projectOptions.m_MaxContactPointCount = 64;
m_projectOptions.m_3D = false;
}

};

class ResourceTest : public GamesysTest<const char*>
Expand Down
30 changes: 25 additions & 5 deletions engine/physics/src/physics/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ namespace dmPhysics
uint16_t m_Mask;
/// Locked rotation keeps the angular velocity at 0
uint16_t m_LockedRotation : 1;
/// Bullet determings if this behaves like a bullet (fast moving object) or not
uint16_t m_Bullet : 1;
/// Whether the object is enabled from the start or not, default is 1
uint16_t m_Enabled : 1;
uint16_t :14;
Expand Down Expand Up @@ -911,19 +913,19 @@ namespace dmPhysics
bool IsSleeping2D(HCollisionObject3D collision_object);

/**
* Activate 3D collision object
*
* Activate 3D collision object
*
* @param collision_object
*/
void Wakeup3D(HCollisionObject3D collision_object);

/**
* Wake up collision object
*
*
* @param collision_object
*/
void Wakeup2D(HCollisionObject2D collision_object);

/**
* Set whether the 3D collision object has locked rotation or not, which means that the angular velocity will always be 0.
*
Expand Down Expand Up @@ -1044,6 +1046,24 @@ namespace dmPhysics
*/
float GetMass2D(HCollisionObject2D collision_object);

/**
* Return whether this 2D collision object is a bullet or not. Technically a _bullet_
* is a fast moving collision object requiring special handling by the underlying
* physics engine.
*
* @param collision_object The affected collision object
* @return true if is indeed a bullet
*/
bool IsBullet2D(HCollisionObject2D collision_object);

/**
* Makes this collision object a bullet or not.
*
* @param collision_object The affected collision object
* @param value true to make this a bullet, false otherwise
*/
void SetBullet2D(HCollisionObject2D collision_object, bool value);

/**
* Container of data for ray cast queries.
*/
Expand Down
12 changes: 12 additions & 0 deletions engine/physics/src/physics/physics_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ namespace dmPhysics
def.linearDamping = data.m_LinearDamping;
def.angularDamping = data.m_AngularDamping;
def.fixedRotation = data.m_LockedRotation;
def.bullet = data.m_Bullet;
def.active = data.m_Enabled;
b2Body* body = world->m_World.CreateBody(&def);
Vectormath::Aos::Vector3 zero_vec3 = Vectormath::Aos::Vector3(0);
Expand Down Expand Up @@ -1140,6 +1141,17 @@ namespace dmPhysics
return body->GetMass();
}

bool IsBullet2D(HCollisionObject2D collision_object)
{
b2Body* body = ((b2Body*)collision_object);
return body->IsBullet();
}

void SetBullet2D(HCollisionObject2D collision_object, bool value) {
b2Body* body = ((b2Body*)collision_object);
body->SetBullet(value);
}

void RequestRayCast2D(HWorld2D world, const RayCastRequest& request)
{
if (!world->m_RayCastRequests.Full())
Expand Down
8 changes: 8 additions & 0 deletions engine/physics/src/physics/physics_2d_null.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ namespace dmPhysics
return 0;
}

bool IsBullet2D(HCollisionObject2D collision_object)
{
return false;
}

void SetBullet2D(HCollisionObject2D collision_object, bool value) {
}

void RequestRayCast2D(HWorld2D world, const RayCastRequest& request)
{
}
Expand Down
5 changes: 3 additions & 2 deletions engine/physics/src/physics/physics_common.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2020 The Defold Foundation
// 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
Expand Down Expand Up @@ -57,6 +57,7 @@ namespace dmPhysics
, m_Group(1)
, m_Mask(1)
, m_LockedRotation(0)
, m_Bullet(0)
, m_Enabled(1)
{

Expand Down
4 changes: 4 additions & 0 deletions engine/physics/src/physics/test/test_physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ struct Funcs
typedef void (*SetDebugCallbacks)(typename T::ContextType context, const dmPhysics::DebugCallbacks& callbacks);
typedef void (*ReplaceShapeFunc)(typename T::ContextType context, typename T::CollisionShapeType old_shape, typename T::CollisionShapeType new_shape);
typedef void (*SetGravityFunc)(typename T::WorldType world, const Vectormath::Aos::Vector3& gravity);
typedef bool (*IsBulletFunc)(typename T::CollisionObjectType collision_object);
typedef void (*SetBulletFunc)(typename T::CollisionObjectType collision_object, bool value);
typedef Vectormath::Aos::Vector3 (*GetGravityFunc)(typename T::WorldType world);
};

Expand Down Expand Up @@ -238,6 +240,8 @@ struct Test2D
Funcs<Test2D>::ReplaceShapeFunc m_ReplaceShapeFunc;
Funcs<Test2D>::SetGravityFunc m_SetGravityFunc;
Funcs<Test2D>::GetGravityFunc m_GetGravityFunc;
Funcs<Test2D>::IsBulletFunc m_IsBulletFunc;
Funcs<Test2D>::SetBulletFunc m_SetBulletFunc;

float* m_Vertices;
uint32_t m_VertexCount;
Expand Down

0 comments on commit b3ac504

Please sign in to comment.