在libgdx中,如何在矩形顶部附加圆形? [英] In libgdx how do you attach a circleshape on top of a rectangle?

查看:77
本文介绍了在libgdx中,如何在矩形顶部附加圆形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个实体,并创建了两个单独的灯具,一个灯具创建一个矩形,另一个灯具创建一个圆形.但是,当我使用.createfixture时,它将圆放在矩形的中心,我希望圆像火柴一样在矩形的顶部.

I have created a body and I have created two separate fixtures, one fixture creates a rectangle shape and the other fixture creates a circle shape. But when I use the .createfixture it puts the circle in the centre of the rectangle, I want the circle on top of the rectangle like a matchstick.

这是我的代码,不知道该怎么办...

here is my code don't know what to do ...

rectangleBodyDef = new BodyDef();
rectangleBodyDef.type = BodyType.DynamicBody;
rectangleBodyDef.position.set(10,20);
rectangleBody = world.createBody(rectangleBodyDef);
rectangleBodyShape = new PolygonShape();
rectangleBodyShape.setAsBox(2f, 0.75f);
rectangleBodyFixtureDef = new FixtureDef();
rectangleBodyFixtureDef.shape = rectangleBodyShape;
rectangleBodyFixtureDef.restitution = 0.8f;
rectangleBody.createFixture(rectangleBodyFixtureDef);


/**********************CREATING THE SECOND BODY (CIRCLE BODY) ************/


circleShape = new CircleShape();
circleShape.setRadius(0.75f);
circleFixtureDef = new FixtureDef();
circleFixtureDef.shape = circleShape;
circleFixtureDef.restitution = 0.8f;
rectangleBody.createFixture(circleFixtureDef);

推荐答案

Fixture Definitions are relative to the body position, set the CircleShape position to be a little above the rectangle one:

CircleShape circleShape = new CircleShape();
circleShape.setRadius(0.75f);

circleShape.setPosition(new Vector2(0,2));   //<------Add this

FixtureDef circleFixtureDef = new FixtureDef();
circleFixtureDef.shape = circleShape;
circleFixtureDef.restitution = 0.8f;
rectangleBody.createFixture(circleFixtureDef);

这篇关于在libgdx中,如何在矩形顶部附加圆形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆