From 96ef6084aedb24821a584bb864f704c98915bf2e Mon Sep 17 00:00:00 2001 From: Samuel Barney Date: Mon, 4 Nov 2013 17:14:49 -0700 Subject: Flowers, mushrooms and air are no longer collidable. --- source/Tracer.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'source') diff --git a/source/Tracer.cpp b/source/Tracer.cpp index 42f1ae5dd..675905b11 100644 --- a/source/Tracer.cpp +++ b/source/Tracer.cpp @@ -225,15 +225,24 @@ int cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int BLOCKTYPE BlockID = m_World->GetBlock(pos.x, pos.y, pos.z); // No collision with water ;) - if ((BlockID != E_BLOCK_AIR) || IsBlockWater(BlockID)) // _X 2013_03_29: Why is the IsBlockWater condition here? water equals air? + switch(BlockID) { - BlockHitPosition = pos; - int Normal = GetHitNormal(a_Start, End, pos ); - if(Normal > 0) + case E_BLOCK_AIR: + case E_BLOCK_YELLOW_FLOWER: + case E_BLOCK_RED_ROSE: + case E_BLOCK_RED_MUSHROOM: + case E_BLOCK_BROWN_MUSHROOM: + break; + default: { - HitNormal = m_NormalTable[Normal-1]; + BlockHitPosition = pos; + int Normal = GetHitNormal(a_Start, End, pos ); + if(Normal > 0) + { + HitNormal = m_NormalTable[Normal-1]; + } + return true; } - return 1; } } return 0; -- cgit v1.2.3 From b23047f47b19722354ce6fcb4fa671dd7e3528f2 Mon Sep 17 00:00:00 2001 From: Samuel Barney Date: Mon, 4 Nov 2013 20:10:29 -0700 Subject: Reworked collision to use g_BlockIsSolid --- source/Tracer.cpp | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'source') diff --git a/source/Tracer.cpp b/source/Tracer.cpp index 675905b11..d7a613891 100644 --- a/source/Tracer.cpp +++ b/source/Tracer.cpp @@ -225,24 +225,15 @@ int cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int BLOCKTYPE BlockID = m_World->GetBlock(pos.x, pos.y, pos.z); // No collision with water ;) - switch(BlockID) + if (g_BlockIsSolid[BlockID]) { - case E_BLOCK_AIR: - case E_BLOCK_YELLOW_FLOWER: - case E_BLOCK_RED_ROSE: - case E_BLOCK_RED_MUSHROOM: - case E_BLOCK_BROWN_MUSHROOM: - break; - default: + BlockHitPosition = pos; + int Normal = GetHitNormal(a_Start, End, pos ); + if(Normal > 0) { - BlockHitPosition = pos; - int Normal = GetHitNormal(a_Start, End, pos ); - if(Normal > 0) - { - HitNormal = m_NormalTable[Normal-1]; - } - return true; + HitNormal = m_NormalTable[Normal-1]; } + return true; } } return 0; -- cgit v1.2.3 From e37531fe0104b1f97d75a24d2b0e03bf507eded8 Mon Sep 17 00:00:00 2001 From: Samuel Barney Date: Tue, 5 Nov 2013 09:22:28 -0700 Subject: Moved cTracer::SetValues to be an internal function because it is only ever used in cTracer::Trace. Removed SetValues from Bindings.cpp. Added some commenting to explain what each function does in cTracer. --- source/Bindings.cpp | 36 ------------------------------------ source/Tracer.h | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 37 deletions(-) (limited to 'source') diff --git a/source/Bindings.cpp b/source/Bindings.cpp index a052d1062..4bcfe8cbb 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -23790,41 +23790,6 @@ static int tolua_AllToLua_cTracer_Trace00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE -/* method: SetValues of class cTracer */ -#ifndef TOLUA_DISABLE_tolua_AllToLua_cTracer_SetValues00 -static int tolua_AllToLua_cTracer_SetValues00(lua_State* tolua_S) -{ -#ifndef TOLUA_RELEASE - tolua_Error tolua_err; - if ( - !tolua_isusertype(tolua_S,1,"cTracer",0,&tolua_err) || - (tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"const Vector3f",0,&tolua_err)) || - (tolua_isvaluenil(tolua_S,3,&tolua_err) || !tolua_isusertype(tolua_S,3,"const Vector3f",0,&tolua_err)) || - !tolua_isnoobj(tolua_S,4,&tolua_err) - ) - goto tolua_lerror; - else -#endif - { - cTracer* self = (cTracer*) tolua_tousertype(tolua_S,1,0); - const Vector3f* a_Start = ((const Vector3f*) tolua_tousertype(tolua_S,2,0)); - const Vector3f* a_Direction = ((const Vector3f*) tolua_tousertype(tolua_S,3,0)); -#ifndef TOLUA_RELEASE - if (!self) tolua_error(tolua_S,"invalid 'self' in function 'SetValues'", NULL); -#endif - { - self->SetValues(*a_Start,*a_Direction); - } - } - return 0; -#ifndef TOLUA_RELEASE - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'SetValues'.",&tolua_err); - return 0; -#endif -} -#endif //#ifndef TOLUA_DISABLE - /* get function: BlockHitPosition of class cTracer */ #ifndef TOLUA_DISABLE_tolua_get_cTracer_BlockHitPosition static int tolua_get_cTracer_BlockHitPosition(lua_State* tolua_S) @@ -30959,7 +30924,6 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,".call",tolua_AllToLua_cTracer_new00_local); tolua_function(tolua_S,"delete",tolua_AllToLua_cTracer_delete00); tolua_function(tolua_S,"Trace",tolua_AllToLua_cTracer_Trace00); - tolua_function(tolua_S,"SetValues",tolua_AllToLua_cTracer_SetValues00); tolua_variable(tolua_S,"BlockHitPosition",tolua_get_cTracer_BlockHitPosition,tolua_set_cTracer_BlockHitPosition); tolua_variable(tolua_S,"HitNormal",tolua_get_cTracer_HitNormal,tolua_set_cTracer_HitNormal); tolua_variable(tolua_S,"RealHit",tolua_get_cTracer_RealHit,tolua_set_cTracer_RealHit); diff --git a/source/Tracer.h b/source/Tracer.h index 2f627366f..a4e2d352e 100644 --- a/source/Tracer.h +++ b/source/Tracer.h @@ -12,14 +12,29 @@ public: // tolua_export Vector3f BoxOffset; cTracer( cWorld* a_World); // tolua_export ~cTracer(); // tolua_export + + /// Determines if a collision occures along a line. int Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance ); // tolua_export - void SetValues( const Vector3f & a_Start, const Vector3f & a_Direction ); // tolua_export + + /// Contains the position of the block that caused the collision Vector3f BlockHitPosition; // tolua_export + + /// Contains which face was hit Vector3f HitNormal; // tolua_export + + /// Contains the exact position where a collision occured. (BlockHitPosition + Offset on block) Vector3f RealHit; // tolua_export private: + + /// Preps Tracer object for call of Trace function. Only used internally + void SetValues( const Vector3f & a_Start, const Vector3f & a_Direction ); + + /// Calculates where on the block a collision occured, if it does occur int intersect3D_SegmentPlane( const Vector3f & a_Origin, const Vector3f & a_End, const Vector3f & a_PlanePos, const Vector3f & a_PlaneNormal ); + + /// Determines which face on the block a collision occured, if it does occur int GetHitNormal( const Vector3f & start, const Vector3f & end, const Vector3i & a_BlockPos); + float SigNum( float a_Num ); cWorld* m_World; -- cgit v1.2.3 From 9d5d74d826ceaa227c3d1684dde0743c08ae1175 Mon Sep 17 00:00:00 2001 From: Samuel Barney Date: Tue, 5 Nov 2013 14:01:51 -0700 Subject: Added more documentation. Changed cTracer::Trace to return a bool instead of an int because it was only returning 1 or 0 anyways. --- source/Tracer.cpp | 8 ++++---- source/Tracer.h | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) (limited to 'source') diff --git a/source/Tracer.cpp b/source/Tracer.cpp index d7a613891..4d036486e 100644 --- a/source/Tracer.cpp +++ b/source/Tracer.cpp @@ -131,12 +131,12 @@ void cTracer::SetValues(const Vector3f & a_Start, const Vector3f & a_Direction) -int cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance) +bool cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance) { if ((a_Start.y < 0) || (a_Start.y >= cChunkDef::Height)) { LOGD("%s: Start Y is outside the world (%.2f), not tracing.", __FUNCTION__, a_Start.y); - return 0; + return false; } SetValues(a_Start, a_Direction); @@ -157,7 +157,7 @@ int cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int // check if first is occupied if (pos.Equals(end1)) { - return 0; + return false; } bool reachedX = false, reachedY = false, reachedZ = false; @@ -236,7 +236,7 @@ int cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int return true; } } - return 0; + return false; } diff --git a/source/Tracer.h b/source/Tracer.h index a4e2d352e..85cb406c8 100644 --- a/source/Tracer.h +++ b/source/Tracer.h @@ -13,8 +13,8 @@ public: // tolua_export cTracer( cWorld* a_World); // tolua_export ~cTracer(); // tolua_export - /// Determines if a collision occures along a line. - int Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance ); // tolua_export + /// Determines if a collision occures along a line. Returns true if a collision occurs. + bool Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance); // tolua_export /// Contains the position of the block that caused the collision Vector3f BlockHitPosition; // tolua_export @@ -26,13 +26,18 @@ public: // tolua_export Vector3f RealHit; // tolua_export private: - /// Preps Tracer object for call of Trace function. Only used internally + /// Preps Tracer object for call of Trace function. Only used internally. void SetValues( const Vector3f & a_Start, const Vector3f & a_Direction ); /// Calculates where on the block a collision occured, if it does occur + /// Returns 0 if no intersection occured + /// Returns 1 if an intersection occured at a single point + /// Returns 2 if the line segment lies in the plane being checked int intersect3D_SegmentPlane( const Vector3f & a_Origin, const Vector3f & a_End, const Vector3f & a_PlanePos, const Vector3f & a_PlaneNormal ); /// Determines which face on the block a collision occured, if it does occur + /// Returns 0 if the block is air, water or no collision occured + /// Return 1 through 6 for the following block faces, repectively: -x, -z, x, z, y, -y int GetHitNormal( const Vector3f & start, const Vector3f & end, const Vector3i & a_BlockPos); float SigNum( float a_Num ); -- cgit v1.2.3 From eefc6d37efd266edad5d1a2cbc2ea6e543e60cc2 Mon Sep 17 00:00:00 2001 From: Samuel Barney Date: Tue, 5 Nov 2013 14:11:13 -0700 Subject: cTracer can now handle mob sight. --- source/Tracer.cpp | 7 ++++--- source/Tracer.h | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'source') diff --git a/source/Tracer.cpp b/source/Tracer.cpp index 4d036486e..bad1604d7 100644 --- a/source/Tracer.cpp +++ b/source/Tracer.cpp @@ -131,7 +131,7 @@ void cTracer::SetValues(const Vector3f & a_Start, const Vector3f & a_Direction) -bool cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance) +bool cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance, bool a_LineOfSight) { if ((a_Start.y < 0) || (a_Start.y >= cChunkDef::Height)) { @@ -224,8 +224,9 @@ bool cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int } BLOCKTYPE BlockID = m_World->GetBlock(pos.x, pos.y, pos.z); - // No collision with water ;) - if (g_BlockIsSolid[BlockID]) + // Block is counted as a collision if we are not doing a line of sight and it is solid, + // or if the block is not air and not water. That way mobs can still see underwater. + if ((!a_LineOfSight && g_BlockIsSolid[BlockID]) || (BlockID != E_BLOCK_AIR && !IsBlockWater(BlockID))) { BlockHitPosition = pos; int Normal = GetHitNormal(a_Start, End, pos ); diff --git a/source/Tracer.h b/source/Tracer.h index 85cb406c8..dc393ae5e 100644 --- a/source/Tracer.h +++ b/source/Tracer.h @@ -14,7 +14,12 @@ public: // tolua_export ~cTracer(); // tolua_export /// Determines if a collision occures along a line. Returns true if a collision occurs. - bool Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance); // tolua_export + bool Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance) // tolua_export + { + return Trace(a_Start, a_Direction, a_Distance, false); + } + + bool Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance, bool a_LineOfSight); // tolua_export /// Contains the position of the block that caused the collision Vector3f BlockHitPosition; // tolua_export -- cgit v1.2.3 From 5d353fd8f82f93ba881e2f770ab65682dcfe9286 Mon Sep 17 00:00:00 2001 From: Samuel Barney Date: Tue, 5 Nov 2013 14:13:12 -0700 Subject: Added missing check for a_LineOfSight --- source/Tracer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source') diff --git a/source/Tracer.cpp b/source/Tracer.cpp index bad1604d7..ef136302f 100644 --- a/source/Tracer.cpp +++ b/source/Tracer.cpp @@ -226,7 +226,7 @@ bool cTracer::Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int BLOCKTYPE BlockID = m_World->GetBlock(pos.x, pos.y, pos.z); // Block is counted as a collision if we are not doing a line of sight and it is solid, // or if the block is not air and not water. That way mobs can still see underwater. - if ((!a_LineOfSight && g_BlockIsSolid[BlockID]) || (BlockID != E_BLOCK_AIR && !IsBlockWater(BlockID))) + if ((!a_LineOfSight && g_BlockIsSolid[BlockID]) || (a_LineOfSight && (BlockID != E_BLOCK_AIR) && !IsBlockWater(BlockID))) { BlockHitPosition = pos; int Normal = GetHitNormal(a_Start, End, pos ); -- cgit v1.2.3 From e6ace0e4f26afba50df1ad21e399fd76e4fcb19e Mon Sep 17 00:00:00 2001 From: Samuel Barney Date: Tue, 5 Nov 2013 14:19:49 -0700 Subject: More documentation. --- source/Tracer.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/Tracer.h b/source/Tracer.h index dc393ae5e..bc348d955 100644 --- a/source/Tracer.h +++ b/source/Tracer.h @@ -18,7 +18,10 @@ public: // tolua_export { return Trace(a_Start, a_Direction, a_Distance, false); } - + + /// Determines if a collision occures along a line. Returns true if a collision occurs. + /// When a_LineOfSight is true, we don't use the standard collision detection rules. Instead we use + /// the rules for monster vision. E.g. Only water and air do not block vision. bool Trace( const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance, bool a_LineOfSight); // tolua_export /// Contains the position of the block that caused the collision -- cgit v1.2.3