libGDX: Hanging rope knotted with some pivot(libGDX:用一些枢轴打结的吊绳)
问题描述
我正在尝试在 libGDX 中制作一根挂绳,其下端有一些物体,绳子应该像 Box2D 中的吊绳
I am trying to make a hanging rope having some object at it lower end in libGDX, Rope should be like hanging rope in Box2D
我做了很多研究,libGDX 有它的 ropeJoint 方法但是如何使用它来制作绳子?
I have done a lot of research, libGDX has its ropeJoint method but how to use it to make a rope?
请帮忙,这将是一个很大的帮助.
Please help, It will be a great favor.
推荐答案
我已经从一个类似问题的答案开始libGDX:吊绳
I've started with the answer to a similar question libGDX: Hanging Rope
并添加了一些代码以添加一些缺失的代码并修复锚点中的一些问题,我认为以下代码适合目标问题:
and added some code to it to add some missing code and fix some problem in anchor, I think the following code is a suitable to the target proble:
BodyDef ropeStartDef = new BodyDef();
int EACH_RING_DISTANCE = 10
ropeStartDef.type = BodyType.StaticBody;
ropeStartDef.position.set(m_camera.viewportWidth/2,m_camera.viewportHeight );
Body ropStartBody = m_world.createBody(ropeStartDef);
PolygonShape ropeStartShape = new PolygonShape();
ropeStartShape.setAsBox(5, 5);
ropStartBody.createFixture(ropeStartShape, 0);
ropeStartShape.dispose();
//----------------------------
RevoluteJointDef jd = new RevoluteJointDef();
Body prevBody = ropStartBody;
int angle = -90;
Vector2 position = ropeStartDef.position;
BodyDef previousbodyDefinition= ropeStartDef;
for(int i=0; i<5; i++)
{
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
bd.angle = angle-MathUtils.PI/2;
bd.position.set(position.x + i*MathUtils.cos(angle)*EACH_RING_DISTANCE,
position.y + i*MathUtils.sin(angle)*EACH_RING_DISTANCE);
Body body = m_world.createBody(bd);
FixtureDef eachRingFD;
eachRingFD = new FixtureDef();
CircleShape chainCircleshape =new CircleShape();
chainCircleshape.setRadius(4);
eachRingFD.density = 2;
eachRingFD.shape =chainCircleshape;
body.createFixture(eachRingFD);
Vector2 anchor = new Vector2(bd.position.x - MathUtils.cos(angle)*EACH_RING_DISTANCE/2f,
bd.position.y - MathUtils.sin(angle)*EACH_RING_DISTANCE/2f);
jd.initialize(prevBody, body, anchor);
//-----------------------added based on http://www.emanueleferonato.com/2009/10/05/basic-box2d-rope/
RevoluteJointDef joint = new RevoluteJointDef();
joint.initialize(prevBody, body, anchor);
m_world.createJoint(joint);
//------------------------end of added
prevBody = body;
previousbodyDefinition = bd;
}
这篇关于libGDX:用一些枢轴打结的吊绳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:libGDX:用一些枢轴打结的吊绳
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01