Terrain or space physical propeties

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
nikolaiko
Posts: 43
Joined: Mon Aug 04, 2014 8:26 am

Terrain or space physical propeties

Post by nikolaiko » Thu Aug 28, 2014 10:57 am

Hi, I have question about terrain character controller and their collision.

So, I am creating terrain :

Code: Select all

             foreach (BPTerrain CurrentTerrain in Terrains) {

                var Data = new float[CurrentTerrain.Width, CurrentTerrain.Height];
                for (int i = 0; i < CurrentTerrain.Height; i++) {

                    for (int j = 0; j < CurrentTerrain.Width; j++) {
                        Data[j, i] = CurrentTerrain.HeightData[i * CurrentTerrain.Width + j];
                    }
                }

                Terrain GroundShape = new Terrain(Data,
                                                 new AffineTransform((Vector3)(Position)CurrentTerrain.LocalScale,
                                                                     Quaternion.CreateFromYawPitchRoll(
                                                                         CurrentTerrain.Rotation.Y,
                                                                         CurrentTerrain.Rotation.X,
                                                                         CurrentTerrain.Rotation.Z),
                                                                     (Vector3)(Position)CurrentTerrain.Center));

                GroundShape.Shape.QuadTriangleOrganization = QuadTriangleOrganization.BottomRightUpperLeft;                 
                Space.Add(GroundShape);
                DebugModelsDrawer.Add(GroundShape);
            }
Terrains - list of the BPTerrain objects. BPTerrain - just simple object that contains data from xml parameters file for creating terrain. So, using ModelDrawer from BEPUDrawer project I draw this terrain.
Image
Don't care about box at the left side. Just look on box at the top - this is CharacterController (or I guess character controller's entity) bounding box, I draw it using BoundingBoxDrawer. Here is the code for Character controller and space creation :

Code: Select all


           WorldSpace = new Space();
            WorldSpace.ForceUpdater.Gravity = new BEPUutilities.Vector3(0, -9.81f, 0);
            BoxDrawer = new BoundingBoxDrawer(this);

            cc = new CharacterController(new Vector3(40, 12, 40), CharacterHeight, CharacterHeight / 2.0f, CharacterWidth, 0.1f, 10);
            WorldSpace.Add(cc);
Then I start, character fall down on ground - it's good. But it falling to deep inside :
Image
Part of the character went througth terrain. If after it I start to move character at any direction it slowly (depends on speed) will go above terrain like it supposed to be. I think what it is some kind of physical properties issue, like denisty, friction or something else. I am right? How can I fix this and set terrain as solid ground?

Thanks.

Norbo
Site Admin
Posts: 4588
Joined: Tue Jul 04, 2006 4:45 am

Re: Terrain or space physical propeties

Post by Norbo » Thu Aug 28, 2014 5:31 pm

The CharacterController's bounding box extends above and below the actual character body to detect stepping opportunities, so it is not a good representation of the shape itself.
Part of the character went througth terrain. If after it I start to move character at any direction it slowly (depends on speed) will go above terrain like it supposed to be. I think what it is some kind of physical properties issue, like denisty, friction or something else. I am right? How can I fix this and set terrain as solid ground?
I'm not clear on why the character's bounding box would appear to go rise out of the terrain unless it was actually walking on something that was higher than the terrain or it jumped.

I recommend trying to reproduce the issue in the BEPUphysicsDemos to narrow down where the issue might be.

nikolaiko
Posts: 43
Joined: Mon Aug 04, 2014 8:26 am

Re: Terrain or space physical propeties

Post by nikolaiko » Fri Aug 29, 2014 9:14 am

Thanks for the reply!
Norbo wrote:The CharacterController's bounding box extends above and below the actual character body to detect stepping opportunities, so it is not a good representation of the shape itself.
Maybe it is not good representation, but I done this for one reason. In my application I synchronize my client side user model position with CharacterController.Body.Position (Model.Position = CharacterController.Body.Position). And then user respawns above the ground it fall down and some times it looks like model goes under the terrain. And some times I able to go above but some times all model just fall down throught terrain. So even if bounding boxes extends above and below, looks like character controller body coordinates goes below terrain. I will create some video or screenshot to show what happening.
I'm not clear on why the character's bounding box would appear to go rise out of the terrain unless it was actually walking on something that was higher than the terrain or it jumped.
I recommend trying to reproduce the issue in the BEPUphysicsDemos to narrow down where the issue might be.
I will work on it.

Thanks.

nikolaiko
Posts: 43
Joined: Mon Aug 04, 2014 8:26 am

Re: Terrain or space physical propeties

Post by nikolaiko » Mon Sep 01, 2014 6:45 am

Ok, here is the video of issue in game.

We spawn player (CharacterController) above the ground and then it going down. And fall throught ground. I will try to reproduce this on BepuDemo project and tell about results.

qbvbsite
Posts: 8
Joined: Fri Oct 24, 2014 3:30 am

Re: Terrain or space physical propeties

Post by qbvbsite » Fri Oct 24, 2014 3:37 am

I'm having this exact problem on my Authoritative server. Something the character will come back to the top but will eventually fall through, You see it more trying to go up/down hills.

Basic terrain code:

Code: Select all

var center = new Vector3(terrainCollider.Center.X, terrainCollider.Center.Y, terrainCollider.Center.Z);
                var rotation = new Quaternion(terrainCollider.Rotation.X, terrainCollider.Rotation.Y, terrainCollider.Rotation.Z, terrainCollider.Rotation.W);
                var localScale = new Vector3(terrainCollider.LocalScale.X, terrainCollider.LocalScale.Y, terrainCollider.LocalScale.Z);

                var terrainData = new float[terrainCollider.Width, terrainCollider.Height];
                for (var y = 0; y < terrainCollider.Height; y++)
                {
                    for (var x = 0; x < terrainCollider.Width; x++)
                    {
                        terrainData[x, y] = terrainCollider.HeightData[y*terrainCollider.Width + x];
                    }
                }

                var terrainTransform = new AffineTransform(localScale, rotation, center);
                var terrainShape = new Terrain(terrainData, terrainTransform)
                {
                    Shape = {QuadTriangleOrganization = QuadTriangleOrganization.BottomRightUpperLeft},
                };

                _space.Add(terrainShape);
CharacterController Code

Code: Select all

var spawnPoint = new Vector3(obj.Position.X, obj.Position.Y, obj.Position.Z);

            //Create Character Controller
            var characterController = new CharacterController(spawnPoint, CharacterHeight, CharacterHeight/1.25f, CharacterWidth, 0.05f, 5);
            characterController.Body.CollisionInformation.CollisionRules.Group = _charactersCollisionGroup;

            //Set Character Controller
            physicsObject.CharacterController = characterController;

            //Add To World
            _space.Add(characterController);
Movement Code

Code: Select all

Moving = false;
                MoveDirection = MoveDirection.None;

                //Setup Rotation
                CharacterController.Body.Orientation = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), (float) -Math.PI * (_playerMovement.FacingDirection*0.0055f/180f));

                //Get Current Movement Data
                var forwardMovement = CharacterController.Body.WorldTransform.Forward;
                var upMovement = CharacterController.Body.WorldTransform.Up;
                var directionalMovement = CharacterController.Body.WorldTransform.Right;

                //Normilize Data
                forwardMovement.Normalize();
                upMovement.Normalize();
                directionalMovement.Normalize();

                //Setup movement speed
                MoveSpeed = 0.4f;

                //Set Directional Movement
                if (_playerMovement.DirectionalMovement < 0)
                {
                    MovementDirection -= directionalMovement;
                    MoveDirection |= MoveDirection.Left;
                    Moving = true;
                }
                else if (_playerMovement.DirectionalMovement > 0)
                {
                    MovementDirection += directionalMovement;
                    MoveDirection |= MoveDirection.Right;
                    Moving = true;
                }
                else
                {
                    MovementDirection = new Vector3(0, MovementDirection.Y, MovementDirection.Z);
                }

                //Set Froward Movement
                if (_playerMovement.ForwardMovement < 0)
                {
                    MovementDirection -= forwardMovement;
                    MoveDirection |= MoveDirection.Backward;
                    Moving = true;
                }
                else if (_playerMovement.ForwardMovement > 0)
                {
                    MovementDirection += forwardMovement;
                    MoveDirection |= MoveDirection.Forward;
                    Moving = true;
                }
                else
                {
                    MovementDirection = new Vector3(MovementDirection.X, MovementDirection.Y, 0);
                }

                //If Not Moving Zero Out Movement
                if (!Moving)
                {
                    MovementDirection = new Vector3(0, MovementDirection.Y, 0); ;
                }

                MovementDirection.Normalize();

                //Create vectory based on walk direction and movement speed
                var referanceVector = (MovementDirection*MoveSpeed);
                CharacterController.HorizontalMotionConstraint.MovementDirection = new Vector2(referanceVector.X, referanceVector.Z);
Any thoughts?

the terrain has the correct shape so i'm guessing its loading correctly... and the character interacts with other objects find (even steps on boxes and such)... only problem seems to be with terrain.

qbvbsite
Posts: 8
Joined: Fri Oct 24, 2014 3:30 am

Re: Terrain or space physical propeties

Post by qbvbsite » Fri Oct 24, 2014 5:31 am

If i increase the height map from 512 to 1024 it work great until i try to climb a steep hill / cliff which then I start to go into the terrain again and fall into nothingness.

qbvbsite
Posts: 8
Joined: Fri Oct 24, 2014 3:30 am

Re: Terrain or space physical propeties

Post by qbvbsite » Fri Oct 24, 2014 1:09 pm

Hey,
I just increased the resolution to 2048 and the falling issue no longer happens.... My guess its just hiding it because there are more triangles to collide with and has a less chance of falling through (or maybe it can't now because it catches it faster). Any thoughts on this? It maybe a non-issue since my terrain will probably be 2048 in Unity but I would be nice to have the option on running the server with a less detailed terrain to speed up physics/memory.

--James

Norbo
Site Admin
Posts: 4588
Joined: Tue Jul 04, 2006 4:45 am

Re: Terrain or space physical propeties

Post by Norbo » Fri Oct 24, 2014 5:54 pm

Could you try reproducing this behavior in an isolated demo in the BEPUphysicsDemos for me to look at? I don't have any theories at the moment, and my attempts failed to replicate the issue.
CharacterController.Body.Orientation = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), (float) -Math.PI * (_playerMovement.FacingDirection*0.0055f/180f));
It's almost certainly unrelated, but it is recommended that the CharacterController.Body.Orientation remain unchanged when possible. Changing it constantly can very slightly reduce the quality of contact generation through unnecessary invalidation.

Norbo
Site Admin
Posts: 4588
Joined: Tue Jul 04, 2006 4:45 am

Re: Terrain or space physical propeties

Post by Norbo » Fri Oct 24, 2014 6:02 pm

Woops, found it. There's a bug in the handling of BottomRightUpperLeft. Seems I neglected to include that in the tests. As a temporary workaround, try using BottomLeftUpperRight instead. I'll get a proper fix out in a couple of hours.

Thanks for the reports!

qbvbsite
Posts: 8
Joined: Fri Oct 24, 2014 3:30 am

Re: Terrain or space physical propeties

Post by qbvbsite » Fri Oct 24, 2014 8:17 pm

Hey,
Worked like a charm... thanks for the quick response. If I removed the CharacterController.Body.Orientation how would I adjust the character facing direction? (Not sure where I would set it).

--James

Norbo
Site Admin
Posts: 4588
Joined: Tue Jul 04, 2006 4:45 am

Re: Terrain or space physical propeties

Post by Norbo » Fri Oct 24, 2014 8:26 pm

Since the character's body is rotationally invariant (with regard to yaw, anyway), changing the character orientation is unnecessary as far as physics are concerned. It has no effect on the character controller's operation other than that tiny contact generation issue I mentioned.

The CharacterController does have a ViewDirection property, though. It is used to define the basis for movement, i.e. the forward direction and strafe direction. If the horizontal motion constraint is given a movement direction of +X, it will move along the strafe direction derived from the view direction. If it is +Y, it will move along the forward direction derived from the view direction.

If the goal is to rotate the graphics associated with a character, I would recommend handling it entirely on the graphics side of things. So, that FacingDirection used to change the physical orientation could be used to change the graphical orientation instead.

Norbo
Site Admin
Posts: 4588
Joined: Tue Jul 04, 2006 4:45 am

Re: Terrain or space physical propeties

Post by Norbo » Fri Oct 24, 2014 8:49 pm


qbvbsite
Posts: 8
Joined: Fri Oct 24, 2014 3:30 am

Re: Terrain or space physical propeties

Post by qbvbsite » Fri Oct 24, 2014 10:52 pm

Hey,
Thanks for the great explanation and super quick fix :). Great product btw, its extremely fast and easy to use.

--James

Norbo
Site Admin
Posts: 4588
Joined: Tue Jul 04, 2006 4:45 am

Re: Terrain or space physical propeties

Post by Norbo » Sat Oct 25, 2014 12:20 am

Great product btw, its extremely fast and easy to use.
Glad to hear it's working for you :)

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests