Sample for using 3D models with Farseer 2D physics?

Topics: Developer Forum, User Forum
Mar 29, 2008 at 1:49 PM
Just checking if someone have or could send me an sample of how to use 3D models that uses the Farseer 2D physics engine? Would be really helpful for quick starting my project. Otherwise I have to figure out how to align the objects etc, would be nice with some code sample to see how it's supposed to work in an instance if available.

My mail is <ovikskille at hotmail dot com> if code is not available online somewhere. Thanks a lot for your quick responses in the forum and the very nice engine :)
Apr 7, 2008 at 10:12 AM
Anyone? Got to ditch the engine otherwise I'm afraid, even though it would fit like a glove if the above problem could be solved (dunno how to on my own).
Apr 7, 2008 at 8:58 PM
What part are you having trouble with?

You obviously can't use Farseer as a 3D physics engine, so I assume you're doing a 2D game and just rendering your models as 3D.
In which case, is it just how to use 3D models that you're wondering about?

If you're doing collision, I expect you'll have to keep a seperate mesh for collision than the one you use for rendering.
You could generate the 2D collision mesh from the 3D one without too much trouble though.

Apr 7, 2008 at 9:38 PM
Edited Apr 7, 2008 at 9:40 PM

Kal_Torak wrote:
What part are you having trouble with?

You obviously can't use Farseer as a 3D physics engine, so I assume you're doing a 2D game and just rendering your models as 3D.
In which case, is it just how to use 3D models that you're wondering about?

If you're doing collision, I expect you'll have to keep a seperate mesh for collision than the one you use for rendering.
You could generate the 2D collision mesh from the 3D one without too much trouble though.

Exactly. I'm coding a 2D game (side-scrolling the map and the camera follows the player), but want to use 3D models for visuals (both objects and map). So just want to use Farseer for 2D physics (gravitation, movement and collision). Maybe I'm making this problem a lot more difficult than it is, but am stuck non the less.

The thing I can't work out is how to present 3D objects with aligned and same size 2D meshes. 3D objects are defined by how the camera are looking at it, but 2D are calculated and positioned by X- and Y-axis directly.

Think the questions are:

How can I generate the 2D collision mesh from the 3D one making it the same size when rendered on screen?
How do I know which X and Y-position the 3D object is on screen so I'm able to draw the 2D collision mesh at the same place?
Apr 8, 2008 at 7:25 AM
It's the same as using 2D objects... There really is no special thing you need to take care of, except the scaling of the models.

To get the position and rotation you just need to ask the physicsEngine for that Body.Position / Body.Rotation and use it when rendering the model. Scaling is a problem you will have in every game, no matter if 2D or 3D. The models will in most cases not be of the correct size, except you made ALL of them yourself and kept the ration in mind while modelling.
Apr 8, 2008 at 9:26 AM

Huehnerschaender wrote:
It's the same as using 2D objects... There really is no special thing you need to take care of, except the scaling of the models.

To get the position and rotation you just need to ask the physicsEngine for that Body.Position / Body.Rotation and use it when rendering the model. Scaling is a problem you will have in every game, no matter if 2D or 3D. The models will in most cases not be of the correct size, except you made ALL of them yourself and kept the ration in mind while modelling.

Asking the physics engine for Body.Position will give an X/Y coordinate relating to upper-left of the screen. How can I render an 3D object at that point (this involves setting up a camera with look at, distance from object and angles) and getting the correct positions on screen while moving the object?
Apr 8, 2008 at 11:18 AM
There are different approaches to do this... I am currently working on a 2D game which involves 3D models, too. So basically I have the same "problem" like you, but I solve it in another way like you need to do.
I render my 3D objects on a rendertarget and then paste them as a 2D object into the landscape.

If I understand you right, you want to use 3D for everything, surroundings, level, objects etc. So basically you need to get the positions of the objects by asking the physics engine and render them just where the physics engine says they are whilst ignoring the Z axis.

The desired view, angle etc will be defined by your gamecamera. Thats all. If you have problems with setting up a game camera, this might be the wrong place to discuss it.
But the basic idea is: use the physicsEngines "world" as your 3D world by using X and Y and setting Z to 0.
Apr 8, 2008 at 7:44 PM
I made a sample for you.

You can download it at http://www.dino-it.de/FarseerPhysicsDemos.zip

I added a ninth demo to the farseer demos. Just start demo 9 and then you can see how 3D models can be used in a 2D physics world.

@crashlander: If you want this demo to be part of the demo package, just drop me a note and I will polish it a bit (I hammered it together in 30 minutes just to show what I am talking about).
Apr 9, 2008 at 12:50 PM
Works like a charm! Thanks a lot, you've made my week for sure! Look kinda funny when enabling debug mode (as 2D collision meshes and 3D models aren't displayed at the same place on screen), but that doesn't matter much. Basics are up and running now anyhow, can't thank you enough :)
Apr 9, 2008 at 1:57 PM
You're welcome :)

The implemented debugview is always using 2D coordinates that are oriented to 0,0 of the screen. Thats why it shows the geoms in a "obviously wrong" place. But in fact that doesn't matter here. If you need a debugview for your development you need to adjust the debugview to draw the lines at the exact same vertices, BUT in 3D space. So basically you just need a class that can draw a line in 3D space and there you go (I know there are some classes available that can do this for you (or maybe I will add that to the sample, but it would involve a 3rd party class, which I guess is not wanted by crashlander).

Anyway, the demo should just show you how to do what you were asking for.

Happy developing! :)
Apr 23, 2009 at 8:01 AM
Hi!

I'm very interested in taking a look at that demo but it seems to be no longer available. Can anyone republish it?

Thank you.
Regards.
Apr 23, 2009 at 11:48 AM
i would be interested too!

see also: http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=49363
Jul 28, 2009 at 5:19 PM

Link should work again.

But due to lack of time I did not convert it to recent versions of Farseer and XNA GS, so right now you can just try to comile it or read the code to understand it.

I will convert it when I have some time (most likely after the DreamBuildPlay contest).

http://www.dino-it.de/FarseerPhysicsDemos.zip