internal static PhysicsProperties DeserializeOrCreateNew(PhysxScene scene, IMaterial fallbackMaterial, byte[] serializedProperties) { PhysicsProperties properties = null; if (serializedProperties != null) { try { using (System.IO.MemoryStream ms = new System.IO.MemoryStream(serializedProperties)) { properties = ProtoBuf.Serializer.Deserialize <PhysicsProperties>(ms); } properties.FillMaterialFromDesc(scene.SceneImpl.Physics); return(properties); } catch (Exception e) { //unable to deserialize physics properties, fallthrough m_log.ErrorFormat("[InWorldz.PhysxX] PhysicsProperties.DeserializeOrCreateNew: Deserialization failed, falling back to defaults: {0}", e); } } properties = new PhysicsProperties(); properties.Material = fallbackMaterial; return(properties); }
private static void SetCommonProperties(PhysxScene scene, PhysicsShape shape, OpenMetaverse.Vector3 position, OpenMetaverse.Quaternion rotation, PhysX.RigidActor physActor, bool physical, Material material) { shape.AssignToActor(physActor, material.PhyMaterial, physical); physActor.GlobalPose = PhysX.Math.Matrix.RotationQuaternion(new PhysX.Math.Quaternion(rotation.X, rotation.Y, rotation.Z, rotation.W)) * PhysX.Math.Matrix.Translation(position.X, position.Y, position.Z); }
/// <summary> /// Creates a new PhysX.RigidStatic using our defaults /// </summary> /// <param name="shape"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <returns></returns> public static PhysX.RigidStatic CreateRigidStatic(PhysxScene scene, PhysicsShape shape, OpenMetaverse.Vector3 position, OpenMetaverse.Quaternion rotation, Material material) { PhysX.RigidStatic physActor = scene.SceneImpl.Physics.CreateRigidStatic(); SetCommonProperties(scene, shape, position, rotation, physActor, false, material); return(physActor); }
public PhysicsScene GetScene(string sceneIdentifier) { //no lock needed, only called during an init PhysicsScene scene; if (_scenes.TryGetValue(sceneIdentifier, out scene)) { return(scene); } else { scene = new PhysxScene(); _scenes.Add(sceneIdentifier, scene); return(scene); } }
/// <summary> /// Creates a new PhysX.RigidDynamic using our defaults /// </summary> /// <param name="shape"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <returns></returns> public static PhysX.RigidDynamic CreateRigidDynamic(PhysxScene scene, PhysicsShape shape, OpenMetaverse.Vector3 position, OpenMetaverse.Quaternion rotation, bool physical, bool kinematic, Material material) { PhysX.RigidDynamic physActor = scene.SceneImpl.Physics.CreateRigidDynamic(); if (kinematic) physActor.Flags |= PhysX.RigidDynamicFlags.Kinematic; SetCommonProperties(scene, shape, position, rotation, physActor, physical, material); if (physical) { physActor.UpdateMassAndInertia(material.Density); physActor.MaxAngularVelocity *= 4; physActor.AngularDamping = DEFAULT_ANGULAR_DAMPING; physActor.LinearDamping = DEFAULT_LINEAR_DAMPING; } physActor.SleepThreshold = SLEEP_THRESHOLD; return physActor; }
/// <summary> /// Creates a new PhysX.RigidDynamic using our defaults /// </summary> /// <param name="shape"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <returns></returns> public static PhysX.RigidDynamic CreateRigidDynamic(PhysxScene scene, PhysicsShape shape, OpenMetaverse.Vector3 position, OpenMetaverse.Quaternion rotation, bool physical, bool kinematic, Material material) { PhysX.RigidDynamic physActor = scene.SceneImpl.Physics.CreateRigidDynamic(); if (kinematic) { physActor.Flags |= PhysX.RigidDynamicFlags.Kinematic; } SetCommonProperties(scene, shape, position, rotation, physActor, physical, material); if (physical) { physActor.UpdateMassAndInertia(material.Density); physActor.MaxAngularVelocity *= 4; physActor.AngularDamping = DEFAULT_ANGULAR_DAMPING; physActor.LinearDamping = DEFAULT_LINEAR_DAMPING; } physActor.SleepThreshold = SLEEP_THRESHOLD; return(physActor); }
private void GetRayCastResults(OpenMetaverse.Vector3 start, OpenMetaverse.Vector3 direction, float distance, int hitAmounts, Action<List<ContactResult>> result, PhysxScene scene) { int buffercount = 16; int maxbuffercount = 1024; PhysX.RaycastHit[] hits = null; direction = OpenMetaverse.Vector3.Normalize(direction); //Increase the buffer count if the call indicates overflow. Prevent infinite loops. while (hits == null && buffercount <= maxbuffercount) { hits = SceneImpl.RaycastMultiple(PhysUtil.OmvVectorToPhysx(start), PhysUtil.OmvVectorToPhysx(direction), distance, PhysX.SceneQueryFlags.All, buffercount, null); buffercount *= 2; } List<ContactResult> contactResults = new List<ContactResult>(); if (hits != null) { List<PhysX.RaycastHit> hitsSorted = new List<PhysX.RaycastHit>(hits); hitsSorted.Sort((a, b) => a.Distance.CompareTo(b.Distance)); int count = 0; foreach (PhysX.RaycastHit hit in hitsSorted) { contactResults.Add(new ContactResult() { Distance = hit.Distance, FaceIndex = hit.FaceIndex, CollisionActor = hit.Shape.Actor.UserData as PhysicsActor, Position = PhysUtil.PhysxVectorToOmv(hit.Impact), Normal = PhysUtil.PhysxVectorToOmv(hit.Normal), }); if (++count >= hitAmounts) break; } } result(contactResults); }
private void GetRayCastResults(OpenMetaverse.Vector3 start, OpenMetaverse.Vector3 direction, float distance, int hitAmounts, Action <List <ContactResult> > result, PhysxScene scene) { int buffercount = 16; int maxbuffercount = 1024; PhysX.RaycastHit[] hits = null; direction = OpenMetaverse.Vector3.Normalize(direction); //Increase the buffer count if the call indicates overflow. Prevent infinite loops. while (hits == null && buffercount <= maxbuffercount) { hits = SceneImpl.RaycastMultiple(PhysUtil.OmvVectorToPhysx(start), PhysUtil.OmvVectorToPhysx(direction), distance, PhysX.SceneQueryFlags.All, buffercount, null); buffercount *= 2; } List <ContactResult> contactResults = new List <ContactResult>(); if (hits != null) { List <PhysX.RaycastHit> hitsSorted = new List <PhysX.RaycastHit>(hits); hitsSorted.Sort((a, b) => a.Distance.CompareTo(b.Distance)); int count = 0; foreach (PhysX.RaycastHit hit in hitsSorted) { contactResults.Add(new ContactResult() { Distance = hit.Distance, FaceIndex = hit.FaceIndex, CollisionActor = hit.Shape.Actor.UserData as PhysicsActor, Position = PhysUtil.PhysxVectorToOmv(hit.Impact), Normal = PhysUtil.PhysxVectorToOmv(hit.Normal), }); if (++count >= hitAmounts) { break; } } } result(contactResults); }
public PhysxCharacter(PhysxScene scene, float height, float radius, OpenMetaverse.Vector3 position, OpenMetaverse.Quaternion rotation, bool flying, OpenMetaverse.Vector3 initialVelocity) { _scene = scene; _radius = Math.Max(radius, 0.2f); /* * The capsule is defined as a position, a vertical height, and a radius. The height is the distance between the * two sphere centers at the end of the capsule. In other words: * * p = pos (returned by controller) * h = height * r = radius * * p = center of capsule * top sphere center = p.y + h*0.5 * bottom sphere center = p.y - h*0.5 * top capsule point = p.y + h*0.5 + r * bottom capsule point = p.y - h*0.5 - r */ _height = height; _flying = flying; float volume = (float)(Math.PI * Math.Pow(_radius, 2) * this.CapsuleHeight); _mass = CHARACTER_DENSITY * volume; _position = position; _rotation = rotation; _hitReportDelegator = new UserControllerHitReportDelegator(); _hitReportDelegator.OnShapeHitCallback += this.OnShapeHit; _hitReportDelegator.OnControllerHitCallback += this.OnControllerHit; PhysX.CapsuleControllerDesc controllerDesc = new PhysX.CapsuleControllerDesc { Height = this.CapsuleHeight, Radius = _radius, StepOffset = STEP_OFFSET, UpDirection = new PhysX.Math.Vector3(0.0f, 0.0f, 1.0f), Position = PhysUtil.OmvVectorToPhysx(position), Material = scene.DEFAULT_MATERIAL, InteractionMode = PhysX.CCTInteractionMode.Include, SlopeLimit = (float)Math.Cos(OpenMetaverse.Utils.DEG_TO_RAD * MAX_WALKABLE_SLOPE), ContactOffset = CONTACT_OFFSET, Callback = _hitReportDelegator, BehaviorCallback = _rideOnBehavior }; _controller = _scene.ControllerManager.CreateController<PhysX.CapsuleController>(controllerDesc); _controller.Actor.UserData = this; DoZDepenetration(); _controller.ShapeFilterData = CollisionGroup.GetFilterData((uint)(PhysX.PairFlag.NotifyTouchFound | PhysX.PairFlag.NotifyTouchLost), 0, CollisionGroupFlag.Character); _lastSync = (uint)Environment.TickCount; _vTarget = initialVelocity; _velocity = initialVelocity; if (_vTarget != OpenMetaverse.Vector3.Zero) { //hack to continue at velocity until the controller picks up _lastVelocityNonZero = OpenSim.Framework.Util.GetLongTickCount() - VELOCITY_RAMPUP_TIME; } }
public PhysxPrim(PhysxPrim parent, PhysxScene scene, PrimitiveBaseShape baseShape, OpenMetaverse.Vector3 pos, OpenMetaverse.Quaternion rotation, PhysicsShape myShape, PhysX.RigidActor myActor, bool isPhysical, IPhysicsProperties properties, CollisionGroupFlag collisionGroup) { _parentPrim = parent; _scene = scene; _pbs = baseShape; _position = pos; _rotation = rotation; _isPhysical = isPhysical; _properties = (PhysicsProperties)properties; _collisionGroup = collisionGroup; this.AssignActor(myActor, myShape, _isPhysical, DeleteActorFlags.None); if (_properties.VehicleProps != null && _properties.VehicleProps.Type != VehicleType.None) { //init dynamics CheckCreateVehicleDynamics(); } }
public PhysxPrim(PhysxScene scene, PrimitiveBaseShape baseShape, OpenMetaverse.Vector3 pos, OpenMetaverse.Quaternion rotation, PhysicsShape myShape, PhysX.RigidActor myActor, bool isPhysical, IPhysicsProperties properties, CollisionGroupFlag collisionGroup) : this(null, scene, baseShape, pos, rotation, myShape, myActor, isPhysical, properties, collisionGroup) { }
public static PhysX.RigidActor CreateProperInitialActor(PhysicsShape meshedShape, PhysxScene scene, OpenMetaverse.Vector3 pos, OpenMetaverse.Quaternion rotation, PhysicsScene.AddPrimShapeFlags flags, out bool kinematicStatic, Material material) { bool isPhysical = (flags & PhysicsScene.AddPrimShapeFlags.Physical) != 0; kinematicStatic = false; PhysX.RigidActor actor; if (isPhysical) { actor = PhysxActorFactory.CreateRigidDynamic(scene, meshedShape, pos, rotation, isPhysical, kinematicStatic, material); } else { if ((flags & PhysicsScene.AddPrimShapeFlags.FromSceneStartup) != 0) { actor = PhysxActorFactory.CreateRigidStatic(scene, meshedShape, pos, rotation, material); } else { kinematicStatic = true; actor = PhysxActorFactory.CreateRigidDynamic(scene, meshedShape, pos, rotation, isPhysical, kinematicStatic, material); } } return(actor); }
internal static PhysicsProperties DeserializeOrCreateNew(PhysxScene scene, IMaterial fallbackMaterial, byte[] serializedProperties) { PhysicsProperties properties = null; if (serializedProperties != null) { try { using (System.IO.MemoryStream ms = new System.IO.MemoryStream(serializedProperties)) { properties = ProtoBuf.Serializer.Deserialize<PhysicsProperties>(ms); } properties.FillMaterialFromDesc(scene.SceneImpl.Physics); return properties; } catch (Exception e) { //unable to deserialize physics properties, fallthrough m_log.ErrorFormat("[InWorldz.PhysxX] PhysicsProperties.DeserializeOrCreateNew: Deserialization failed, falling back to defaults: {0}", e); } } properties = new PhysicsProperties(); properties.Material = fallbackMaterial; return properties; }
private static void SetCommonProperties(PhysxScene scene, PhysicsShape shape, OpenMetaverse.Vector3 position, OpenMetaverse.Quaternion rotation, PhysX.RigidActor physActor, bool physical, Material material) { shape.AssignToActor(physActor, material.PhyMaterial, physical); physActor.GlobalPose = PhysX.Math.Matrix.RotationQuaternion(new PhysX.Math.Quaternion(rotation.X, rotation.Y, rotation.Z, rotation.W)) * PhysX.Math.Matrix.Translation(position.X, position.Y, position.Z); }
/// <summary> /// Creates a new PhysX.RigidStatic using our defaults /// </summary> /// <param name="shape"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <returns></returns> public static PhysX.RigidStatic CreateRigidStatic(PhysxScene scene, PhysicsShape shape, OpenMetaverse.Vector3 position, OpenMetaverse.Quaternion rotation, Material material) { PhysX.RigidStatic physActor = scene.SceneImpl.Physics.CreateRigidStatic(); SetCommonProperties(scene, shape, position, rotation, physActor, false, material); return physActor; }
public static PhysX.RigidActor CreateProperInitialActor(PhysicsShape meshedShape, PhysxScene scene, OpenMetaverse.Vector3 pos, OpenMetaverse.Quaternion rotation, PhysicsScene.AddPrimShapeFlags flags, out bool kinematicStatic, Material material) { bool isPhysical = (flags & PhysicsScene.AddPrimShapeFlags.Physical) != 0; kinematicStatic = false; PhysX.RigidActor actor; if (isPhysical) { actor = PhysxActorFactory.CreateRigidDynamic(scene, meshedShape, pos, rotation, isPhysical, kinematicStatic, material); } else { if ((flags & PhysicsScene.AddPrimShapeFlags.FromSceneStartup) != 0) { actor = PhysxActorFactory.CreateRigidStatic(scene, meshedShape, pos, rotation, material); } else { kinematicStatic = true; actor = PhysxActorFactory.CreateRigidDynamic(scene, meshedShape, pos, rotation, isPhysical, kinematicStatic, material); } } return actor; }