Ejemplo n.º 1
0
        public void SetAngularVelocity(IPhysicsObject physObj, Vector3 velocity)
        {
            if (!objectIDs.ContainsKey(physObj))
            {
                return;
            }

            HavokDllBridge.set_angular_velocity(objectIDs[physObj], Vector3Helper.ToFloats(velocity));
        }
Ejemplo n.º 2
0
        public void AddTorque(IPhysicsObject physObj, float timeStep, Vector3 torque)
        {
            if (!objectIDs.ContainsKey(physObj))
            {
                return;
            }

            HavokDllBridge.add_torque(objectIDs[physObj], timeStep, Vector3Helper.ToFloats(torque));
        }
Ejemplo n.º 3
0
        public void InitializePhysics()
        {
            Vector3 g = info.Gravity * info.GravityDirection;

            if (!HavokDllBridge.init_world(Vector3Helper.ToFloats(g), info.WorldSize,
                                           info.CollisionTolerance, info.HavokSimulationType, info.HavokSolverType,
                                           info.FireCollisionCallbacks, info.EnableDeactivation))
            {
                throw new GoblinException("Failed to initialize Havok physics");
            }
        }
Ejemplo n.º 4
0
        public void ApplyHardKeyFrame(IPhysicsObject physObj, Vector3 newPos, Quaternion newRot, float timeStep)
        {
            if (!objectIDs.ContainsKey(physObj))
            {
                return;
            }

            float[] pos = Vector3Helper.ToFloats(ref newPos);
            float[] rot = { newRot.X, newRot.Y, newRot.Z, newRot.W };

            HavokDllBridge.apply_hard_keyframe(objectIDs[physObj], pos, rot, timeStep);
        }
Ejemplo n.º 5
0
        public void StopKeyframe(IPhysicsObject physObj)
        {
            if (!objectIDs.ContainsKey(physObj))
            {
                return;
            }

            Vector3    scale, trans;
            Quaternion quat;

            physObj.PhysicsWorldTransform.Decompose(out scale, out quat, out trans);

            float[] pos = Vector3Helper.ToFloats(ref trans);
            float[] rot = { quat.X, quat.Y, quat.Z, quat.W };

            HavokDllBridge.apply_hard_keyframe(objectIDs[physObj], pos, rot, 0.016f);
        }
Ejemplo n.º 6
0
        public void ApplySoftKeyFrame(IPhysicsObject physObj, Vector3 newPos, Quaternion newRot,
                                      Vector3 angularPositionFactor, Vector3 angularVelocityFactor, Vector3 linearPositionFactor,
                                      Vector3 linearVelocityFactor, float maxAngularAcceleration, float maxLinearAcceleration,
                                      float maxAllowedDistance, float timeStep)
        {
            if (!objectIDs.ContainsKey(physObj))
            {
                return;
            }

            float[] pos           = Vector3Helper.ToFloats(ref newPos);
            float[] rot           = { newRot.X, newRot.Y, newRot.Z, newRot.W };
            float[] angularPosFac = Vector3Helper.ToFloats(ref angularPositionFactor);
            float[] angularVelFac = Vector3Helper.ToFloats(ref angularVelocityFactor);
            float[] linearPosFac  = Vector3Helper.ToFloats(ref linearPositionFactor);
            float[] linearVelFac  = Vector3Helper.ToFloats(ref linearVelocityFactor);

            HavokDllBridge.apply_soft_keyframe(objectIDs[physObj], pos, rot, angularPosFac,
                                               angularVelFac, linearPosFac, linearVelFac, maxAngularAcceleration,
                                               maxLinearAcceleration, maxAllowedDistance, timeStep);
        }
Ejemplo n.º 7
0
        public void AddPhysicsObject(IPhysicsObject physObj)
        {
            if (objectIDs.ContainsKey(physObj))
            {
                return;
            }

            physObj.PhysicsWorldTransform = physObj.CompoundInitialWorldTransform;

            HavokPhysics.MotionType            motionType  = MotionType.MOTION_INVALID;
            HavokPhysics.CollidableQualityType qualityType =
                CollidableQualityType.COLLIDABLE_QUALITY_INVALID;
            float friction                = -1;
            float restitution             = -1;
            float maxLinearVelocity       = -1;
            float maxAngularVelocity      = -1;
            float allowedPenetrationDepth = -1;
            float gravityFactor           = 1;

            if ((physObj is HavokObject))
            {
                HavokObject havokObj = (HavokObject)physObj;
                motionType              = havokObj.MotionType;
                qualityType             = havokObj.QualityType;
                friction                = havokObj.Friction;
                restitution             = havokObj.Restitution;
                maxLinearVelocity       = havokObj.MaxLinearVelocity;
                maxAngularVelocity      = havokObj.MaxAngularVelocity;
                allowedPenetrationDepth = havokObj.AllowedPenetrationDepth;
                gravityFactor           = havokObj.GravityFactor;
            }
            else
            {
                bool isDynamic = (physObj.Mass != 0.0f && physObj.Interactable);
                if (isDynamic)
                {
                    motionType = MotionType.MOTION_DYNAMIC;
                }
                else
                {
                    motionType = MotionType.MOTION_FIXED;
                }
            }

            Quaternion rotation;
            Vector3    trans;
            Vector3    scale;

            physObj.CompoundInitialWorldTransform.Decompose(out scale, out rotation, out trans);

            IntPtr shape = GetCollisionShape(physObj, scale);

            float[] pos = Vector3Helper.ToFloats(ref trans);
            float[] rot = { rotation.X, rotation.Y, rotation.Z, rotation.W };

            IntPtr body = HavokDllBridge.add_rigid_body(shape, physObj.Mass, motionType, qualityType,
                                                        pos, rot, Vector3Helper.ToFloats(physObj.InitialLinearVelocity), physObj.LinearDamping,
                                                        maxLinearVelocity, Vector3Helper.ToFloats(physObj.InitialAngularVelocity),
                                                        physObj.AngularDamping.X, maxAngularVelocity, friction, restitution,
                                                        allowedPenetrationDepth, physObj.NeverDeactivate, gravityFactor);

            objectIDs.Add(physObj, body);
            reverseIDs.Add(body, physObj);
            scaleTable.Add(body, scale);

            if ((physObj is HavokObject))
            {
                if (((HavokObject)physObj).ContactCallback != null ||
                    ((HavokObject)physObj).CollisionStartCallback != null ||
                    ((HavokObject)physObj).CollisionEndCallback != null)
                {
                    HavokDllBridge.add_contact_listener(body, ((HavokObject)physObj).ContactCallback,
                                                        ((HavokObject)physObj).CollisionStartCallback,
                                                        ((HavokObject)physObj).CollisionEndCallback);
                }
            }
        }
Ejemplo n.º 8
0
        public RaceCar(Object container, NewtonPhysics engine) : base(container)
        {
            this.engine = engine;

            //Crea los cuatro nodos de transformacion para las 4 Ruedas
            tireTransNode = new TransformNode[4];
            for (int i = 0; i < 4; i++)
            {
                tireTransNode[i] = new TransformNode();
            }

            // set the mass of the race car - Propiedad heredada
            mass = VEHICLE_MASS;

            // lower the center of the mass for a race car
            centerOfMass = -Vector3.UnitY * 1.5f;

            // sets up the callback function when body moves in the simulation
            transformCallback = delegate(IntPtr body, float[] matrix)
            {
                Matrix mat = MatrixHelper.FloatsToMatrix(matrix);

                // set the transformation of the vehicle body
                PhysicsWorldTransform = mat;

                Matrix     invMat        = Matrix.Invert(mat);
                float[]    tireMatrix    = new float[16];
                NewtonTire tire          = null;
                float      sign          = 0;
                float      angle         = 0;
                float      brakePosition = 0;

                // set the global matrix for each tire
                for (IntPtr tyreId = Newton.NewtonVehicleGetFirstTireID(joint);
                     tyreId != IntPtr.Zero; tyreId = Newton.NewtonVehicleGetNextTireID(joint, tyreId))
                {
                    int tireID = (int)GetTireID(tyreId);
                    tire = tires[tireID];
                    Newton.NewtonVehicleGetTireMatrix(joint, tyreId, tireMatrix);

                    // calculate the local matrix
                    Matrix tireMat = MatrixHelper.GetRotationMatrix(
                        MatrixHelper.FloatsToMatrix(tireMatrix) * invMat) * tire.TireOffsetMatrix;
                    tire.TireMatrix = tireMat;
                    tireTransNode[tireID].WorldTransformation = Matrix.CreateRotationX(MathHelper.PiOver2)
                                                                * tireMat;

                    // calcualte the parametric brake position
                    brakePosition    = tireMat.Translation.Y - tire.TireRefHeight;
                    tire.BrakeMatrix = Matrix.CreateTranslation(0, tire.BrakeRefPosition.Y + brakePosition, 0);

                    // set suspensionMatrix
                    sign  = (tire.BrakeRefPosition.Z > 0) ? 1 : -1;
                    angle = (float)Math.Atan2(sign * brakePosition, Math.Abs(tire.BrakeRefPosition.Z));
                    Matrix rotationMatrix = new Matrix(
                        1, 0, 0, 0,
                        0, (float)Math.Cos(angle), (float)Math.Sin(angle), 0,
                        0, -(float)Math.Sin(angle), (float)Math.Cos(angle), 0,
                        0, 0, 0, 1);
                    tire.AxelMatrix             = rotationMatrix * tire.AxelMatrix;
                    tire.SuspensionTopMatrix    = rotationMatrix * tire.SuspensionTopMatrix;
                    tire.SuspensionBottomMatrix = rotationMatrix * tire.SuspensionBottomMatrix;
                }
            };

            forceCallback = delegate(IntPtr body)
            {
                float Ixx = 0, Iyy = 0, Izz = 0, tmpMass = 0;

                Newton.NewtonBodyGetMassMatrix(body, ref tmpMass, ref Ixx, ref Iyy, ref Izz);
                tmpMass *= (1.0f + (float)Math.Abs(GetSpeed()) / 20.0f);
                float[] force = Vector3Helper.ToFloats(engine.GravityDirection * engine.Gravity * tmpMass);

                Newton.NewtonBodySetForce(body, force);
            };

            tireUpdate = delegate(IntPtr vehicleJoint)
            {
                NewtonTire tire = null;

                for (IntPtr tyreId = Newton.NewtonVehicleGetFirstTireID(vehicleJoint);
                     tyreId != IntPtr.Zero; tyreId = Newton.NewtonVehicleGetNextTireID(vehicleJoint, tyreId))
                {
                    tire = tires[(int)GetTireID(tyreId)];

                    // If the tire is a front tire
                    if ((tire == tires[(int)TireID.FrontLeft]) || (tire == tires[(int)TireID.FrontRight]))
                    {
                        float currSteerAngle = Newton.NewtonVehicleGetTireSteerAngle(vehicleJoint, tyreId);
                        Newton.NewtonVehicleSetTireSteerAngle(vehicleJoint, tyreId,
                                                              currSteerAngle + (tire.Steer - currSteerAngle) * 0.035f);
                    }
                    else // if the tire is a rear tire
                    {
                        Newton.NewtonVehicleSetTireTorque(vehicleJoint, tyreId, tire.Torque);

                        if (tire.Brakes > 0)
                        {
                            // ask Newton for the precise acceleration needed to stop the tire
                            float brakeAcceleration =
                                Newton.NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicleJoint, tyreId);

                            // tell Newton you want this tire stoped but only if the torque need it is less than
                            // the brakes pad can withstand (assume max brake pad torque is 500 newton * meter)
                            Newton.NewtonVehicleTireSetBrakeAcceleration(vehicleJoint, tyreId,
                                                                         brakeAcceleration, 10000.0f);

                            // set some side slip as function of the linear speed
                            float speed = Newton.NewtonVehicleGetTireLongitudinalSpeed(vehicleJoint, tyreId);
                            Newton.NewtonVehicleSetTireMaxSideSleepSpeed(vehicleJoint, tyreId, speed * 0.1f);
                        }
                    }
                }
            };

            bodyLeaveCallback = delegate(IntPtr body)
            {
                Respawn(body);
            };
        }