XNA Game Development Forums
2012/05/21 19:47:17 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Physics simulation: BulletX (Bullet for XNA) physics and Hazy Mind Xna Engine  (Read 5561 times)
Yubastard
Newbie
*
Offline Offline

Posts: 14


View Profile
« on: 2007/06/21 13:05:38 »

Hi:

I'd like to add Physics Simulation to this wonderful little engine with BulletX from the XNADev.ru guys. I added a Physics manager class.

Here's the code:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using HMEngine.HMObjects;
using XnaDevRu.BulletX;
using XnaDevRu.BulletX.Dynamics;

namespace HMEngine.HMPhysics
{
    public class HMPhysicsManager
    {
        protected DiscreteDynamicsWorld world;
        protected CollisionDispatcher dispatcher;
        protected OverlappingPairCache pairCache;
        protected SequentialImpulseConstraintSolver solver;
        protected Vector3 gravity;
        protected CollisionShape groundPlane;
        public Matrix groundTransformationMatrix;
        public List<RigidBody> bodies = new List<RigidBody>();

        //constructor
        public HMPhysicsManager()
        {
            dispatcher = new CollisionDispatcher();
            dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeTypes.Sphere, BroadphaseNativeTypes.Cylinder, new SphereTriangleCollisionAlgorithm.CreateFunc());
            dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeTypes.Sphere, BroadphaseNativeTypes.Sphere, new SphereSphereCollisionAlgorithm.CreateFunc());
            dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeTypes.Sphere, BroadphaseNativeTypes.Box, new SphereBoxCollisionAlgorithm.CreateFunc());
            dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeTypes.StaticPlane, BroadphaseNativeTypes.Sphere, new SphereTriangleCollisionAlgorithm.CreateFunc());
           
            solver = new SequentialImpulseConstraintSolver();
            pairCache = new SimpleBroadphase();
            gravity = new Vector3(0, -9.8f, 0);

            world = new DiscreteDynamicsWorld(dispatcher, pairCache,solver);
            world.Gravity = gravity;

            //groundPlane = new BoxShape(new Vector3(50, 50, 1));
            groundTransformationMatrix = Matrix.Identity;
            groundTransformationMatrix.Translation = new Vector3(0, 0, 0);           

            //bodies.Add(localCreateRigidBody(0,groundTransformationMatrix,groundPlane));
        }
        //funcs
        private RigidBody localCreateRigidBody(float mass, Matrix trans, CollisionShape shape)
        {
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool isDynamic = (mass != 0);

            Vector3 localInertia = new Vector3();
            if (isDynamic)
                shape.CalculateLocalInertia(mass, out localInertia);

            DefaultMotionState ms = new DefaultMotionState(trans, Matrix.Identity);
            RigidBody body = new RigidBody(mass, ms, shape, localInertia, 0, 0, 0.5f, 0);

            World.AddRigidBody(body);

            return body;
        }

        //public propeties
        public Matrix Transform
        {
            set { groundTransformationMatrix = value; }
            get { return groundTransformationMatrix; }
        }
        public DiscreteDynamicsWorld World
        {
            set { world = value; }
            get { return world; }
        }
        public Vector3 Gravity
        {
            set { World.Gravity = gravity = value; }
            get { return gravity; }
        }

    }
}


Now I wonder:  each model I want to simulate physics on should have it's own CollisionShape attached to it some how, no? Even if it's invisible...

should I put this CollisionShape, as a variable, in the HMModel class? It shouldn't be constant, right?

or maybe some sort of IHMPhysics interface for the HMModel class?

or maybe even a PhysicsModel class that inherits from HMModel class?

I'm in doubt as to where to put this CollisionShape so I can make a RigidBody with it... AND keep it attached to the model.

any help in this matter would be appreciated!  Cheesy

I just love this engine and woujld like to expand it.  Cool
Logged
Yubastard
Newbie
*
Offline Offline

Posts: 14


View Profile
« Reply #1 on: 2007/06/22 07:41:48 »

well I tried with such a manager class, and no luck...

I added a CollisionShape member variable to the HMModel class, instantiated a HMPhysicsManager class object, then added RigidBodies to the DiscreeteDynamicsWorld using the CollisionShapes (from the skulls added to HMOctree in the last tutorial), a mass of 1 (or 5, whatever, as long as its > 0) and a transformation matrix (I'm sending a Matrix.Identity). Since the API only says "trans" as the CollisionShape Matrix parameter name, I'm confused as it may be "Transformation" or "Translation"

I added HMPhysicsManager.World.stepSimulation(60.0f); to the Update(gameTime); method

The skulls on the Octree do not fall...

Maybe it's the Matrix I'm passing on to the rigid body... I've tried Matrix.Identity, a matrix made up from the multiplication of each model's position vector (to keep it stuck to the CollisionShape), the physics manager's transform matrix and there's no effect. I still don't understand very well the thing about matrixes, can I pass a Matrix.Identity always?

or maybe it's something with the Octree and bounding volumes already there...

any help? thanks!
« Last Edit: 2007/06/22 08:03:10 by Yubastard » Logged
Yubastard
Newbie
*
Offline Offline

Posts: 14


View Profile
« Reply #2 on: 2007/06/26 21:31:44 »

I did it!!!!! OMG, I did it!!!!  Shocked

I'm literally crying!!! after all this time of burning my eyelashes the skulls fell!! they fell through, they did indeed!

it's an oh-so-awesome realization... I love programming... thanks mike!!!! your engine is awesome!!

I was right: the physics simulation was working, but the invisible collisionShapes were not attached to the models, so, if they were falling, I couldn't see it.

I had to sync the model's position with the RigidBodies centerOfMassPosition in the update method, and that was it!

Right now I'm very, very tired, but tomorrow I'm gonna clean up the code. If anyone wants to know how I did it, let me know and I'll post it! Wink

thanks again! and happy coding! Cheesy
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #3 on: 2007/06/27 00:35:52 »

Post it any way, thats what I do, regardless of whether people want it or not  Tongue
Logged
Yubastard
Newbie
*
Offline Offline

Posts: 14


View Profile
« Reply #4 on: 2007/06/30 08:33:05 »

Post it any way, thats what I do, regardless of whether people want it or not  Tongue

well, that's pretty nice of you Smiley

I plan on doing the same but I'm still taming the beast hehe I'm trying to sync the model's rotation, too... ( I hate Quaternions, I just can't visualize a 4 dimensional figure, there should be Quaternions for Dummies hehe)

I will post everything in detail when I get it to work properly
Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #5 on: 2007/08/03 19:20:00 »

OMG! I was sooo going to steal the Dev.ru guys' physcis too Wink Once you get it up and running and to your happy standard let me know and I'll add bounding boxes to the content importer so we can combine everything together Smiley This would make for a great tutorial addition too
Logged
Yubastard
Newbie
*
Offline Offline

Posts: 14


View Profile
« Reply #6 on: 2007/08/12 08:57:21 »

well... after a massive hard-drive crash, and subsequent loss of all data (code, models, shaders, assets, etc.) caused by my father's utter lack of patience while using a computer, I'm at it again rebuilding the engine...

it's a nice thing I wrote the Physics Manager class up in here, I'll copy and paste it. Smiley over the course of the week I shall have everything set up and running... again  :Smiley

I'm glad you also chose the Dev.ru guys' physics! If you chose it, it means I'm on the right track...

if I remember well, I was having problems with the quaternion rotations of the models and physics shapes, they were orbiting instead of rotating (or rolling), but I'll paste everything here! Smiley thanks for everything mike, u rock.
Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Hazy Mind 3D Engine Hazy Mind 3D Engine mikeschuld 0 2174 Last post 2006/07/22 15:02:05
by mikeschuld
Hazy Mind 3D Engine on the December 2006 DirectX SDK Hazy Mind 3D Engine edgaronfo 4 2296 Last post 2007/02/12 07:41:23
by precious roy
Hazy Mind 3D Engine tutorials Hazy Mind 3D Engine justus 1 2003 Last post 2007/02/13 08:19:03
by ericc59
why is the Hazy Mind engine designed like this? General Discussion dmf 3 2006 Last post 2007/02/16 09:10:48
by DaphydTheBard
Physics Architecture Hazy Mind XNA Engine Jonotron 0 1081 Last post 2007/04/15 09:11:12
by Jonotron
Making bullet holes... Hazy Mind XNA Engine EclipsE 8 3151 Last post 2007/08/14 21:17:58
by mikeschuld
XNA Animation Component Library in XNA HMEngine Hazy Mind XNA Engine dotslash 0 2621 Last post 2007/05/31 13:53:12
by dotslash
3D Collision Detection and Physics General Discussion « 1 2 » LucianX 15 4651 Last post 2007/08/02 20:54:38
by mikeschuld
Creating and Rendering a Planet (XNA HM Engine) General Discussion Suzume 7 6190 Last post 2007/09/02 08:48:14
by Suzume
Hazy Mind 3D Engine Hazy Mind 3D Engine precious roy 2 4027 Last post 2007/08/28 06:15:22
by precious roy
Running Hazy Mind engine on another machine Hazy Mind XNA Engine Silvo 2 1789 Last post 2008/04/12 22:34:29
by Silvo
XNA ENgine sources/pdf General Discussion Maverick 3 2656 Last post 2008/04/17 01:59:11
by Maverick
XNA 3.0 CTP General Discussion Chr0n1x 0 2036 Last post 2008/05/08 16:14:46
by Chr0n1x
Hazy Mind Engine - Re Born General Discussion Nemo Krad 5 2554 Last post 2008/12/20 10:28:38
by mikeschuld
Please write a tutorial: XNA+WinForm in Hazy Mind General Discussion Francisk 4 3275 Last post 2009/12/14 11:45:40
by Mikeske
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 1.122 seconds with 19 queries.