How can I change the size of a geom dynamically?

Topics: Developer Forum
Jul 5, 2010 at 1:14 PM
Hi. I'm very new to Farseer and at the moment am loving it. I am making a platforming game. At the moment I have platforms that when stood on shrink in size and when left alone for two seconds grow back to their original size. They visually shrink and visually grow, however the geom bounding box doesn't seem to get updated. As I said I am new to Farseer so I expect the solution is a very simple one. Thanks for your time in reading this post and any guidance given I'll be thankful for. Below is a snippet of the code I use to shrink the platform. private void Shrink() { if (width > 0 && height > 0) { Vector2 pos = body.Position; body = BodyFactory.Instance.CreateRectangleBody(width, height, mass); body.Position = pos; geom = GeomFactory.Instance.CreateRectangleGeom(body, width, height); width -= RateOfChange; height -= RateOfChange; } else { width = 0; height = 0; isShrinking = false; } }
Jul 6, 2010 at 5:46 PM
Edited Jul 6, 2010 at 5:46 PM

I am not a developer of Farseer in any way. Just a user like you. From what I know about Farseer, dynamically changing the size of a geom will cause many glitches. I decided to do a little test of it with this code:


            Vertices verts = physicsGeom.LocalVertices;

            for (int i = 0; i < verts.Count; i++)
                verts[i] *= .99f;

            physicsGeom.LocalVertices = verts;


The way you have it coded would make an entirely new geom and body. If you want to do it that way, you have to remove the old body from the simulation and then put your new one in the exact same place. I'm guessing you know that Farseer doesn't hold width and height values in the body or geom either. The code above manipulates the vertices (ton of points that make up a geom). I had to change LocalVertices to being able to be manipulated by editing the Geom class to even try this. The code above does work but it does not handle pushing bodies out of other bodies well AT ALL. Your character will be thrown around in the air when it collides with something like this. Hopefully someone else has a better answer for you. From what I know, this would require a LOT more than just a few lines of code.

Jul 6, 2010 at 10:23 PM

Tumain - are you using 2.1.x or 3.x? If your using 2.1.x, are you using DistanceGrid (default) or SAT (Separation of Axis Theorem)? I need answers to these questions before I can accurately answer your question.


Jul 7, 2010 at 8:01 AM
Thanks sonic. I did follow the remove body and readd body route and it works, however there must be a more efficient way. @mattbetcher: I'm using 2.1.x and am using DistanceGrid. How is it possible to get 3.x since I haven't been able to find a download for it on the codeplex website. I assumed it was in closed beta?
Jul 7, 2010 at 4:33 PM
Tumain: when using the Distance Grid in 2.1.x you must rebuild the geometry for every frame. I recommend precreating all the geometries you will need in a Pool and Add/Remove them in sequence every frame. Or use SAT (Separation of Axis Theorm) for your narrow phase and just change/animate the local vertices. Note that you should not change the number of vertices used for the geometry, but this has not been tested so it may work. Also remember if you go with SAT that you must use all convex geometry. 3.x is available from the source code tab. Just download the latest version. But 3.x is quite different then 2.1.x so be prepared to learn a new engine.
Jul 8, 2010 at 2:09 PM
Thanks for the help mattbetcher. :-) Also would you recommend me moving onto Farseer 3 or staying on 2.1x, since the project I'm working on is quite close to the start?
Jul 8, 2010 at 4:06 PM
Well if your comfortable with figuring out how to scale/render from the Meters/Kilgograms/Seconds that 3.0 uses then I would definitely go with 3.0
Jul 9, 2010 at 8:48 AM
Thanks again for the help. I just tried out 3.1.x but unfortunately I was missing some asset of some sort? I think I'm going to stick with 2.1.x since some problems I had with it have been ironed out, plus I've gotten used to 2.1.x.
Jul 10, 2010 at 6:02 AM
Tumain, the asset was probably a png file in the content project. Just delete it and you should be good to go.