Changing Player Size By Player Control

Get technical support about the C++ source code and about Lua scripts for maps, entities, GUIs, the console, materials, etc. Also covered are the Cafu libraries and APIs, as well as compiling, linking, and the build system.
Post Reply
TheWhiteLion
Posts:11
Joined:2019-07-23, 08:28
Changing Player Size By Player Control

Post by TheWhiteLion » 2019-08-07, 18:52

I have been working on implementing a crouch functionality to the engine source code, I have figured out how to change the camera height so the player appears to be crouching, but I am at a lost for how to change the actual collision size of the player. When the player character ducks, the camera perspective will be lowered by 1/2 the collision height and the collision box itself will be halved. How can the collision box of the player character be changed while the game is running?
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Changing Player Size By Player Control

Post by Carsten » 2019-08-08, 17:36

Hello,
please give me some time for answering this. I'm currently very busy and will only be able to look into it over the weekend.
I'm very sorry that I cannot reply sooner, but I'll do my best!
Best regards,
Carsten
TheWhiteLion
Posts:11
Joined:2019-07-23, 08:28

Re: Changing Player Size By Player Control

Post by TheWhiteLion » 2019-08-08, 20:51

Carsten wrote:
2019-08-08, 17:36
Hello,
please give me some time for answering this. I'm currently very busy and will only be able to look into it over the weekend.
I'm very sorry that I cannot reply sooner, but I'll do my best!
Carsten,

I have actually figured it out myself, this is the code that does what I want:

Code: Select all

if (PC.IsDown(PCK_Duck        ) && !PrevPC.IsDown(PCK_Duck) || !PC.IsDown(PCK_Duck) && PrevPC.IsDown(PCK_Duck))		Ducking = !Ducking;
			if (Ducking && !PrevPC.IsDown(PCK_Duck))
			{
				CamOrigin.z = CamOrigin.z / 2;
				CameraTrafo->SetOriginPS(CamOrigin);
				WishVelocity = scale(VectorT(Vel.x,Vel.y,0), 0.5);
				const BoundingBox3dT& HumanPlayerBoundingBox = CompPlayerPhysics->GetDimensions();
				BoundingBox3dT HPBB = HumanPlayerBoundingBox;
				HPBB.Max.z = HPBB.Max.z / 2;
				const BoundingBox3dT& HPBBn = HPBB;
				CompPlayerPhysics->SetDimensions(HPBBn);
			}
			if (!Ducking && PrevPC.IsDown(PCK_Duck))
			{
				CamOrigin.z = CamOrigin.z * 2;
				CameraTrafo->SetOriginPS(CamOrigin);
				WishVelocity = scale(VectorT(Vel.x,Vel.y,0), 1.5);
				const BoundingBox3dT& HumanPlayerBoundingBox = CompPlayerPhysics->GetDimensions();
				BoundingBox3dT HPBB = HumanPlayerBoundingBox;
				HPBB.Max.z = HPBB.Max.z * 2;
				const BoundingBox3dT& HPBBn = HPBB;
				CompPlayerPhysics->SetDimensions(HPBBn);
			}
It's inelegant but the issue is that m_Dimensions of CompPlayerPhysics is a constant so it must be copied into a local variable, assigned to a non const, modified, then assigned to a new local const ref before the original can be changed. If there is a better way then I am unaware, this is what I was able to put together with my limited knowledge of the Cafu API. SetDimensions() is a new function I made to change the bounding box of the player physics entity. The issue with this solution is that the player can raise the character INTO SOLIDS, I need to add a condition where if raising the height of the bounding box were to collide with a solid, then do not raise the bounding box. I haven't figured out how to do that yet though...

Rather than making a new thread, I will ask this question here. I am now working on the function that accelerates the player's movement making it so that the player accelerates gradually rather than immediately. Here is the code:

Code: Select all

void ComponentPlayerPhysicsT::ApplyAcceleration(double FrameTime, PosCatT PosCat, const Vector3dT& WishVelocity)
{
    const  double AirAcceleration   	= 2.5/FrameTime;//1.5 / FrameTime; //units over time
    const  double GroundAcceleration	= 5.0/FrameTime;//6.0 / FrameTime; //units over time
	static double AccelSpeed            = 0.000;

    double    WishSpeed=length(WishVelocity); //this function is called from CompPlayerHuman with WishVelocity set to the current velocity of the player (m_Vel?), the length of the vector is the speed
    //Vector3dT WishDir;

    //if (WishSpeed>0.001 /*Epsilon*/) WishDir=scale(WishVelocity, 1.0/WishSpeed); // I don't know what this means

    // Velocity auf WishDir projezieren um festzustellen, wie schnell wir sowieso schon in WishDir laufen
    //double CurrentSpeed = length(m_Vel);//dot(m_Vel, WishDir);	//the current speed is the length of the wish velocity?
    double Acceleration = 0;
    //double AddSpeed     = 0;

    switch (PosCat)
    {
        case InAir  : // airborn, so little effect on velocity
                      Acceleration=AirAcceleration;
                      //AddSpeed=Acceleration;
                      break;
        case OnSolid: Acceleration=GroundAcceleration;
                      //AddSpeed=Acceleration;
                      // m_Vel.z=0;
                      break;
    }

    //if (Acceleration<=0) return;

	AccelSpeed = /*(WishSpeed * FrameTime * Acceleration) +*/ WishSpeed + (Acceleration * FrameTime); //accelerate the wish speed by adding the acceleration units
    //if (AccelSpeed>WishSpeed) AccelSpeed=CurrentSpeed - WishSpeed;	// ??

    m_Vel = m_Vel + (m_Vel - VectorT(AccelSpeed,AccelSpeed,0));		//add the difference to m_Vel, the current player velocity SHOULD be made slightly greater
}
The idea I have is to take the current player velocity, add the acceleration units, then add the difference between the current player velocity and the accelerated velocity to the current player velocity. What this results in is the player accelerates to tremendous speed instantaneously without me even touching the player control keys. Can you explain what is happening? I really do not understand some of this code, plus it doesn't help that I have had no proper education in vectors or physics.

...after further review I see that my code makes no sense. I have found some helpful information elsewhere on the forums explaining what some of the variables and statements mean, but the variable FrameTime remains a mystery to me. What does it represent? What is the potential range of values of TimeFrame?
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Changing Player Size By Player Control

Post by Carsten » 2019-08-13, 07:37

Hello,
it's good to hear that you could already solve many of the problems yourself! :up:

FrameTime is the time that it took to render the previous frame, e.g. 0.03 seconds.
The code in ComponentPlayerPhysicsT::ApplyAcceleration() computes how much the player's speed must change over FrameTime, that is, within the time that it took to render the previous frame, to eventually reach WishVelociy.
In order to increase ground acceleration, it would have been enough to increase the const for the GroundAcceleration.
Best regards,
Carsten
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests