4
불규칙한 2D 스프라이트 Farseer의 본문을 만들려고하는 임 3.3.1. 그것은 BodyFactory.CreateCompoundPolygon 메서드를 사용하여 수행 할 수 있습니까?Farseer에서 불규칙한 2D 스프라이트의 본문 만들기
불규칙한 2D 스프라이트 Farseer의 본문을 만들려고하는 임 3.3.1. 그것은 BodyFactory.CreateCompoundPolygon 메서드를 사용하여 수행 할 수 있습니까?Farseer에서 불규칙한 2D 스프라이트의 본문 만들기
이것은 내 프로젝트 중 하나의 방법입니다. 그것은 내 건축물에 대해 조금 구체적이지만 스스로 사용할 수 있어야합니다.
한 가지 고려해야 할 사항은 크기 조정입니다. 당신이 가장 잘 알고있는 ConvertUnits.ToSimUnits 등으로 대체하는 것이 가장 좋습니다.
public static Body CreateBodyFromImage(Game game, World world, string textureName)
{
//Load the passed texture.
Texture2D polygonTexture = game.Content.Load<Texture2D>(textureName);
//Use an array to hold the textures data.
uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];
//Transfer the texture data into the array.
polygonTexture.GetData(data);
//Find the verticals that make up the outline of the passed texture shape.
Vertices vertices = PolygonTools.CreatePolygon(data, polygonTexture.Width);
//For now we need to scale the vertices (result is in pixels, we use meters)
Vector2 scale = new Vector2(0.07f, 0.07f);
vertices.Scale(ref scale);
//Partition the concave polygon into a convex one.
var decomposedVertices = BayazitDecomposer.ConvexPartition(vertices);
//Create a single body, that has multiple fixtures to the polygon shapes.
return BodyFactory.CreateCompoundPolygon(world, decomposedVertices, 1f);
}
신체의 중심을 설정하는 데 문제가 있습니다. Farseer 3.3.1 샘플에 제공된 코드를 따랐습니다. // Vector2 centroid = -textureVertices.GetCentroid(); // textureVertices.Translate(ref centroid); // basketOrigin = -centroid; 이렇게하면 개체가 여전히 움직입니다. –
감사합니다. @ 닉 감사합니다. 화면에서 스프라이트를 볼 수 있습니다. 하지만 충돌은 여전히 일어나지 않으며 강제 적용으로 문제가 발생합니다. (kstate.IsKeyDown (Keys.Down)) _compound.ApplyForce (ConvertUnits.ToSimUnits (Vector2.UnitY)); if (kstate.IsKeyDown (Keys.Up)) _compound.ApplyForce (ConvertUnits.ToSimUnits (-Vector2.UnitY)); 이것은 객체를 변환해야하지만 객체를 회전시키는 것이 아닙니다. 어떤 아이디어로 그렇게하는 이유는 무엇입니까? –
@Yousuf, DebugViewXNA를 실행하고 있습니까? 당신의 몸이 당신의 스프라이트와 같은 위치에 있고 그것이 올바른 척도라는 것을 100 %로하는 것이 현명 할 것입니다. _compound가 메서드를 사용하여 생성 된 본문이라고 가정 할 때 맞습니까? – Nick