Skip to content
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

Issue 3191 expose isbullet #5905

Merged
merged 7 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
23 changes: 23 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,15 @@ 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) {
// TODO - display not-implemented warning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've done the TODO, so you can remove the comment :)

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 +1214,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)
otsakir marked this conversation as resolved.
Show resolved Hide resolved
{
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 @@ -909,6 +909,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 @@ -1128,6 +1129,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
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