Page 1 of 1

Help on StaticMesh and Entity

Posted: Tue Jun 14, 2011 3:20 pm
by das_vicky
Hello,

To give a background, I am going through my first 6 months of Game development experience. I am somewhat new to XNA and absolutely new to Bepu. I am working on developing a baseball type of game(not baseball exactly). So of course I need a playing field, a player who hits, few players who stops the ball and 1 who throws the ball.

So I have a 3D model of the playing field (implemented as a thin cylinder), for which I created a StaticMesh (I assume this is correct way to go for a playing field). I have a 3D model of a ball (implemented as a sphere). So I Initialized a Sphere in Bepu representing the Ball. I am keeping those at 1 units (radius and Mass) as I read the physics works best for values 0.5 -10. What I am observing is that if I throw the ball towards the ground, it skids to the edge of the field, then goes past the field for some distance and then falls off. I expect it to fall right when the field ends, because the field model is kept at default scale. Why this might be happening?

Like real balls I would like to implement a rotating ball when ball moves along ground, but right now, after ball hits the ground it mostly slides and doesn't rotate? What is the appropriate way to make the ball rotate when its moving along the ground? Will making changes to Friction of the Staticmesh help? I am also confused when to use StaticFriction and when to use KineticFriction in real game dev scenarios.

Now regarding my players,

1) 1 Player(human model) who hits the ball with a bat. The Bat is part of the Human model. I need approximate collision detection for entire body but precise collision detection for the Bat (like ball deviating from travel direction if hits the edge.), what are the ways to implement this (represent the human model with bat in Physics Environment and have relatively precise collision detection for bat) in Bepu. My model would have predefined animations for variety of shots, running etc and there would be per bone transformations as well for (to handle things like wrist movement)

2) For fielders who stop the ball, I am thinking of creating a "bigger" box around the player with center positioned at Player's center. Then when the ball collides with the outer Box, I can have Collision event handlers to play specific animation, like dive for catch or simply bend down and pick up ball, then I need to check for collision events at mesh level(collision with hands) to pick up the ball. Once that is done, the fielder needs to throw the ball. For that, the game knows the approximate destination , speed of throw, but the throwing angle (projectile) needs to be calculated. Is the whole thing possible and do we have helper functions to calculate these things (like throwing angle).

Re: Help on StaticMesh and Entity

Posted: Tue Jun 14, 2011 4:00 pm
by Norbo
What I am observing is that if I throw the ball towards the ground, it skids to the edge of the field, then goes past the field for some distance and then falls off. I expect it to fall right when the field ends, because the field model is kept at default scale. Why this might be happening?
I don't have enough information to know for certain, but the graphic may not match the collision model.
Like real balls I would like to implement a rotating ball when ball moves along ground, but right now, after ball hits the ground it mostly slides and doesn't rotate? What is the appropriate way to make the ball rotate when its moving along the ground?
The ball should rotate given the default values, unless the ball's inertia has been manually set to infinity or the friction of the involved objects has been set to 0. Is it truly not rotating, or is the graphic just not rotating?
I am also confused when to use StaticFriction and when to use KineticFriction in real game dev scenarios.
Some types of materials in real life are very sticky when motionless, but slide relatively easy once you get them going. That is the difference between static and kinetic friction. StaticFriction is used when the objects are not moving relative to each other at a contact point. If you don't care about precise tuning of the friction, then just having the StaticFriction around or slightly above KineticFriction will be fine.
what are the ways to implement this (represent the human model with bat in Physics Environment and have relatively precise collision detection for bat) in Bepu.
The easiest way to handle a human-ish shape is something like a Capsule that contains the human shape. The SimpleCharacterController shows an example of this that is capable of moving around.

Bat-ball collision will be difficult. Those are small, extremely fast moving objects with extreme precision requirements. While it is technically possible to make it work, it would probably take an impractically tiny time step to get it to look right, and even then, it may not feel right. If your player doesn't have real-life levels of control over the bat, they'll probably just miss every single time.

Instead, it would be easier (and probably better) to fake it. Perform some calculations based on the timing and type of bat swing and position of the ball to play a proper animation and apply a proper impulse to the ball to send it flying. There would not be an actual collision with a physical bat.
For fielders who stop the ball, I am thinking of creating a "bigger" box around the player with center positioned at Player's center. Then when the ball collides with the outer Box, I can have Collision event handlers to play specific animation, like dive for catch or simply bend down and pick up ball
That'll work.
then I need to check for collision events at mesh level(collision with hands) to pick up the ball.
That won't :) Technically, it would be easy enough to get something to work physically, but it almost certainly won't do what the game needs. I would highly recommend faking this as well. Play an animation that gets you closest to the ball (based on player input or whatever else), and then decide if they caught it. If they did, just pop it into their hands. Otherwise, let it continue.

Modifying animations on the fly using inverse kinematics or something along those lines will make both catching and batting look better, but that's quite a bit of work and the physics engine doesn't help with it.
Once that is done, the fielder needs to throw the ball. For that, the game knows the approximate destination , speed of throw, but the throwing angle (projectile) needs to be calculated. Is the whole thing possible and do we have helper functions to calculate these things (like throwing angle).
That's pretty much all simple projectile motion calculations. There's nothing in the engine to explicitly help with it, but it's trivial to compute the correct velocity and apply it to the ball. Here's an old thread covering a conceptually similar situation: http://www.bepu-games.com/forums/viewto ... 683&p=5096

After all that, you may notice that the physics engine is just being used for updating the position of the ball and detecting collisions between the ball and the environment or trigger volumes (and perhaps the players running around).

Re: Help on StaticMesh and Entity

Posted: Tue Jun 14, 2011 4:45 pm
by das_vicky
This is the code for the Stadium and StaticMesh

Code: Select all

            //StadiumModel
            stadia = new StadiumModel(content.Load<Model>("ThreeDEE/Models/Stadiums/Base/stadium"),
                new Vector3(0f, 0f, 0f), //Position
                new Vector3(0f, 0f, 0f), //Rotation
                new Vector3(1f , 1f , 1f), //Scale
                sceneEngine.GraphicsDevice,
                "Stadia1",   //Descriptive name
                1); //ID for internal use

            //Create a physical environment from a triangle mesh.
            TriangleMesh.GetVerticesAndIndicesFromModel(
                stadia.model, 
                out vertices, 
                out indices);

            //Give the mesh information to a new StaticMesh.  
            var mesh = new StaticMesh(vertices, indices, new AffineTransform(new Vector3(0, 0, 49)));

            stadia.PhysicsEntity = mesh;

            //Add it to the Environment!
            PhysicsEnvironment.Add(mesh);
The ball should rotate given the default values, unless the ball's inertia has been manually set to infinity or the friction of the involved objects has been set to 0. Is it truly not rotating, or is the graphic just not rotating?
I tried with a cube model and the cube model rotates when falls at odd angles. But this ball model is not rotating. I haven't tinkered with any values. The texture is mapped to model and models transformation is dependent on the entities transformation as well, so the graphics should rotate too.

I believe in most games we need to fake lot of the physics stuff, else it would become impossible to play. Even in this type of games, making the whole environment behave nearly as real world also would mean reduced play-ability. So I understand that part, but I am very specific about Bat-Ball collision. I almost got the idea of implementing a human body using capsules, but I didnt understand what to use to represent a bat. Would a cuboid work well? Ofcourse the physics cuboid would be slightly bigger than the graphical bat to help in shot making. Is the problem of attaining precise or atleast 60% precise Bat Edge/Ball collision common to all physics engine or is this specific to Bepu's implementation?

Re: Help on StaticMesh and Entity

Posted: Tue Jun 14, 2011 5:14 pm
by Norbo
I tried with a cube model and the cube model rotates when falls at odd angles. But this ball model is not rotating. I haven't tinkered with any values. The texture is mapped to model and models transformation is dependent on the entities transformation as well, so the graphics should rotate too.
Nothing in there jumps out as incorrect. Double-checking with the BEPUphysicsDrawer might help. You can get the full source for it in the main BEPUphysics source download.
I almost got the idea of implementing a human body using capsules, but I didnt understand what to use to represent a bat. Would a cuboid work well? Ofcourse the physics cuboid would be slightly bigger than the graphical bat to help in shot making.
If you want a semi-realistic response, you'll need a curved shape. A capsule or cylinder would be decent. Another option is a WrappedShape which is composed of a few spheres to approximate the shape of a bat more precisely. A BoxShape would send the ball flying in fairly unpredictable directions due to the box's flat faces (just like if you used a box as a bat in real life), but it's possible to hack something together such that it would still work.

A large box-plank representing the bat would eliminate the unpredictability of the box approach by ensuring the ball only ever hits the one face, but then you'd be playing tennis, not baseball :)
Is the problem of attaining precise or atleast 60% precise Bat Edge/Ball collision common to all physics engine or is this specific to Bepu's implementation?
The problem on the physics side is that you have very fast, tiny objects flying at each other with demands of high physical realism. This requires small timesteps. That is universal to all physics engines. You can also enable continuous collision detection (which BEPUphysics has), but many implementations do not care about angular motion, so a bat will still likely require small timesteps. Increasing the size of the bat to unrealistic proportions helps with these problems. If a simulation really needed it, it can be done. The simulation will likely be simple enough otherwise that running at 120+ hz won't pose a significant problem, even on the Xbox360.

The more significant issue is how the player actually controls the bat. Even with something like Kinect, it would still be difficult to hit the ball with a bat-sized object due to lack of precision in the control scheme. Even if the game had physics quality matching real life, this problem would still occur because the player is not personally holding the bat.

To make it easy to hit the ball, things will likely need to be hacked to be more and more non-physical. Bigger bat shapes, helper forces, autoaiming, etc. At that point, the question would be 'why not just fake the whole thing'? It's apparently good enough for AAA titles :)

Re: Help on StaticMesh and Entity

Posted: Wed Jun 15, 2011 12:58 pm
by das_vicky
What I didn't mention earlier was that the bat is not like a Baseball bat, but more like a plank of wood, which is why Relatively thin cuboid is best option :). Yeah most thing s has to be faked or I would say "articulated". BTW the speeds I would be dealing is not very high, so I would take my chances to see how physics behave.

Another question..I need a pole to be in place in the field. The pole in real life would have some of its part beneath the ground. In physics engine, is it possible to simulate that, so that relatively lighter mass objects don bend it too much when hit (the pole dosent get uprooted, but may get angular displacement), whereas larger mass objects or faster moving objects can make it fall (or uprooted from ground)

Re: Help on StaticMesh and Entity

Posted: Wed Jun 15, 2011 3:15 pm
by Norbo
In physics engine, is it possible to simulate that, so that relatively lighter mass objects don bend it too much when hit (the pole dosent get uprooted, but may get angular displacement), whereas larger mass objects or faster moving objects can make it fall (or uprooted from ground)
Connecting the pole to the environment using a constraint could do it. A BallSocketJoint and an AngularMotor in velocity mode might be a good choice. The ball socket joint would anchor the bottom of the pole to the ground. The angular motor would have a goal velocity of zero. Depending on the tuning of the angular motor (its softness/maximum force), hitting the pole would push it more or less. As it became more and more broken, you could weaken the motor by increasing the softness or decreasing its maximum force. Eventually, if it was too broken, the two constraints would just be removed and it would be uprooted.

Determining how much the pole has been broken could be done by looking at the constraints' TotalImpulse properties each frame as well as the orientation of the pole. Assuming the default state of the pole is to point up along the Y axis, the tilt angle of the pole is Math.Acos(pole.OrientationMatrix.Up.Y).

Re: Help on StaticMesh and Entity

Posted: Thu Jun 16, 2011 5:24 am
by das_vicky
Thanks so much for your suggestions. That seems to be best fitting for my requirement.

Re: Help on StaticMesh and Entity

Posted: Wed Nov 30, 2011 2:06 pm
by jakovd
das_vicky wrote: So I have a 3D model of the playing field (implemented as a thin cylinder), for which I created a StaticMesh (I assume this is correct way to go for a playing field). I have a 3D model of a ball (implemented as a sphere). (...) What I am observing is that if I throw the ball towards the ground, it skids to the edge of the field, then goes past the field for some distance and then falls off. I expect it to fall right when the field ends, because the field model is kept at default scale. Why this might be happening?
This sound like something I've seen recently in my project. My problem was that I was rendering terrain model based on its center of mesh (set in modeling program) and BEPU was using center of bounding box. Those centers where not on the same position. "Proper behavior" was only seen where terrain model and bounding box accidentally overlaped, and that gave me various misleading ideas about what might be wrong. :)