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?