iPad 上使用 cocos2D 和(?)Box2d 进行触控的滚轮动画?
我有一个关于 cocos2d 和 box2d 的问题。 我必须创建一个简单的轮子,根据用户触摸 iPad 应用程序轮子的方式进行旋转。 我希望根据一定的触摸动作,轮子以一定的速度开始,经过一些旋转后开始降低速度直到停止。 (就像经典的幸运轮)。
我需要一些提示。您认为仅使用 cocos2d 可以做到这一点吗?或者我必须使用 Box2D?我已经开始使用Box2d,它非常强大,但是是否可以给一个对象一定的起始速度旋转,然后让世界模拟停止该对象?
多谢! 本扎
I have a question about cocos2d and box2d.
I have to create a simple wheel that rotate according how the user touch the wheel for an Ipad App.
I would like that according a certain touch movements the wheel start with a certain velocity, and after some rotation start to decrease velocity until it stops. (like a classic lucky wheel).
I need some hint. Do you think that is possible to do that using only cocos2d? Or I have to use Box2D? I already started to use Box2d, it's very powerful, but is it possible to give an object a certain starting velocity rotation and after, let the world simulation to stop the object?
Thanks a lot!
Benza
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你现在可能已经想到了,但无论如何..你可以创建轮子并从触摸时的一些初始旋转和速度开始。然后根据您的需要启用摩擦力,在一段时间后会显示物体。
希望有帮助
u might have figured it by now but any ways .. u can create the wheel and start with some initial rotation and velocity on touch. then have friction enable according to your need that would show down the object after some time .
Hope it helps
当我在 Android 上开发完全相同的东西时,我也找不到任何有用的示例/文章来实现这一点,所以我这样做了:
当用户触摸屏幕并进行滑动/滑动(在屏幕上移动手指)并将其从屏幕上删除)您以像素/秒为单位计算 speedX 和 speedY (在 android 中您有一个方法 onFling(float velocitY,float velocitX))。
然后,您将以像素/秒为单位的速度转换为转/秒。这意味着,如果用户的手指在 1 秒内移动 1500 像素,您的轮子将旋转 X 次。例如,如果 fling/swipe speedX 和 speedY 为 1500 px/s 并且您的轮子的半径为 150px,您可以像这样计算轮弧长度:
然后您可以像这样计算每秒转数的速度:
一旦您有了速度您可以每隔 T 毫秒(我使用 40 毫秒)更改轮子旋转角度,如下所示:
您还必须实现拖动,因此轮子最终会减慢速度,如下所示:
您必须调整将值拖动到您想要的效果(减慢的速度)。您可能还想将 spinSpeed 乘以一个常数,这样您就可以获得更高的速度(我使用了 40)。
此实现不包括跟随用户手指的滚轮。为此,您必须计算自上次触摸屏幕以来用户手指移动的距离(对于 X 和 Y 坐标)。所以你需要2个坐标(lastTouchX,lastTouchY,newTouchX,newTouchY)。有了坐标后,您可以计算经过该坐标的直线的斜率或梯度(在此公式中,斜率/梯度为 K:y = kx +n - 标准直线公式)。然后你必须计算这两条线之间的交叉角并将其应用到车轮上。像这样的事情:
希望你能明白
When i developed the exact same thing on Android i also couldn't find any useful examples/articles to implement this, so i did it like this:
When a user touches the screen and does a fling/swipe (moves his finger across the screen and removes it from the screen) you calculate the speedX and speedY in pixels/second (in android you have a method onFling(float velocitY,float velocitX)).
You then convert the speed which is in pixels/second to revolutions/second. This means that if the users fingered would traveled 1500 px in 1 second, your wheel would spin X times. For example if the fling/swipe speedX and speedY is 1500 px/s and your wheel has a radius of 150px, you calculate the wheel arc length like so:
Then you calculate the speed in revolutions per second like this:
Once you have the speed you can change the wheel rotation angle every T miliseconds (i used 40miliseconds) like this:
You also have to implement drag, so the wheel eventually slows down, like this:
You have to adjust the drag_value to your desired effect (how fast it slows down). You may also want to multiply the spinSpeed by a constant, so you get higher speed (i used 40).
This implementation doesn't include the wheel following the users finger. To do that you have to calculate for how much the users finger moved since the last touch of the screen (for X and Y coordinates). So you need 2 coordinates (lastTouchX,lastTouchY,newTouchX,newTouchY). When you have the coordinates you can calculate the slope or gradient of the lines that go through this coordinates (the slope/gradient is K in this formula: y = kx +n - standard line formula). Then you have to calculate the intersection angle between this two lines and apply it to the wheel. Something like this:
Hope you get the idea