Skip to content

Commit

Permalink
Basic tests are complete
Browse files Browse the repository at this point in the history
  • Loading branch information
selimanac committed Oct 4, 2024
1 parent c80d9d3 commit fadebfa
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 32 deletions.
5 changes: 4 additions & 1 deletion daabbcc/include/daabbcc/daabbcc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstdint>
#include <daabbcc/collision.h>
#include <dmsdk/dlib/array.h>
#include <dmsdk/dlib/hashtable.h>
Expand Down Expand Up @@ -140,7 +141,7 @@ int32_t AddProxy(uint8_t groupID, float x, float y, uint32_t width, uint32_t hei

void AddGameObject(uint8_t groupID, int32_t proxyID, dmVMath::Point3 position, uint32_t width, uint32_t height, dmGameObject::HInstance gameObjectInstance);

void MoveProxy( int32_t proxyID, float x, float y, uint32_t width, uint32_t height);
void MoveProxy(int32_t proxyID, float x, float y, uint32_t width, uint32_t height);

void UpdateGameobjectSize(uint8_t groupID, int32_t proxyID, uint32_t width, uint32_t height);

Expand Down Expand Up @@ -230,6 +231,8 @@ void Reset();

void ErrorAssert(const char* info, uint8_t groupID);

void LimitErrorAssert(const char* info, uint16_t count);

////////////////////////////////////////
// Tests
////////////////////////////////////////
Expand Down
13 changes: 0 additions & 13 deletions daabbcc/src/daabbcc/CMakeLists.txt

This file was deleted.

61 changes: 48 additions & 13 deletions daabbcc/src/daabbcc/daabbcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ uint8_t AddGroup(uint8_t treeBuildType)
{
if (m_daabbcc.m_dynamicTreeGroup.Full())
{
dmLogError("Can't add new group. Groups are full. Increase the group size.");
LimitErrorAssert("Max Group Count", m_daabbcc.m_dynamicTreeGroup.Size());
return 0;
}

Expand Down Expand Up @@ -122,7 +122,14 @@ void AddGameObject(uint8_t groupID, int32_t proxyID, dmVMath::Point3 position, u
{
GameObject gameObject = {groupID, proxyID, position, gameObjectInstance, width, height};

m_daabbcc.m_GameObjectContainer.Push(gameObject);
if (m_daabbcc.m_GameObjectContainer.Full())
{
LimitErrorAssert("Max Gameobject Count", m_daabbcc.m_GameObjectContainer.Size());
}
else
{
m_daabbcc.m_GameObjectContainer.Push(gameObject);
}
}

void MoveProxy(int32_t proxyID, float x, float y, uint32_t width, uint32_t height)
Expand Down Expand Up @@ -169,7 +176,15 @@ static bool QueryCallback(int32_t proxyID, int32_t groupID, void* queryContainer
return true;
}

m_daabbcc.m_queryResult.Push(proxyID);
if (m_daabbcc.m_queryResult.Full())
{
LimitErrorAssert("Max Query Result Count", m_daabbcc.m_queryResult.Size());
}
else
{
m_daabbcc.m_queryResult.Push(proxyID);
}

return true;
}

Expand All @@ -187,8 +202,15 @@ static bool QuerySortCallback(int32_t proxyID, int32_t groupID, void* queryConta

m_daabbcc.m_sortResult = {proxyID, b2Distance(m_daabbcc.m_aabbCenter, m_queryContainer->m_center)};

m_daabbcc.m_tempSortResults.Push(m_daabbcc.m_sortResult);
m_daabbcc.m_sortResults.Push(m_daabbcc.m_sortResult);
if (m_daabbcc.m_sortResults.Full())
{
LimitErrorAssert("Max Query Result Count", m_daabbcc.m_sortResults.Size());
}
else
{
m_daabbcc.m_tempSortResults.Push(m_daabbcc.m_sortResult);
m_daabbcc.m_sortResults.Push(m_daabbcc.m_sortResult);
}

return true;
}
Expand Down Expand Up @@ -267,7 +289,15 @@ dmArray<SortResult>& GetQuerySortResults() { return m_daabbcc.m_sortResults; }

static float RayCastCallback(const b2RayCastInput* input, int32_t proxyID, int32_t groupID, void* context)
{
m_daabbcc.m_rayResult.Push(proxyID);
if (m_daabbcc.m_rayResult.Full())
{
LimitErrorAssert("Max Raycast Result Count", m_daabbcc.m_rayResult.Size());
}
else
{
m_daabbcc.m_rayResult.Push(proxyID);
}

return input->maxFraction;
}

Expand All @@ -278,8 +308,16 @@ static float RayCastSortCallback(const b2RayCastInput* input, int32_t proxyID, i

m_daabbcc.m_sortRayResult = {proxyID, b2Distance(m_daabbcc.m_aabbCenter, input->origin)};

m_daabbcc.m_tempSortRayResults.Push(m_daabbcc.m_sortRayResult);
m_daabbcc.m_sortRayResults.Push(m_daabbcc.m_sortRayResult);
if (m_daabbcc.m_sortRayResults.Full())
{
LimitErrorAssert("Max Raycast Result Count", m_daabbcc.m_sortRayResults.Size());
}
else
{
m_daabbcc.m_tempSortRayResults.Push(m_daabbcc.m_sortRayResult);
m_daabbcc.m_sortRayResults.Push(m_daabbcc.m_sortRayResult);
}

return input->maxFraction;
}

Expand Down Expand Up @@ -350,7 +388,6 @@ void GameObjectUpdate()
{
m_daabbcc.m_gameObject = &m_daabbcc.m_GameObjectContainer[i];

// TODO COMMENT THIS
m_daabbcc.m_gameObject->m_position = dmGameObject::GetPosition(m_daabbcc.m_gameObject->m_gameObjectInstance);

// TODO Find a better way:
Expand All @@ -360,10 +397,6 @@ void GameObjectUpdate()
Bound(&m_aabb, m_daabbcc.m_gameObject->m_position.getX(), m_daabbcc.m_gameObject->m_position.getY(), m_daabbcc.m_gameObject->m_width, m_daabbcc.m_gameObject->m_height);

b2DynamicTree_MoveProxy(&m_treeGroup->m_dynamicTree, m_daabbcc.m_gameObject->m_proxyID, m_aabb);

// TODO RACE CONDITION BY SETTIN GROUIP ID?
// SetTreeGroup(m_daabbcc.m_gameObject->m_groupID);
// MoveProxy(m_daabbcc.m_gameObject->m_groupID, m_daabbcc.m_gameObject->m_proxyID, m_daabbcc.m_gameObject->m_position.getX(), m_daabbcc.m_gameObject->m_position.getY(), m_daabbcc.m_gameObject->m_width, m_daabbcc.m_gameObject->m_height);
}

m_daabbcc.m_dynamicTreeGroup.Iterate(GameobjectRebuildIterateCallback, (void*)0x0);
Expand Down Expand Up @@ -470,6 +503,8 @@ void Reset()

void ErrorAssert(const char* info, uint8_t groupID) { dmLogError("%s: Group ID [%i] is invalid or already removed!", info, groupID); }

void LimitErrorAssert(const char* info, uint16_t count) { dmLogError("%s reached: %i", info, count); }

////////////////////////////////////////
// Tests
////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion daabbcc/src/daabbcc/dynamic_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ static void b2RemoveLeaf(b2DynamicTree* tree, int32_t leaf)
// instead of a pointer so that we can grow the node pool.
int32_t b2DynamicTree_CreateProxy(b2DynamicTree* tree, b2AABB aabb, uint64_t categoryBits, int32_t userData)
{
dmLogInfo("b2DynamicTree_CreateProxy - categoryBits: %llu", categoryBits);

B2_ASSERT(-b2_huge < aabb.lowerBound.x && aabb.lowerBound.x < b2_huge);
B2_ASSERT(-b2_huge < aabb.lowerBound.y && aabb.lowerBound.y < b2_huge);
B2_ASSERT(-b2_huge < aabb.upperBound.x && aabb.upperBound.x < b2_huge);
Expand Down
4 changes: 2 additions & 2 deletions daabbcc/src/extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ static int RayCastSort(lua_State* L)

static int AddProxy(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 1);
//DM_LUA_STACK_CHECK(L, 1);

uint8_t groupID = luaL_checkint(L, 1);

Expand Down Expand Up @@ -381,7 +381,7 @@ static int AddProxy(lua_State* L)

static int AddGameObject(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 1);
//DM_LUA_STACK_CHECK(L, 1);

uint8_t groupID = luaL_checkint(L, 1);

Expand Down
54 changes: 53 additions & 1 deletion example/example.script
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function init(self)
msg.post("@render:", "use_camera_projection")
profiler.enable_ui(true)
profiler.set_ui_view_mode(profiler.VIEW_MODE_MINIMIZED)

--se the powers of two for each category. Each category should be a unique power of two, allowing for clear and non-overlapping bitwise representations.
local PLAYER = 1 -- 00000000001 in binary (2^0)
Expand Down Expand Up @@ -45,7 +47,7 @@ function init(self)

go_url = msg.url("/test1")
local test1 = daabbcc.insert_gameobject(groupID, go_url, width, height, FLAG_B)

local groupID = daabbcc.new_group(daabbcc.UPDATE_FULLREBUILD)
go_url = msg.url("/test3")
local test3 = daabbcc.insert_gameobject(groupID, go_url, width, height, FLAG_C)

Expand All @@ -63,4 +65,54 @@ function init(self)
if result then
pprint(result)
end


local collision_bits = {
GROUND = 0x00000001,
TEAM1 = 0x00000002,
TEAM2 = 0x00000004,
TEAM3 = 0x00000008,

ALL = bit.bnot(0)
}


local x = 0
local y = 0
local w = 100
local h = 100
local test_group_id = daabbcc.new_group(daabbcc.UPDATE_FULLREBUILD)

--categoryBits
local team1_proxy_id = daabbcc.insert_aabb(test_group_id, x, y, w, h, collision_bits.TEAM1)
local team2_proxy_id = daabbcc.insert_aabb(test_group_id, x, y, w, h, collision_bits.TEAM2)
local team3_proxy_id = daabbcc.insert_aabb(test_group_id, x, y, w, h, collision_bits.TEAM3)

print('team1_proxy_id: ', team1_proxy_id)
print('team2_proxy_id: ', team2_proxy_id)
print('team3_proxy_id: ', team3_proxy_id)

--maskBits
local mask_bits = bit.bor(collision_bits.TEAM1)
print(mask_bits)
local result, count = daabbcc.query_aabb(test_group_id, x, y, w, h, mask_bits)
print('-----:')
print('count:', count)
if result then
for i = 1, count do
print(result[i])
end
end


local result, count = daabbcc.raycast(test_group_id, 300, 300, 0, 0, mask_bits)
print('raycast-----:')
print('count:', count)
if result then
for i = 1, count do
print(result[i])
end
end

daabbcc.run(false)
end
1 change: 0 additions & 1 deletion example/example_masks.script
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function init(self)
print('team2_proxy_id: ', team2_proxy_id)
print('team3_proxy_id: ', team3_proxy_id)


--maskBits
local mask_bits = bit.bor(collision_bits.TEAM1)
print(mask_bits)
Expand Down

0 comments on commit fadebfa

Please sign in to comment.