Drop weapon

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
dirtysteve
Posts: 7
Joined: Thu Sep 22, 2011 2:58 pm

Drop weapon

Post by dirtysteve »

I've been using this code for my characters' weapons.

Code: Select all

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using SynapseGaming.LightingSystem.Core;
using SynapseGaming.LightingSystem.Rendering;
using SgMotion;
using SgMotion.Controllers;
using SgMotion.Effects;

namespace Aggro.Clutter
{
    public class Weapon
    {
        public Model model;       
        public SceneObject weaponObject;
        public SceneObject dropObject;

        public bool held = true;
        private Matrix lastBonePos = Matrix.Identity;       

        public Vector3 Position
        {
            get { return weaponObject.World.Translation; }
            set { weaponObject.World = Matrix.CreateTranslation(value); }
        }

        public Weapon(ContentManager gameContent, string modelFile, Matrix bonePos)
        {
            LoadContent(gameContent, modelFile);
        }

        public void LoadContent(ContentManager gameContent, string modelFile)
        {
            model = gameContent.Load<Model>(modelFile);             
           
            weaponObject = new SceneObject(model);
            weaponObject.UpdateType = UpdateType.Automatic;
            weaponObject.Visibility = ObjectVisibility.RenderedAndCastShadows;
            weaponObject.StaticLightingType = StaticLightingType.Composite;
            weaponObject.CollisionType = SynapseGaming.LightingSystem.Collision.CollisionType.Collide;
            weaponObject.HullType = HullType.Mesh;
            weaponObject.Mass = 10;
           
            weaponObject.AffectedByGravity = true;
            SceneInterface.ActiveSceneInterface.ObjectManager.Submit(weaponObject);
            weaponObject.CalculateBounds();
        }
        public void Update(GameTime gametime, Matrix bonePos, Matrix offset)
        {
            weaponObject.World = bonePos;
        }
    }
}

Would anyone here know a quick way to convert it to a physics object, so that when and event triggers (character is hit), the weapon is dropped to the ground?
I use a bepu character controller, but it's a bit too complicated for a physics noob like me to convert for this purpose.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Drop weapon

Post by Norbo »

I'm not too familiar with Sunburn's API, but I'll try to give a rough transliteration (or at least some pointers).

The character's body is represented by an Entity. An entity is a physical object in the simulation capable of motion. Some entities are dynamic, others are kinematic. Dynamic entities respond to collisions and have finite mass. Kinematic entities do not respond to collisions and act like they have infinite mass/inertia- an unstoppable force, in other words. The physical representation of the weapon would be another Entity. A Box would do the trick.

It would probably be a good idea to ensure the weapon entity doesn't collide with the character as it's falling, since it will probably spawn inside of the character. You can do this using collision rules. Doing something like this when setting up the weapon entity and character initially would work:

Code: Select all

CollisionRules.AddRule(weaponBox, character.Body, CollisionRule.NoBroadPhase);
You could also use more advanced concepts like collision groups if it is more convenient.

To assign a collision event that fires when the character's body gets hit by another entity or collidable object, add an event to the character.Body.CollisionInformation.Events.InitialCollisionDetected event or one of the many others. An overview of the available events can be found here.

Inside that event, you could do something like add the Box representing the weapon to the space and set its position/orientation to the animated character model's hand. You could also compute the velocity of the mesh's hand (from a previous state and the time step, if available) and apply it to the entity too.
Post Reply