Rotation problem after texturetovertice

Topics: Developer Forum, Project Management Forum, User Forum
Dec 4, 2012 at 11:24 AM
Edited Dec 4, 2012 at 1:27 PM

 Hello guys, I am working on a topdown space game using farseer physics. Since today i was using rectangles as the body of my ships and i was moving them by using addforce (to simulate engine bursts). I used the code in XNA samples to transform my texture to vertices and created the body of ships using that code. Everything was working great except when move forward the ship slowly rotates to a specific angle and then moves straight when it reach that angle. Actually two different angles according to the ships current rotation. I guessed that its a center of mass related problem but my texture is symmetrical and texture to vertices code shouldnt mess with its center of mass. I would greatly appreciate your help. 

Thank you.

Oh and the code for texturetovertices:

            uint[] data = new uint[this.texture.Width * this.texture.Height];
            this.texture.GetData(data);
            Vertices textureVertices = PolygonTools.CreatePolygon(data, this.texture.Width, false);
            Vector2 centroid = -textureVertices.GetCentroid();
            textureVertices.Translate(ref centroid);
            textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f);
            List<Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);
            float scale = 1f;
            Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * scale;
            foreach (Vertices vertices in list)
            {
                vertices.Scale(ref vertScale);
            }
            body = BodyFactory.CreateCompoundPolygon(world, list, 1f, BodyType.Dynamic);
            body.BodyType = BodyType.Dynamic;
And my applyforce code: 
obj.body.ApplyForce(new Vector2(obj.ship.getBoosterPower() * obj.ship.acceleration * (float)Math.Cos(obj.body.Rotation), obj.ship.getBoosterPower() * -obj.ship.acceleration * (float)Math.Sin(obj.body.Rotation)));
Dec 4, 2012 at 1:52 PM
Edited Dec 4, 2012 at 1:52 PM

 

obj.body.LinarVelocity += (new Vector2(obj.ship.getBoosterPower() * obj.ship.acceleration * (float)Math.Cos(obj.body.Rotation), obj.ship.getBoosterPower() * -obj.ship.acceleration * (float)Math.Sin(obj.body.Rotation)));

 

Ok i solved the issue by increasing the velocity instead of using applyforce. But it changes the mechanics, i would still like to know if you have suggestions to make applyforce work properly. 

Thank you.

Coordinator
Dec 7, 2012 at 12:24 AM

If the body slowly shift to one of the sides when you apply force to the body (it is applied to the centroid), it is probably due to the centroid being off center. Make sure that the texture to vertices code does not shift the centroid slightly to one of the sides, it can happen due to pixels->meters conversion and rounding issues.

The density of the body should also be the same on all fixtures attached to the body, or the same shifting will happen. I'm not sure what you mean by moving straight down when it reaches an angle. Try implementing the DebugView used in the samples to see what is really going on.

Dec 7, 2012 at 9:49 AM

Thanks a lot Genbox i will check it out. And thanks for this great engine too. :)