如何创建有界弹跳动画? (SFML)
How to create bounded bounce animation? (SFML)
我必须在打开的 SFML 中以一定角度移动球并将其保持在 window 大小范围内(就像 DVD 一样),但我当前的功能使其到达底部并且没有 "bounce"。它滑过底部并在到达另一个角落后停止。初始位置为(1,1)
void Bubble::updatePosition() {
if( isTopBottom() ){
do{
_x += .1;
_y += -.2;
}while( !isTopBottom() );
}
else if( isLeftRight() ){
do{
_x += -.1;
_y += .2;
}while( !isLeftRight() );
}
else{
_x += .1;
_y += .2;
}
_bubble.setPosition(_x, _y);
}
isLeftRight
、isTopBottom
是检查它们是否到达边缘的布尔值
简单的解决方案
使用速度并在碰撞时操纵速度;然后,使用速度更新位置。
分别检查每条边并据此决定一个相关的速度分量。
例如(遵循您的价值观)
// Positions:
float x = 1.f;
float y = 1.f;
// Velocities:
float vx = 0.1f;
float vy = 0.2f;
// ... then, inside loop:
// Check collisions (and adjust velocity):
if (x < 0.f)
vx = 0.1f;
else if (x > 640.f)
vx = -0.1f;
if (y < 0.f)
vy = 0.2f;
else if (y > 640.f)
vy = -0.2f;
// update position (still inside loop):
x += vx;
y += vy;
更清洁的解决方案
这与上面的简单解决方案相同,但是,由于您标记了 SFML,因此您可以使用 SFML 向量将两个组件保持在一起。还修改了变量名称以使其更加清晰。拉出 window 的大小,速度也从硬编码到逻辑中:
const sf::Vector2f windowSize(640.f, 640.f);
const sf::Vector2f velocityAmount(0.1f, 0.2f);
sf::Vector2f position(1.f, 1.f);
sf::Vector2f velocity = velocityAmount;
// ... then, inside loop:
// Check collisions (and adjust velocity):
if (position.x < 0.f)
velocity.x = velocityAmount.x;
else if (position.x > windowSize.x)
velocity.x = -velocityAmount.x;
if (position.y < 0.f)
velocity.y = velocityAmount.y;
else if (position.y > windowSize.y)
velocity.y = -velocityAmount.y;
// update position (still inside loop):
position += velocity;
您应该注意到速度是在循环的每次迭代中添加到位置的值,并且当不考虑与边缘碰撞时速度不会改变。
初始问题
你最初遇到的问题是,如果它没有撞到边缘,它总是朝同一个方向移动(向右下角)。这意味着它永远不会被允许上升到底部边缘以上(或远离右边缘)。
我必须在打开的 SFML 中以一定角度移动球并将其保持在 window 大小范围内(就像 DVD 一样),但我当前的功能使其到达底部并且没有 "bounce"。它滑过底部并在到达另一个角落后停止。初始位置为(1,1)
void Bubble::updatePosition() {
if( isTopBottom() ){
do{
_x += .1;
_y += -.2;
}while( !isTopBottom() );
}
else if( isLeftRight() ){
do{
_x += -.1;
_y += .2;
}while( !isLeftRight() );
}
else{
_x += .1;
_y += .2;
}
_bubble.setPosition(_x, _y);
}
isLeftRight
、isTopBottom
是检查它们是否到达边缘的布尔值
简单的解决方案
使用速度并在碰撞时操纵速度;然后,使用速度更新位置。 分别检查每条边并据此决定一个相关的速度分量。
例如(遵循您的价值观)
// Positions:
float x = 1.f;
float y = 1.f;
// Velocities:
float vx = 0.1f;
float vy = 0.2f;
// ... then, inside loop:
// Check collisions (and adjust velocity):
if (x < 0.f)
vx = 0.1f;
else if (x > 640.f)
vx = -0.1f;
if (y < 0.f)
vy = 0.2f;
else if (y > 640.f)
vy = -0.2f;
// update position (still inside loop):
x += vx;
y += vy;
更清洁的解决方案
这与上面的简单解决方案相同,但是,由于您标记了 SFML,因此您可以使用 SFML 向量将两个组件保持在一起。还修改了变量名称以使其更加清晰。拉出 window 的大小,速度也从硬编码到逻辑中:
const sf::Vector2f windowSize(640.f, 640.f);
const sf::Vector2f velocityAmount(0.1f, 0.2f);
sf::Vector2f position(1.f, 1.f);
sf::Vector2f velocity = velocityAmount;
// ... then, inside loop:
// Check collisions (and adjust velocity):
if (position.x < 0.f)
velocity.x = velocityAmount.x;
else if (position.x > windowSize.x)
velocity.x = -velocityAmount.x;
if (position.y < 0.f)
velocity.y = velocityAmount.y;
else if (position.y > windowSize.y)
velocity.y = -velocityAmount.y;
// update position (still inside loop):
position += velocity;
您应该注意到速度是在循环的每次迭代中添加到位置的值,并且当不考虑与边缘碰撞时速度不会改变。
初始问题
你最初遇到的问题是,如果它没有撞到边缘,它总是朝同一个方向移动(向右下角)。这意味着它永远不会被允许上升到底部边缘以上(或远离右边缘)。