70 lines
1.9 KiB
C++
70 lines
1.9 KiB
C++
#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);
|
||
}
|