java 加速度_为什么我的球使用加速度作为速度?

在粒子类的实现中,发现一个球体在初始化时,如果使用速度而非加速度,它不会移动。然而,如果初始化时速度设置为加速度,球体会以恒定速度移动。问题可能出在`velocity`和`acceleration`的更新逻辑上。检查`Particle`类的`update`方法以及`Vector`类的运算符重载,确保速度和加速度的正确累加。
摘要由CSDN通过智能技术生成

我有一个 ball 类型的对象,它是类 Particle 的子类 . 该类有三个成员 position , velocity 和 acceleration ,类型为 Vector .

在每个帧上,当调用 myball.update 时,其速度与该位置相加,并且加速度与速度相加 . 然后球在屏幕上绘制 . 无论如何,对于一些不明确的动机,无论我给出的任何值作为速度,球都不会移动,但如果我给出加速度的值,它会以非加速的均匀运动移动 .

这是我的类Vector:

class Vector {

private:

void updateC() {

x = cos(a) * l;

y = sin(a) * l;

}

void updateT() {

a = atan2(y, x);

l = sqrt(x * x + y * y);

}

public:

float x = 0, y = 0;

float a = 0, l = 0;

Vector() {

}

Vector(float nx, float ny): x(nx), y(ny) {

updateT();

}

void X(float nx) {

x = nx;

updateT();

}

void Y(float ny) {

y = ny;

updateT();

}

void A(float na) {

a = na;

updateC();

}

void L(float nl) {

l = nl;

updateC();

}

Vector operator +(Vector other) {

return Vector(x + other.x, y + other.y);

}

Vector operator -(Vector other) {

return Vector(x - other.x, y - other.y);

}

Vector operator *(float m) {

Vector result(x, y);

result.L(l * m);

return result;

}

void operator +=(Vector other) {

x += other.x;

y += other.y;

updateT();

}

void operator -=(Vector other) {

x -= other.x;

y -= other.y;

updateT();

}

void operator *=(float m) {

l *= m;

updateC();

}

};

粒子:

class Particle {

public:

Vector position;

Vector velocity;

Vector gravity;

Particle() {}

Particle(Vector np, Vector nv = Vector(0, 0), Vector na = Vector(0, 0)): position(np), velocity(na), gravity(na) {}

void accelerate(Vector a) {

velocity += a;

}

void update() {

position += velocity;

velocity += gravity;

}

};

和球:

class ball: public Particle {

public:

ball(Vector p, Vector v, Vector a): Particle(p, v, a) {}

void update() {

Particle::update();

graphics.circle("fill", position.x, position.y, 10);

}

};

因此,正如我之前所说的,如果我以不同于0的速度初始化 myball ,球仍然不会移动,但如果我使用加速度作为速度以不同于0的加速度初始化它,它将移动 .

我做错了什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值