Files
DNF_DEV/source_game/Actor/Object/ActiveObject.cpp

70 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "ActiveObject.h"
void ActiveObject::SetPosition(VecPos3 pos)
{
BaseObject::SetPosition(pos);
}
void ActiveObject::SetYpos(int y)
{
BaseObject::SetYpos(y);
}
void ActiveObject::SetSpeed(VecSpeed3 speed)
{
this->Speed = speed;
}
VecSpeed3 ActiveObject::GetSpeed()
{
return this->Speed;
}
void ActiveObject::Update(float deltaTime)
{
int IntegerDelta = static_cast<int>(deltaTime * 1000);
const int Gravity = 1020;
// X轴移动含余数补偿
if (Speed.x != 0)
{
int totalX = Speed.x * IntegerDelta + Remainder.x; // 加上上次余数
int moveX = totalX / 1000; // 整数部分为实际移动
Remainder.x = totalX % 1000; // 保留余数(-999~999
MoveBy(moveX, 0, 0);
}
// Y轴移动同上
if (Speed.y != 0)
{
int totalY = Speed.y * IntegerDelta + Remainder.y;
int moveY = totalY / 1000;
Remainder.y = totalY % 1000;
MoveBy(0, moveY, 0);
}
// Z轴重力与移动含余数补偿
if (Position.z > 0 || Speed.z > 0)
{
// 重力对速度的影响先更新Speed.z含余数
int speedZTotal = -Gravity * IntegerDelta + Remainder.z; // 注意负号(减速)
Speed.z += speedZTotal / 1000; // 速度的整数部分
Remainder.z = speedZTotal % 1000; // 速度的余数
// 基于更新后的Speed.z计算移动量同样含余数
int moveZTotal = Speed.z * IntegerDelta + _zRemainderMove; // 新增_zRemainderMove记录移动余数
int moveZ = moveZTotal / 1000;
_zRemainderMove = moveZTotal % 1000;
MoveBy(0, 0, moveZ);
}
else if (Position.z < 0)
{
Position.z = 0;
Speed.z = 0;
Remainder.z = 0; // 重置余数
_zRemainderMove = 0;
SetPosition(Position);
}
BaseObject::Update(deltaTime);
}