Moving physics from Tick to HandlePhysics
#11
Also, for curved rails, remember that 2 curved rails next to each other count as one diagonal, not 2 curved.
Reply
Thanks given by: tigerw
#12
Curved rails actually masquerade as a single piece straight in a 45-degree angle, so multiple curved rails will connect as a single 45-degree line.
Reply
Thanks given by: tigerw
#13
Useful to know, thanks.

X-soft, did you see the code I pasted?
Reply
Thanks given by:
#14
T-sigerw, can you paste the code inside shcode=cpp instead of raw code tags? The scrolling is bad.

(btw, I'd appreciate if you didn't maul my name)

The HandlePhysics function needs to be structured into logical blocks, something like this:
1. Check if the coords are inside world (Y within 0 .. 255); if not, only call the superclass' handling so that the minecart falls / takes void damage
2. Check if the block is rails. If not, only call the superclass' handling
3. (On a rail) Apply rail physics.

It might even be beneficial to split the "Apply rail physics" into a separate function, for clarity.
Reply
Thanks given by: tigerw
#15
(08-31-2013, 08:43 PM)xoft Wrote: T-sigerw, can you paste the code inside shcode=cpp instead of raw code tags? The scrolling is bad.

(btw, I'd appreciate if you didn't maul my name)

The HandlePhysics function needs to be structured into logical blocks, something like this:
1. Check if the coords are inside world (Y within 0 .. 255); if not, only call the superclass' handling so that the minecart falls / takes void damage
2. Check if the block is rails. If not, only call the superclass' handling
3. (On a rail) Apply rail physics.

It might even be beneficial to split the "Apply rail physics" into a separate function, for clarity.

:DD

Okay, 'x-soft' is how I pronounce it mentally, heh.

1. yay!
2. yay!
3. yay!
4. yay!

Curved rails are progressing well. The diagonal movement tip was really helpful, thanks bear and xoft.
Reply
Thanks given by:
#16
That's it, I'm done with minecarts. The curve code seems to randomly make carts derail, not sure if it's good or bad, but I have no idea why it does that. East west movement seems stiff, but the code is the same as north south, so again, I have no idea. Other than that, it is good-ish.


// Minecart.cpp

// Implements the cMinecart class representing a minecart in the world
// Indiana Jones!

#include "Globals.h"
#include "Minecart.h"
#include "../World.h"
#include "../ClientHandle.h"
#include "Player.h"





cMinecart::cMinecart(ePayload a_Payload, double a_X, double a_Y, double a_Z) :
	super(etMinecart, a_X, a_Y, a_Z, 0.98, 0.7),
	m_Payload(a_Payload)
{
	SetMass(20.f);
	SetMaxHealth(6);
	SetHealth(6);
}




bool cMinecart::Initialize(cWorld * a_World)
{
	if (super::Initialize(a_World))
	{
		a_World->BroadcastSpawnEntity(*this);
		return true;
	}
	return false;
}





void cMinecart::SpawnOn(cClientHandle & a_ClientHandle)
{
	char SubType = 0;
	switch (m_Payload)
	{
		case mpNone:    SubType = 0; break;
		case mpChest:   SubType = 1; break;
		case mpFurnace: SubType = 2; break;
		case mpTNT:     SubType = 3; break;
		case mpHopper:  SubType = 5; break;
		default:
		{
			ASSERT(!"Unknown payload, cannot spawn on client");
			return;
		}
	}
	a_ClientHandle.SendSpawnVehicle(*this, 10, SubType); // 10 = Minecarts, SubType = What type of Minecart
}





void cMinecart::HandlePhysics(float a_Dt, cChunk & a_Chunk)
{
	BLOCKTYPE BelowType = GetWorld()->GetBlock(floor(GetPosX()), floor(GetPosY() -1 ), floor(GetPosZ()));

	if (
		((GetPosY() > 0) && (GetPosY() < cChunkDef::Height)) &&
		(BelowType == E_BLOCK_RAIL) ||
		(BelowType == E_BLOCK_POWERED_RAIL) ||
		(BelowType == E_BLOCK_DETECTOR_RAIL) ||
		(BelowType == E_BLOCK_ACTIVATOR_RAIL)
		)
	{
		ActualHandlePhysics(a_Dt, a_Chunk);
	}
	else
	{
		super::HandlePhysics(a_Dt, a_Chunk);
		BroadcastMovementUpdate();
	}
}





void cMinecart::ActualHandlePhysics(float a_Dt, cChunk & a_Chunk)
{
	
	super::HandlePhysics(a_Dt, a_Chunk); // Main physics handling

	/*
	NOTE: Please bear in mind that taking away from negatives make them even more negative,
	adding to negatives make them positive, etc.
	*/
	
	// Get block meta below the cart
	NIBBLETYPE BelowMeta = GetWorld()->GetBlockMeta(floor(GetPosX()), floor(GetPosY() -1 ), floor(GetPosZ()));
	double SpeedX = GetSpeedX(), SpeedY = GetSpeedY(), SpeedZ = GetSpeedZ(); // Get current speed
	
	switch (BelowMeta)
	{
		case E_RAIL_ZM_ZP: // NORTHSOUTH
		{
			SetRotation(270);
			SpeedY = 0; // Don't move vertically as on ground
			SpeedX = 0;
			
			// Set Y as current Y rounded up to bypass friction
			SetPosY(floor(GetPosY()));

			if (SpeedZ != 0) // Don't do anything if cart is stationary
			{
				if (SpeedZ > 0)
				{
					// Going SOUTH, slow down
					SpeedZ = SpeedZ - 0.1;
				}
				else
				{
					// Going NORTH, slow down
					SpeedZ = SpeedZ + 0.1;
				}
			}
			break;
		}

		case E_RAIL_XM_XP: // EASTWEST
		{
			SetRotation(180);
			SpeedY = 0;
			SpeedZ = 0;

			SetPosY(floor(GetPosY()));

			if (SpeedX != 0)
			{
				if (SpeedX > 0)
				{
					SpeedX = SpeedX - 0.1;
				}
				else
				{
					SpeedX = SpeedX + 0.1;
				}
			}
			break;
		}

		case E_RAIL_ASCEND_ZM: // ASCEND NORTH
		{
			SetRotation(270);
			SetPosY(floor(GetPosY()) + 0.2); // It seems it doesn't work without levitation :/
			SpeedX = 0;

			if (SpeedZ >= 0)
			{
				// SpeedZ POSITIVE, going SOUTH
				if (SpeedZ <= 8) // Speed limit of 8 SOUTH (m/s??)
				{
					SpeedZ = SpeedZ + 0.5; // Speed up
					SpeedY = (0 - SpeedZ); // Downward movement is negative (0 minus positive numbers is negative)
				}
				else
				{
					SpeedZ = 8; // Enforce speed limit
					SpeedY = (0 - SpeedZ);
				}
			}
			else
			{
				// SpeedZ NEGATIVE, going NORTH
				SpeedZ = SpeedZ + 0.4; // Slow down
				SpeedY = (0 - SpeedZ); // Upward movement is positive (0 minus negative number is positive number)
			}
			break;
		}

		case E_RAIL_ASCEND_ZP: // ASCEND SOUTH
		{
			SetRotation(270);
			SetPosY(floor(GetPosY()) + 0.2);
			SpeedX = 0;

			if (SpeedZ > 0)
			{
				// SpeedZ POSITIVE, going SOUTH
				SpeedZ = SpeedZ - 0.4; // Slow down
				SpeedY = SpeedZ; // Upward movement positive
			}
			else
			{
				if (SpeedZ >= -8) // Speed limit of 8 NORTH (m/s??)
				{
					// SpeedZ NEGATIVE, going NORTH
					SpeedZ = SpeedZ - 0.5; // Speed up
					SpeedY = SpeedZ; // Downward movement negative
				}
				else
				{
					SpeedZ = -8; // Enforce speed limit
					SpeedY = SpeedZ;
				}
			}
			break;
		}

		case E_RAIL_ASCEND_XM: // ASCEND EAST
		{
			SetRotation(180);
			SetPosY(floor(GetPosY()) + 0.2);
			SpeedZ = 0;

			if (SpeedX >= 0)
			{
				if (SpeedX <= 8)
				{
					SpeedX = SpeedX + 0.5;
					SpeedY = (0 - SpeedX);
				}
				else
				{
					SpeedX = 8;
					SpeedY = (0 - SpeedX);
				}
			}
			else
			{
				SpeedX = SpeedX + 0.4;
				SpeedY = (0 - SpeedX);
			}
			break;
		}

		case E_RAIL_ASCEND_XP: // ASCEND WEST
		{
			SetRotation(180);
			SetPosY(floor(GetPosY()) + 0.2);
			SpeedZ = 0;

			if (SpeedX > 0)
			{
				SpeedX = SpeedX - 0.4;
				SpeedY = SpeedX;
			}
			else
			{
				if (SpeedX >= -8)
				{
					SpeedX = SpeedX - 0.5;
					SpeedY = SpeedX;
				}
				else
				{
					SpeedX = -8;
					SpeedY = SpeedX;
				}
			}
			break;
		}

		case E_RAIL_CURVED_ZM_XM:
		{
			SetRotation(315);
			SetPosY(floor(GetPosY()) + 0.2);

			if (SpeedZ > 0)
			{
				SpeedX = (0 - SpeedZ);
			}
			else if (SpeedX > 0)
			{
				SpeedZ = (0 - SpeedX);
			}
			break;
		}

		case E_RAIL_CURVED_ZM_XP:
		{
			SetRotation(225);
			SetPosY(floor(GetPosY()) + 0.2);

			if (SpeedZ > 0)
			{
				SpeedX = SpeedZ;
			}
			else if (SpeedX < 0)
			{
				SpeedZ = SpeedX;
			}
			break;
		}

		case E_RAIL_CURVED_ZP_XM:
		{
			SetRotation(135);
			SetPosY(floor(GetPosY()) + 0.2);

			if (SpeedZ < 0)
			{
				SpeedX = SpeedZ;
			}
			else if (SpeedX > 0)
			{
				SpeedZ = SpeedX;
			}
			break;
		}

		case E_RAIL_CURVED_ZP_XP:
		{
			SetRotation(45);
			SetPosY(floor(GetPosY()) + 0.2);

			if (SpeedZ < 0)
			{
				SpeedX = (0 - SpeedZ);
			}
			else if (SpeedX < 0)
			{
				SpeedZ = (0 - SpeedX);
			}
			break;
		}

		default:
		{
			ASSERT(!"Unhandled rail meta!");
			break;
		}
	}/*
	else if (m_bOnGround)
	{
		SetPosY(floor(GetPosY()) + 0.2);
	}*/

	// Set speed to speed variables
	SetSpeedX(SpeedX);
	SetSpeedY(SpeedY);
	SetSpeedZ(SpeedZ);


	// Broadcast position to client
	BroadcastMovementUpdate();
}





void cMinecart::DoTakeDamage(TakeDamageInfo & TDI)
{
	super::DoTakeDamage(TDI);

	if (GetHealth() == 0)
	{
		Destroy(true);
	}
	else
	{
		m_Health = m_Health - 1;
	}
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cEmptyMinecart:

cEmptyMinecart::cEmptyMinecart(double a_X, double a_Y, double a_Z) :
	super(mpNone, a_X, a_Y, a_Z)
{
}





void cEmptyMinecart::OnRightClicked(cPlayer & a_Player)
{
	if (m_Attachee != NULL)
	{
		if (m_Attachee->GetUniqueID() == a_Player.GetUniqueID())
		{
			// This player is already sitting in, they want out.
			a_Player.Detach();
			return;
		}
		
		if (m_Attachee->IsPlayer())
		{
			// Another player is already sitting in here, cannot attach
			return;
		}
		
		// Detach whatever is sitting in this minecart now:
		m_Attachee->Detach();
	}
	
	// Attach the player to this minecart
	a_Player.AttachTo(this);
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cMinecartWithChest:

cMinecartWithChest::cMinecartWithChest(double a_X, double a_Y, double a_Z) :
	super(mpChest, a_X, a_Y, a_Z)
{
}





void cMinecartWithChest::SetSlot(int a_Idx, const cItem & a_Item)
{
	ASSERT((a_Idx >= 0) && (a_Idx < ARRAYCOUNT(m_Items)));
	
	m_Items[a_Idx] = a_Item;
}





void cMinecartWithChest::OnRightClicked(cPlayer & a_Player)
{
	// Show the chest UI window to the player
	// TODO
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cMinecartWithFurnace:

cMinecartWithFurnace::cMinecartWithFurnace(double a_X, double a_Y, double a_Z) :
	super(mpFurnace, a_X, a_Y, a_Z)
{
}





void cMinecartWithFurnace::OnRightClicked(cPlayer & a_Player)
{
	// Try to power the furnace with whatever the player is holding
	// TODO
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cMinecartWithTNT:

cMinecartWithTNT::cMinecartWithTNT(double a_X, double a_Y, double a_Z) :
	super(mpTNT, a_X, a_Y, a_Z)
{
}

// TODO: Make it activate when passing over activator rail





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cMinecartWithHopper:

cMinecartWithHopper::cMinecartWithHopper(double a_X, double a_Y, double a_Z) :
	super(mpHopper, a_X, a_Y, a_Z)
{
}

// TODO: Make it suck up blocks and travel further than any other cart and physics and put and take blocks
// AND AVARYTHING!!

Shall I commit?
Reply
Thanks given by:
#17
Change the if (GetHealth() == 0) to if (GetHealth() <= 0) for example if I would somehow with a plugin hit 100 on a mob its not equal to 0 but much lower.
Reply
Thanks given by:
#18
Why not <=?
Reply
Thanks given by: NiLSPACE
#19
Thats what I ment Wink Thanks.
Reply
Thanks given by:
#20
Your code will still crash the server if the minecart goes out of the world Y-wise. You need to check the Y coord first, and only then you can call cChunk::GetBlock().

I don't like calling a function "ActualSomething" when there's already "Something" - it feels as if "Something" is doing bad job and is being replaced. Why not call it HandlePhysicsOnRail() ?

Also the huuuge switch() in HandlePhysicsOnRail() can be made smaller by leaving out the speed limit check in each branch and doing the check once for all directions after the switch. Not to mention that instead of hardcoding a number (8), you might want to consider using a named constant for it - use "static const double SPEED_LIMIT = 8;" at the top of the file, this will make it easier if we ever decide to tweak the limit or change the speed's units of measurement (I'm seriously considering moving away from m/sec to m/tick to avoid division-by-twenty in all physics code).

Is the "else" branch in cMinecart::DoTakeDamage() needed? The superclass should already decrease the health by the amount specified in the TDI, so unless minecarts take more damage than other entities, that code is wrong.

I think your HandlePhysicsOnRail() should NOT call super::HandlePhysics(), and instead it should adjust the speed (your big switch + limit, add small friction), update the cart position and snap to the rail block's axis (the "center" of the rail).

Any chance you're getting duplicate carts on the client? I think the cMinecart::Initialize() function is not needed at all - the cEntity's Initialize() already calls the BroadcastSpawnEntity(). Try to comment the entire function out (both cpp and header) and see if the carts still spawn, if they do, just delete the function altogether.
Reply
Thanks given by:




Users browsing this thread: 12 Guest(s)