【发布时间】:2017-09-18 12:09:18
【问题描述】:
我有一个游戏,球从墙上反弹。它在一个坐标平面上。我希望在弹跳时有一些随机性,以使游戏更有趣。在保持球始终保持恒定速度的同时,我将如何做到这一点?现在我的代码意味着它只能以直角反弹。
窗口的左上角是 0,0,右下角是 winW,winH(现在设置为 800,800)。
ball.cpp sn-p
pos.x = start.x;
pos.y = start.y;
speed.x = .4f; // the f indicates that it's per frame.
speed.y = .4f;
void Ball::hitLeftRight() {
speed.x = -speed.x;
}
void Ball::hitTopBottom() {
speed.y = -speed.y;
}
void Ball::reset() {
// for a new level in game
pos.x = start.x;
pos.y = start.y;
}
void Ball::update() {
// called every frame
pos.y += speed.y;
pos.x += speed.x;
ballShape.setPosition(pos);
}
【问题讨论】:
-
...通过添加一个被钳位的随机生成数?
-
速度和反射角度是独立的,那又如何呢?
标签: c++ math sfml trigonometry