实施“橡皮筋” Android 中使用 Canvas 转换的相机
我正在为 Android 编写一个 2D 横向卷轴游戏,并尝试实现一种跟随玩家角色上下移动的橡皮筋/弹性相机运动。
例如,假设屏幕的正常状态是字符始终垂直居中。如果角色跳跃,“摄像机”会跟随角色,首先在他上升时向上,然后在他下降时向下。
我已经使用 canvas.translate(x,y)
和如下代码完成了此操作:
drawBackground();
canvas.save();
canvas.translate(0, canvasHeight/2 - player.y);
drawPlayer();
canvas.restore();
但是,这看起来不自然,因为它没有考虑速度,所以无论角色上升或下降的速度有多快,屏幕始终以他为中心。我尝试使用某种画布速度变量,但我似乎无法正确使用。我想要的是,如果玩家静止不动,则摄像机保持在玩家的中心,但是当他跳跃时,摄像机应该“滞后”在他身后一点并尝试赶上。
谁能给我一些关于我应该如何做到这一点的建议?请尽可能具体!
谢谢
I'm writing a 2D side scrolling game for Android, and am trying to implement a kind of rubber banding/elastic camera movement that follows the player's character up and down.
For example, say the normal state of the screen is such that the character is always centered vertically. If the character jumps, the 'camera' follows the character, first upward as he rises, then downward as he falls.
I've accomplished this using canvas.translate(x,y)
with code like this:
drawBackground();
canvas.save();
canvas.translate(0, canvasHeight/2 - player.y);
drawPlayer();
canvas.restore();
However this looks unnatural because it doesn't account for speed, so no matter how fast the character is rising or falling, the screen is always centered on him. I've tried to use some sort of canvas speed variables but I can't seem to get it right. What I want to have is the camera stay centered on the player if he is still, but when he jumps, the camera should 'lag' a little behind him and try to catch up.
Can anyone give me some suggestions on how I should do this? Please be as specific as possible!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需为相机位置编写一个低通滤波器即可:
t
的值越大,相机的反应越快。drawStuff
是一个在不知道你的游戏如何工作的情况下我无法编写的函数。希望你能做到!后续问题
其中 f 是一些函数,描述了给定速度和位置下相机应指向的位置。像这样的东西可能就足够了:
显然,如果相机移动太快,这将使玩家离开屏幕!
Just write a low pass filter for the camera position:
The larger the value of
t
, the faster the reaction of the camera.drawStuff
is a function I can't write without knowing how your game works. Hopefully, you do!Follow up question
Where f is some function that describes where the camera should point for a given velocity and position. Something like this might be sufficient:
Obviously, that will place the player offscreen if the camera moves too fast!