简体   繁体   English

用Box2D制作独轮车

[英]Making a unicycle with Box2D

I'm fairly new to Box2D and trying to figure out the best way to make a unicycle. 我对Box2D很新,并试图找出制造独轮车的最佳方法。 The unicycle essentially is in two pieces, the wheel and the stem (with seat post etc). 独轮车基本上分为两部分,车轮和车轮(带有座杆等)。 I've tried attaching the two with a revolute joint and using a motor for the wheel, which works well except that the stem is then subject to forces from the movement of the wheel. 我已经尝试用旋转接头连接这两个并使用电机作为车轮,除了阀杆受到车轮运动的力量之外,其工作正常。 I want to be able to directly control the rotation of the stem (via the accelerometer on the iPhone), and have it unaffected by the movement of the wheel, except to maintain its position based on the position of the wheel. 我希望能够直接控制杆的旋转(通过iPhone上的加速度计),并且不受车轮运动的影响,除了根据车轮的位置保持其位置。

What is the best way to do this? 做这个的最好方式是什么? How do you control rotation of b2Body's? 你如何控制b2Body的旋转? Should I be using a distance joint instead? 我应该使用距离接头吗? Any help would be appreciated. 任何帮助,将不胜感激。

I see several routes, depending on your needs. 根据您的需要,我会看到几条路线。 Which way is preferable is up to you and your game. 哪种方式更受选择取决于您和您的游戏。

1. Fix the stem's rotation 1.固定杆的旋转

For the bodyDef for the stem set the fixedRotation-flag to true. 对于stem的bodyDef,将fixedRotation-flag设置为true。 This prevents any rotation of the stem (be it by forces from the motor joint, (de)acceleration or collisions. 这可以防止阀杆的任何旋转(无论是来自电机接头的力,(de)加速度还是碰撞。

Than you'd have to manually set the rotation each tick. 比你必须手动设置每个滴答的旋转。 This is easy if it's purely based on the iPhone's position. 如果纯粹基于iPhone的位置,这很容易。 If you still want to calculate other factors into it, things might get from a bit more complicated (eg adding rotation if stem leans too far in one direction) to somewhat painful (have collisions affect the rotation). 如果你仍然想要计算其中的其他因素,事情可能会变得更复杂(例如,如果在一个方向上倾斜太远,则添加旋转)会有些痛苦(碰撞会影响旋转)。

2. Constantly apply balancing forces to the stem Each tick read the stems angular velocity and apply countering forces to balance the stem. 2.不断对杆施加平衡力每个刻度读取杆的角速度并施加反作用力以平衡杆。

While this would probably be more complicated to implement correctly (always find the right force to apply etc.) it may result in a more realistic behaviour as the fixed rotation obviously removes most reactions stem movement would have and how the stem itself is affected by the world. 虽然这可能会更加复杂地正确实施(总是找到适当的力来应用等),但它可能会导致更加真实的行为,因为固定旋转显然会消除干活动所产生的大多数反应以及干itself本身如何受到影响。世界。

3. Don't actually use a wheel While your layout is the obvious choice for a unicycle (and seems to be a somewhat popular choice for all kind of characters) it might not be the best choice from a gameplay point of view. 3.实际上不使用滚轮虽然您的布局是单轮脚踏车的明显选择(并且对于所有类型的角色而言似乎是一种有点流行的选择),但从游戏玩法的角度来看,它可能不是最佳选择。

Instead you could combine the stem and wheel fixtures in a single body (or attach them with a prismatic joint) and create all movement by applying forces to this body. 相反,您可以将杆和车轮固定装置组合在一个整体中(或者将它们与棱柱关节连接起来)并通过向该车身施加力来创造所有运动。 A sensor at the bottom can inform you of ground contact to determine if movement forces are to be applied. 底部的传感器可以通知您地面接触,以确定是否应用移动力。

This way you'd get rid of all the forces a wheel creates (forces to the stem may not be the only unwelcome ones in gameplay) and still have it react to all external forces. 这样你就可以摆脱轮子产生的所有力量(对杆身的力量可能不是游戏中唯一不受欢迎的力量)并且仍然会对所有外力产生反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM