Beginner desperately needs help:)

Discuss any questions about BEPUphysics or problems encountered.
ole_92
Posts: 35
Joined: Fri Apr 06, 2012 12:08 pm

Beginner desperately needs help:)

Post by ole_92 »

Hello people!
What type of objects can I pass into space.Add(ISpaceObject)?

I know I can add spheres and boxes, but what else?
If I make my own models in Maya and export them as .fbx, how could I add them to the space?
And also how would I add collision detection between my first person camera and the objects?
I think that I need to load like a box or something and always have it at camera position (but without actually rendering it)
Is that the correct approach?

Sorry if these are really dumb questions (this is a first physics engine I'm using )
I would appreciate some help.

Thanks very much!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Beginner desperately needs help:)

Post by Norbo »

What type of objects can I pass into space.Add(ISpaceObject)?

I know I can add spheres and boxes, but what else?
The usual suspects are constraints (joints and motors), static geometry like StaticMesh, InstancedMesh, and Terrain, and entities.
If I make my own models in Maya and export them as .fbx, how could I add them to the space?
Environment geometry is usually best handled by a StaticMesh. You can see an example in the StaticMeshDemo in the BEPUphysicsDemos (contained in the main source download).

You also might want to check out the getting started documentation and demo for more examples.
And also how would I add collision detection between my first person camera and the objects?
One free-flying option would be to toss a dynamic Sphere entity into the space, set its IsAffectedByGravity to false, optionally set the LocalInertiaTensorInverse to the zero matrix to keep it from rotating, and control its motion by setting its LinearVelocity property using user input. The camera itself then follows the sphere entity's Position.

If you need a character with the usual FPS behaviors, check out the BEPUphysicsDemos CharacterController.
ole_92
Posts: 35
Joined: Fri Apr 06, 2012 12:08 pm

Re: Beginner desperately needs help:)

Post by ole_92 »

Thanks, Norbo, for your help!
I solved my first 2 problems, but the character controller is a bit trickier.
I probably need the usual fps controls, so I can't use a sphere.

It might be just me, but that part in demos is quite complicated.
Is there a similar tutorial to "getting started" one, but about character controller?
If not, is there a simple set of steps that I can perform to include it (nothing else) into my game?
Where do I start? Can I use my own camera class?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Beginner desperately needs help:)

Post by Norbo »

It might be just me, but that part in demos is quite complicated. Is there a similar tutorial to "getting started" one, but about character controller?
If not, is there a simple set of steps that I can perform to include it (nothing else) into my game? Where do I start?
The character controller implementation itself is indeed quite complicated, but you don't need to worry about any of the character's implementation.

There is not a separate piece of documentation about the character controller, but the CharacterControllerInput in the BEPUphysicsDemos shows how to use it. The update method goes through and tells the character to do things (move, jump, crouch, etc.). The CharacterControllerInput is not a fundamental part of the CharacterController implementation; it's a layer which processes input into CharacterController commands. Don't mind the camera interpolation stuff too much, either, it just makes going up and down steps smooth and isn't vital to the character :)

To use the character controller, copy-paste the relevant parts of the character into your project source directly. Everything in the BEPUphysicsDemos.AlternateMovement.Character folder except the CharacterControllerInput is simulation-related code. The CharacterControllerInput can be replaced with your own implementation if you want.
Can I use my own camera class?
If you'd like. The engine and character controller do not have any concept of cameras or graphics, it's all physics simulation stuff.
ole_92
Posts: 35
Joined: Fri Apr 06, 2012 12:08 pm

Re: Beginner desperately needs help:)

Post by ole_92 »

Thank you SO much! I got it all working!

Now I just need to fix some minor issues and I'll be ready to start making a game!
Could you pls answer these if you have a minute:

1) How can I adjust the speed of CharacterController? I found jumpSpeed class member, but didn't see velocity .
2) Regarding the models that I created and loaded using StaticMesh, I guess they don't move at all. How can I add models that I can push around and tip over? I tried InstancedMesh but I think it's also static.
3) What would be a good approach and implementation to make it possible to "pick up" objects? To delete them from the space (when looking at them and pressing E for example) and to recreate them in front of the camera?

Thanks again, the answers are really helpful!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Beginner desperately needs help:)

Post by Norbo »

1) How can I adjust the speed of CharacterController? I found jumpSpeed class member, but didn't see velocity .
The character.HorizontalMotionConstraint.Speed sets the target speed of the character while walking. There's some other properties in there too (for difference stances, sliding, or the acceleration of the character in various stances).
2) Regarding the models that I created and loaded using StaticMesh, I guess they don't move at all. How can I add models that I can push around and tip over? I tried InstancedMesh but I think it's also static.
StaticMesh, InstancedMesh, and Terrain are indeed all static.

Entities are moving objects. These include the prefab entity types like Box, Sphere, Cylinder, etc. and the Entity, Entity<T>, and MorphableEntity. The EntityConstructionDemo in the BEPUphysicsDemos various ways of setting up entities.

The MobileMesh prefab entity type in particular can be used to represent meshes. Careful, though- using the mesh directly for dynamic objects is rarely necessary or efficient. Generally, it's best to use something simpler (a simple primitive like a box, a convex hull, or a compound body of multiple such shapes). The graphic then follows the entity. (Note: Due to the differences in origin between the model and a ConvexHullShape or MobileMeshShape created from the model, the graphic will generally need some local offsetting. More information can be found here.)
3) What would be a good approach and implementation to make it possible to "pick up" objects? To delete them from the space (when looking at them and pressing E for example) and to recreate them in front of the camera?
You can teleport an Entity by setting its Position and Orientation properties directly. The entity does not need to be removed from the space to perform this teleportation.

Be careful, though; teleportation is not velocity-based motion, and so collision response will act a bit 'gooey' with the object as springy penetration resolution impulses are applied. If you control the object with velocities instead by setting the LinearVelocity and AngularVelocity such that the object achieves your goals, then collision response will work as expected. Another option is to use motors to do this (SingleEntityLinear/AngularMotor, or the convenience classes EntityMover and EntityRotator). This may not matter, though; if teleportation behaves like you want, it's simple! :)
ole_92
Posts: 35
Joined: Fri Apr 06, 2012 12:08 pm

Re: Beginner desperately needs help:)

Post by ole_92 »

I can increase the speed now, but I couldn't remove the inertia. It just keeps sliding for a bit after i stop pressing the button... I tried to alter the "SlidingSpeed" and "MaximumSlidingForce" variables, but they don't seem to change anything. I also tried changing many other class members of HorisontalMotionConstraint - no luck yet.

I loaded a simple model using MobileMesh, and made it dynamic, gave it a mass of 1, but it doesn't seem to move at collision with camera. Should I change the mass? Or how can I make it collidable with the camera (like the little boxes and spheres)?

Thanks alot
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Beginner desperately needs help:)

Post by Norbo »

I can increase the speed now, but I couldn't remove the inertia. It just keeps sliding for a bit after i stop pressing the button...
The character will indeed continue to move after releasing the key. It will decelerate by applying the MaximumForce, the same force it applies to accelerate. Try setting MaximumForce to something huge like 9999999; the character should immediately reach the maximum speed when moving and immediately stop when movement keys are released.
I tried to alter the "SlidingSpeed" and "MaximumSlidingForce" variables, but they don't seem to change anything.
The sliding properties relate to when the character is touching a surface, but does not have traction on that surface due to an excessively steep slope.
I loaded a simple model using MobileMesh, and made it dynamic, gave it a mass of 1, but it doesn't seem to move at collision with camera. Should I change the mass? Or how can I make it collidable with the camera (like the little boxes and spheres)?
Conceptually, the camera itself will not be doing any colliding. It is following an object which is capable of collision which is itself controlled by user input. A convenient representation is a sphere entity discussed before. To collide with things, that entity will need to be added to the Space, along with every other simulation element.

That's how the CharacterController works- user input tells the entity to scoot around via the constraints, and the camera follows the character's body.
ole_92
Posts: 35
Joined: Fri Apr 06, 2012 12:08 pm

Re: Beginner desperately needs help:)

Post by ole_92 »

Yeah, I've already been using full CharacterController, with crouching, jumping etc. The problem is that the object, that I loaded using MobileMesh, doesn't move away when colliding with the character. So, when I walk up to it, and try to "push" it, it just acts like a StaticMesh that I've been using before (stays still on one spot and doesn't let the character through).

Also, another little issue I have, is that the character doesn't walk up the stairs. I tries scaling the stairs up and down, but it still seems to stop at the first step. Is there any way to fix that?

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

Re: Beginner desperately needs help:)

Post by Norbo »

The problem is that the object, that I loaded using MobileMesh, doesn't move away when colliding with the character. So, when I walk up to it, and try to "push" it, it just acts like a StaticMesh that I've been using before (stays still on one spot and doesn't let the character through).
Could you post the code used to set up the object? The character controller should shove around sufficiently light dynamic objects that are not otherwise constrained.
Also, another little issue I have, is that the character doesn't walk up the stairs. I tries scaling the stairs up and down, but it still seems to stop at the first step. Is there any way to fix that?
The character.StepManager.MaximumStepHeight defines the height of objects which the character can climb. There are also other requirements- the candidate surface must not be too steep, and the candidate step surface must be touching the character. That second requirement means a step that looks like this:

Code: Select all

    /''''''''
   /
__/
will act more as a barrier than a step, because the slope of the vertical part of the step is such that the upper surface is too far from the character body to be detected and it's too tall/steep to just walk over normally.
ole_92
Posts: 35
Joined: Fri Apr 06, 2012 12:08 pm

Re: Beginner desperately needs help:)

Post by ole_92 »

I think I know what was happening - I had a separate mechanism for rendering models - and the graphics wasn't matching the mesh's position.

Now I've imported the ModelDrawer and it works fine, except:
1) The colors of the models are all random - how can I set them to be the original colors of the exported .fbx models?
2) The MobileMesh is acting strange - it's shaking and slowly sinking into the ground for some reason (no matter what mass the object is). Might be something wrong with the floor?
3) I've been attempting to reduce the steepness of the stairs, and the character can now go up the first one, but it stops there. I can't make it less steep, because it will not look like stairs anymore. How does it work in the demo? It looks like each step is exactly at right angle (or very close to).
4) How can I remove backface culling? I used

Code: Select all

graphics.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
before. But now the ModelDrawer doing all the work.


I've attached my code, just in case my questions are not very clear.
Thanks for your help
Attachments
MyGame.zip
(1.18 MiB) Downloaded 305 times
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Beginner desperately needs help:)

Post by Norbo »

1) The colors of the models are all random - how can I set them to be the original colors of the exported .fbx models?
The BEPUphysicsDrawer is just a debugging and simple visualization tool and shouldn't be used as a primary renderer for a game. While it's possible to adjust the colors by changing the color index, I'd recommend not going down that road and just creating a separate renderer capable of showing the physics in the same place as the BEPUphysicsDrawer.
2) The MobileMesh is acting strange - it's shaking and slowly sinking into the ground for some reason (no matter what mass the object is). Might be something wrong with the floor?
It looks like the the collision mesh's sidedness is reversed. In other words, the triangles are one-sided, but in the wrong direction. If the mesh is currently using counterclockwise sidedness, set it to clockwise, or vice versa.
3) I've been attempting to reduce the steepness of the stairs, and the character can now go up the first one, but it stops there. I can't make it less steep, because it will not look like stairs anymore. How does it work in the demo? It looks like each step is exactly at right angle (or very close to).
The steps in the demo are indeed perfect right angles. The problem you are encountering is caused by the step surface being too far away from the character to reach. In the game, the steps look like the ascii art in my previous post, so the character can't detect the step surfaces. They're in the deadzone of being too steep to walk up normally (without stepping), but too sloped to keep the step surface within detection distance (so it can't step).

For example- when walking at the side of your steps where the face is completely vertical and the step surface can be detected, the character steps up.
4) How can I remove backface culling?
The ModelDrawer has a couple of rasterizer states you can alter in the source (fillState, wireframeState). Since it's not a graphics engine, it's not exactly made for easy customization. :)
ole_92
Posts: 35
Joined: Fri Apr 06, 2012 12:08 pm

Re: Beginner desperately needs help:)

Post by ole_92 »

Ok I've recreated the stairs from scratch, with right angles, and they work fine now. I don't even want to know why :D
Now (I think) I'm facing a problem of local offsetting of MobileMesh. It sinks on one side and it doesn't touch the ground on the other.
I've checked in the GraphicsMatching demo, but they use DisplayEntityModel, which needs a ModelDrawer. Is there any way to set the local transform with my current set up? This is the code that I use for the model:

Code: Select all

//-----------------------------
            //--mobile chair 
            //-----------------------------
            Model chair = ((Game1)Game).Content.Load<Model>("Models/chair");
            Vector3[] vertices3;
            int[] indices3;

            TriangleMesh.GetVerticesAndIndicesFromModel(chair, out vertices3, out indices3);
            var transform = new AffineTransform(new Vector3(0, 0, -100));
            var mesh3 = new MobileMesh(vertices3, indices3, transform, MobileMeshSolidity.Counterclockwise, 1);

            space.Add(mesh3);

            EntityModel entMod = new EntityModel(space.Entities[space.Entities.Count - 1], chair, Matrix.Identity, ((Game1)Game));

            ((Game1)Game).Components.Add(entMod);
Thanks
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Beginner desperately needs help:)

Post by Norbo »

I've checked in the GraphicsMatching demo, but they use DisplayEntityModel, which needs a ModelDrawer. Is there any way to set the local transform with my current set up? This is the code that I use for the model:
The offset is only applied to graphics, so it depends on the graphics system you write. The key is to apply a transform in local space to the model which brings the graphic into alignment with the shape before transforming it by the entity's world matrix.

Usually, somewhere, you'll have a line setting the world transform property of an effect. Right now it might look something like:

Code: Select all

effect.World = entity.WorldTransform
To include a local transform, throw it in first:

Code: Select all

effect.World = LocalTransform * entity.WorldTransform;
The local transform should be constructed from the offset described in the shape recentering documentation and graphics matching demo (and any other local transforms you wish to include, though be careful with the order if there's any complex transforms).
ole_92
Posts: 35
Joined: Fri Apr 06, 2012 12:08 pm

Re: Beginner desperately needs help:)

Post by ole_92 »

No, I can't find anything like that in my code. I'm adding the EntityModel directly into Game Componens, which I don't have any control of.
EntityModel has a Transform, so I thought It might be the local transform thing.

Code: Select all

ConvexHull ch = new ConvexHull(vertices3, 10);
             space.Add(ch);
            EntityModel entMod = new EntityModel(ch, chair, Matrix.CreateTranslation(-ch.Position), ((Game1)Game));

            ((Game1)Game).Components.Add(entMod);
But it doesn't work. I know what to to, but I just don't know how to.
Post Reply