SFML 如何创建可步行的地面?
SFML how do I create walkable ground?
据我所知,我设法让它看起来像是在地面上移动的纹理,但实际发生的是我稍微设置了 Ground
精灵的位置在 Character
精灵下方,我只在 X 坐标中移动了 Character
,我让它看起来像是在地面上移动。
现在,我知道必须有另一种方法,可能是像素串通(当 2 个对象 pixels
命中时给出 true
)
无论如何,据我所知,我不知道我是否应该为此使用另一个库。
关于我这样做的低效方式的示例:
sf::texture cTexture,gTexture;
sf::sprite character,ground;
character.setPosition(0,400);
ground.setPosition(0,390);
通过这种方式移动:
character.move(1.5,0.f);
好像会在地上动,我主要说的是,不是。
请告诉我更有效的方法,更好的是 class 友好的方法!
所以基本上您是在尝试制作平台游戏,据我了解,您是在尝试找出以不同于操纵 X、Y 坐标的其他方式移动 sprite 的系统?如果是这样,那么遗憾的是没有其他方法,每个游戏都基于操纵 X、Y、(Z) 坐标并在该位置渲染 texture/sprite/whatever。您真正要寻找的(如果我回答正确的话)是物理学(准确地说是碰撞检测)。 SFML 已经在 sprite class 中进行了一些碰撞检测(实际上在 FloatRect 中,但是 sprite class 由 float rect 变量组成)
//If a sprite contains given point then it will return true
if(sprite.getGlobalBounds().contains(otherSprite.getPosition()))
{
some collision handling, returning to previous position before moving or whatever
}
//or
//If a sprite intersects other "bounding box" then true
if(sprite.getGlobalBounds().intersects(otherSprite.getGlobalBounds()))
{
// same as above
}
我推荐阅读 sfml wiki/manual,yt 上的一些教程或 sfml 指南书。
如果你不喜欢自己处理物理问题,你可以使用 Box2D 库,它是一个非常好的物理库,但如果你开始学习 gamedev 物理的基础知识,那就选择 "manual" 一个 ;)
祝你好运,玩得开心:)
据我所知,我设法让它看起来像是在地面上移动的纹理,但实际发生的是我稍微设置了 Ground
精灵的位置在 Character
精灵下方,我只在 X 坐标中移动了 Character
,我让它看起来像是在地面上移动。
现在,我知道必须有另一种方法,可能是像素串通(当 2 个对象 pixels
命中时给出 true
)
无论如何,据我所知,我不知道我是否应该为此使用另一个库。
关于我这样做的低效方式的示例:
sf::texture cTexture,gTexture;
sf::sprite character,ground;
character.setPosition(0,400);
ground.setPosition(0,390);
通过这种方式移动:
character.move(1.5,0.f);
好像会在地上动,我主要说的是,不是。
请告诉我更有效的方法,更好的是 class 友好的方法!
所以基本上您是在尝试制作平台游戏,据我了解,您是在尝试找出以不同于操纵 X、Y 坐标的其他方式移动 sprite 的系统?如果是这样,那么遗憾的是没有其他方法,每个游戏都基于操纵 X、Y、(Z) 坐标并在该位置渲染 texture/sprite/whatever。您真正要寻找的(如果我回答正确的话)是物理学(准确地说是碰撞检测)。 SFML 已经在 sprite class 中进行了一些碰撞检测(实际上在 FloatRect 中,但是 sprite class 由 float rect 变量组成)
//If a sprite contains given point then it will return true
if(sprite.getGlobalBounds().contains(otherSprite.getPosition()))
{
some collision handling, returning to previous position before moving or whatever
}
//or
//If a sprite intersects other "bounding box" then true
if(sprite.getGlobalBounds().intersects(otherSprite.getGlobalBounds()))
{
// same as above
}
我推荐阅读 sfml wiki/manual,yt 上的一些教程或 sfml 指南书。 如果你不喜欢自己处理物理问题,你可以使用 Box2D 库,它是一个非常好的物理库,但如果你开始学习 gamedev 物理的基础知识,那就选择 "manual" 一个 ;)
祝你好运,玩得开心:)