Panagiotis Christopoulos Charitos 11 лет назад
Родитель
Сommit
e5d881de48
3 измененных файлов с 18 добавлено и 11 удалено
  1. 1 0
      include/anki/collision/ConvexHullShape.h
  2. 14 8
      src/collision/ConvexHullShape.cpp
  3. 3 3
      src/renderer/Dbg.cpp

+ 1 - 0
include/anki/collision/ConvexHullShape.h

@@ -117,6 +117,7 @@ private:
 	U32 m_pointsCount = 0;
 	U32 m_pointsCount = 0;
 	CollisionAllocator<Vec4> m_alloc;
 	CollisionAllocator<Vec4> m_alloc;
 	Bool8 m_ownsTheStorage = false;
 	Bool8 m_ownsTheStorage = false;
+	Bool8 m_trfIdentity = true; ///< Optimization.
 
 
 	void move(ConvexHullShape& b);
 	void move(ConvexHullShape& b);
 
 

+ 14 - 8
src/collision/ConvexHullShape.cpp

@@ -26,6 +26,7 @@ void ConvexHullShape::destroy()
 	m_pointsCount = 0;
 	m_pointsCount = 0;
 	m_alloc = CollisionAllocator<U8>();
 	m_alloc = CollisionAllocator<U8>();
 	m_ownsTheStorage = false;
 	m_ownsTheStorage = false;
+	m_trfIdentity = true;
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -51,6 +52,9 @@ void ConvexHullShape::move(ConvexHullShape& b)
 
 
 	m_ownsTheStorage = b.m_ownsTheStorage;
 	m_ownsTheStorage = b.m_ownsTheStorage;
 	b.m_ownsTheStorage = false;
 	b.m_ownsTheStorage = false;
+
+	m_trfIdentity = b.m_trfIdentity;
+	b.m_trfIdentity = true;
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -90,7 +94,7 @@ void ConvexHullShape::initStorage(void* buffer, U pointCount)
 F32 ConvexHullShape::testPlane(const Plane& p) const
 F32 ConvexHullShape::testPlane(const Plane& p) const
 {
 {
 	// Compute the invert transformation of the plane instead
 	// Compute the invert transformation of the plane instead
-	Plane pa = p.getTransformed(m_invTrf);
+	Plane pa = (m_trfIdentity) ? p : p.getTransformed(m_invTrf);
 
 
 	F32 minDist = MAX_F32;
 	F32 minDist = MAX_F32;
 
 
@@ -116,6 +120,7 @@ void ConvexHullShape::transform(const Transform& trf)
 {
 {
 	m_trf = m_trf.combineTransformations(trf);
 	m_trf = m_trf.combineTransformations(trf);
 	m_invTrf = m_trf.getInverse();
 	m_invTrf = m_trf.getInverse();
+	m_trfIdentity = false;
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -129,7 +134,7 @@ void ConvexHullShape::computeAabb(Aabb& aabb) const
 	const Vec4* end = m_points + m_pointsCount;
 	const Vec4* end = m_points + m_pointsCount;
 	for(; points != end; ++points)
 	for(; points != end; ++points)
 	{
 	{
-		Vec4 o = m_trf.transform(*points);
+		Vec4 o = (m_trfIdentity) ? *points : m_trf.transform(*points);
 		for(U i = 0; i < 3; ++i)
 		for(U i = 0; i < 3; ++i)
 		{
 		{
 			mina[i] = min(mina[i], o[i]);
 			mina[i] = min(mina[i], o[i]);
@@ -146,21 +151,22 @@ Vec4 ConvexHullShape::computeSupport(const Vec4& dir) const
 	F32 m = MIN_F32;
 	F32 m = MIN_F32;
 	U index = 0;
 	U index = 0;
 
 
+	Vec4 d = (m_trfIdentity) ? dir : (m_invTrf.getRotation() * dir).xyz0();
+
 	const Vec4* points = m_points;
 	const Vec4* points = m_points;
 	const Vec4* end = m_points + m_pointsCount;
 	const Vec4* end = m_points + m_pointsCount;
-	for(; points != end; ++points)
+	U i = 0;
+	for(; points != end; ++points, ++i)
 	{
 	{
-		// XXX Optimize
-		Vec4 o = m_trf.transform(*points);
-		F32 dot = o.dot(dir);
+		F32 dot = points->dot(d);
 		if(dot > m)
 		if(dot > m)
 		{
 		{
 			m = dot;
 			m = dot;
-			index = 0;
+			index = i;
 		}
 		}
 	}
 	}
 
 
-	return m_points[index];
+	return (m_trfIdentity) ? m_points[index] : m_trf.transform(m_points[index]);
 }
 }
 
 
 } // end namespace anki
 } // end namespace anki

+ 3 - 3
src/renderer/Dbg.cpp

@@ -151,13 +151,13 @@ Error Dbg::run(GlCommandBufferHandle& cmdb)
 		MoveComponent& move = sn.getComponent<MoveComponent>();
 		MoveComponent& move = sn.getComponent<MoveComponent>();
 
 
 		hull.initStorage(storage, 4);
 		hull.initStorage(storage, 4);
-		hull.transform(move.getWorldTransform());
 
 
 		Sphere s(Vec4(0.0), 1.0);
 		Sphere s(Vec4(0.0), 1.0);
-		Sphere s2(Vec4(0.01), 1.0);
+
+		hull.transform(move.getWorldTransform());
 
 
 		Gjk gjk;
 		Gjk gjk;
-		Bool collide = gjk.intersect(s, s2);
+		Bool collide = gjk.intersect(s, hull);
 
 
 		if(collide)
 		if(collide)
 		{
 		{