This project is read-only.

physics?

Dec 30, 2011 at 1:03 PM
browsing through your engine im guessing you have not implemented a form of physics into the game. being a fan of jiglib physics engine i am trying to impliment physics. i have a great looking heightmap at which i would like to keep. the issue i run into is jiglib wants a Array2D which is converted from a 2d float array. i cant find a 2d float anywhere in your engine. do you not use this method to calculate vertices. if not is there another way this can be done? if not do you have a physics engine that you recommend using vs jiglib? i have tried creating my own float array by using heightmap.height and heightmap.width but it doesnt come out anywhere near correct. i anyone could help me on this subject that would be great. i really seem to be struggling with this.
Dec 30, 2011 at 1:23 PM
Yes, I'm about to integrate a physics engine, and I'm been looking at the Bepu physics a lot recently.

A quick glance at Jiglib's source code leads me to something like below:

Array2D field = new Array2D(terrain.Heightmap.Width, terrain.Heightmap.Height);
for (int x = 0; x < terrain.Heightmap.Width; x++)
{
for (int z = 0; z < terrain.Heightmap.Height; z++)
{
field.SetAt(x, z, terrain.Heightmap.Heights[x, z]);
}
}
collision.AddPrimitive(new Heightmap(field, terrain.AbsolutePosition.X, terrain.AbsolutePosition.Y,
terrain.Step, terrain.Step), new MaterialProperties(0.7f, 0.7f, 0.6f));

It's probably not going to work, as I'm just writing this out by hand. But you get the idea of how to get the heights, positions and scale. One of the thing I'm worrying about is that the terrain in Nine uses Z axis as the up, while in Jiglib it's using Y as the Up axis, this may cause problems. Also, is there an easy way to visualize the physics model in Jiglib, if so, you can compare the difference between the physics model and the graphics visual. Makes life a lot easier.

Dec 30, 2011 at 3:43 PM
Well shoot that is exactly what I came up with. It is pretty easy to draw the collision skin using a basiceffect you just have to calculate the indices as well. However when I do that it returns somewhere near 6 mil. And xna can not handle that many. Maybe my math is wrong. You mentioned using bepu. Bepu is also a good engine just lacks a little in vehicle physics. Maybe ill try that and see what I come up with. Ill let you know how it goes when I get home tonight. Also I did notice z is up. You mind if I ask why you chose to do it this way? I think if im not mistaking you would usally want y up do to the way fbx file are exported. Not a big issue to me being I use mostly .x and you can choose the up axis.
Dec 31, 2011 at 10:24 AM
Edited Dec 31, 2011 at 10:27 AM

I played with Bepu a little bit today, and here is a code snippet that works with the terrain in Bepu:

            int xLength = surface.Heightmap.Width + 1;
            int yLength = surface.Heightmap.Height + 1;

            var heights = new float[xLength, yLength];
            for (int x = 0; x < xLength; x++)
            {
                for (int y = 0; y < yLength; y++)
                {
                    heights[x, y] = -surface.Heightmap.GetHeight(x, y);
                }
            }

            //Create the terrain.
            var terrain = new Terrain(heights, new AffineTransform(
                    new Vector3(surface.Step, 1, surface.Step),
                    Quaternion.CreateFromYawPitchRoll(0, -MathHelper.PiOver2, 0),
                    surface.AbsolutePosition));

Notice that the width of the heightmap in Nine is the count of cells, not including the borders, so the size of Heightmap.Height is (Width + 1) * (Height + 1).

The reason for choosing z as up is that it makes more sense for me to treat the world as a flat surface seen from the sky, so position (x,y,z) maps directly to the (x,y) on the ground in 2D space.

For models, you almost always want some scale/rotation to make it fit the world and it can be set easily in the content processor.

I'm also use x for the ability to split animations during export. The animation processor in Nine does not handle splits.

Dec 31, 2011 at 1:54 PM
Thanks. You beat me to it. I always forget about the step. Ill give it a shot today. Are you going to be providing a method to adjust terrain height in game? Lets say for use in an editor.
Dec 31, 2011 at 9:11 PM

yufeih, I've spent the last couple weeks building an editor/engine using Nine as the display, and Bepu for the physics - it works great and once the meshes are loaded in Bepu (in my case, both static and convexhull type), drawing the objects in Nine is as simple as grabbing the body transform and passing it to your mesh drawer. Very, very simple, adds literally no extra run-time code after loading. So, good choice :)

Jan 1, 2012 at 5:04 AM

I think Heightmap.LoadHeightmap method can be used to adjust the heights of the terrain in an editor. It takes a new array of heights, and the result will be automatically reflected in DrawableSurface. The downside is that this method update the whole terrain, so it might be a little slow for editing terrains in realtime. I may consider adding an overload that takes a rectangle bounds.

Jan 1, 2012 at 5:06 AM

Glad to hear that it works for you, cannot wait to see your games :)

Jan 1, 2012 at 6:03 AM
Yeah its seems to be a great engine so far. One of the things I like the most is that its open source and still in production with steady updates. And the fact that your quick to reply just adds a cherry on top. Thanks a lot. Great work. I do hope that everything works out good for the game. Everything is going smoothly right now for the most part. I am running into problems drawing a simple sphere model with the modelbatch but I will start a new thread on that separate topic if I have no success.