public VisualProjectileAction(VisualEntity source, VisualEntity target, int amount, Color color) { Source = source; Target = target; Amount = amount; Color = color; }
public VA_SummonPlayer(VisualEntity visual, Color color) { Frames = DefaultFrames; Visual = visual; Visual.GetComponent <Renderer>().material.color = color; Visual.transform.localScale = new Vector3(0, 0, 0); }
private static bool IsSlopeCollisionDetection(VerletSimulator physicSimu, VisualEntity entityTesting, ref BoundingBox playerBoundingBox, ref BoundingBox playerBoundingBox2Evaluate, ref Vector3D newPosition2Evaluate, ref Vector3D previousPosition, ItemOrientation slopeOrientation, bool onSlidingSlope, out bool isSliding) { isSliding = false; Vector3 entityPosition = newPosition2Evaluate.AsVector3(); float posi = 0.0f; float L = 0.0f; float H = entityTesting.WorldBBox.Maximum.Y - entityTesting.WorldBBox.Minimum.Y; switch (slopeOrientation) { case ItemOrientation.North: L = entityTesting.WorldBBox.Maximum.Z - entityTesting.WorldBBox.Minimum.Z; posi = (entityPosition.Z + playerBoundingBox.Maximum.Z) - entityTesting.WorldBBox.Minimum.Z; break; case ItemOrientation.South: L = entityTesting.WorldBBox.Maximum.Z - entityTesting.WorldBBox.Minimum.Z; posi = entityTesting.WorldBBox.Maximum.Z - (entityPosition.Z + playerBoundingBox.Minimum.Z); break; case ItemOrientation.East: L = entityTesting.WorldBBox.Maximum.X - entityTesting.WorldBBox.Minimum.X; posi = entityTesting.WorldBBox.Maximum.X - (entityPosition.X + playerBoundingBox.Minimum.X); break; case ItemOrientation.West: L = entityTesting.WorldBBox.Maximum.X - entityTesting.WorldBBox.Minimum.X; posi = (entityPosition.X + playerBoundingBox.Maximum.X) - entityTesting.WorldBBox.Minimum.X; break; default: break; } float posiOriginal = posi; posi = posi / L; float Y = posi * H; Y = Math.Min(Math.Max(Y, 0), 1); if (onSlidingSlope) { return(SlidingSlope(physicSimu, entityTesting, Y, ref newPosition2Evaluate, ref previousPosition, slopeOrientation, out isSliding)); } //Apply only if new Y is >= Current Y if (entityTesting.WorldBBox.Minimum.Y + Y > newPosition2Evaluate.Y) { return(NormalSlope(physicSimu, entityTesting, Y, ref newPosition2Evaluate, ref previousPosition)); } else { physicSimu.AllowJumping = true; return(false); } }
private void AddVisualEntity(VisualEntity entry) { if (CurrentFloor == null) { return; } CurrentFloor.AddVisualEntity(entry); }
public VA_DealDamage(VisualEntity source, VisualEntity target, int amount, Color color) : base(source, target, amount, color) { Frames = DefaultFrames; ProjectTileBaseSize = 0.05f; BobDurationRel = 0.5f; BobExtraScale = 1.5f; Init(); }
public VA_HealSelf(VisualEntity visual, int amount) { Frames = DefaultFrames * 1.5f; Visual = visual; SourceColor = Visual.GetComponent <Renderer>().material.color; BobExtraScale = 1 + ((float)amount / 10); BobSourceScale = Visual.transform.localScale; BobTargetScale = new Vector3(BobSourceScale.x * BobExtraScale, BobSourceScale.y * BobExtraScale, BobSourceScale.z * BobExtraScale); }
public VA_IncreaseCardOption(VisualEntity visual, MatchUI UI) { Frames = DefaultFrames * 1.5f; Visual = visual; ParticleSystem = GameObject.Instantiate(UI.PS_CardOptionIncrease, visual.transform.position, Quaternion.identity); var main = ParticleSystem.main; main.duration = Frames; ParticleSystem.Play(); }
private void MakeNameUnique(VisualEntity entity) { int append = 1; string baseName = entity.State.Name; while (SimulationEngine.GlobalInstance.IsEntityNameInUse(entity.State.Name)) { entity.State.Name = baseName + append; ++append; } }
VisualEntity FindConnectedEntity(string name, VisualEntity me) { // find the parent at the top of the hierarchy while (me.Parent != null) { me = me.Parent; } // now traverse the hierarchy looking for the name return(FindConnectedEntityHelper(name, me)); }
public float Speed; // Rate of moving toward the target position public JointDesc(string name, float min, float max, VisualEntity parentEntity, bool useNormal) { Name = name; Min = min; Max = max; _parentEntity = parentEntity; _useNormal = useNormal; Joint = null; Joint2 = null; Current = Target = 0; Speed = 30; }
static void Update() { if (Input.GetKeyPressed(KeyCode.C)) { fullscreen = !fullscreen; Application.SetFullScreen(fullscreen); } if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } if (Input.GetKeyPressed(KeyCode.E)) { StaticMesh m = new StaticMesh(Settings.ModelsDirPath + "\\Crate1.obj"); int i = Random.Range(0, 4); VisualEntity visual = new VisualEntity(m, mats[i]); visual.Transform.Position = Camera.Main.Position + Camera.Main.Forward * 5f; var bdy = phys.CreateRigidBody(visual.Transform.Position); bdy.AddCollisionShape(new BoxShape(1.1f)); visual.AddComponent(bdy); mWorld.AddEntity(visual); bdy.ApplyForceToCenterOfMass(Camera.Main.Forward * 5000f); shotSound.Play(); } float intensity = GetSliderValueUI_Native(); mWorld.skySettings.exposure = intensity * 32f; if (GetButtonPressedUI_Native() == 1) { for (int i = 0; i < visuals.Length; i++) { float power = 300f; if (GetRadioOptionUI_Native() == 1) { power = 800f; } visuals[i].GetComponent <RigidBody>().ApplyForceToCenterOfMass(Vector3.UnitY * power); } } }
private void FindTarget() { if (HasBeenInitialized) { var query = new VisualEntity(); query.State.Name = _targetName; Activate( Arbiter.Choice( SimulationEngine.GlobalInstancePort.Query(query), success => Target = success.Entity, CcrServiceBase.EmptyHandler ) ); } }
private static bool NormalSlope(VerletSimulator physicSimu, VisualEntity entityTesting, float Y, ref Vector3D newPosition2Evaluate, ref Vector3D previousPosition) { if (((entityTesting.WorldBBox.Minimum.Y + Y) - newPosition2Evaluate.Y) < 0.3f) { //Push up the player, and stabilize its physic simulation newPosition2Evaluate.Y = entityTesting.WorldBBox.Minimum.Y + Y; previousPosition.Y = newPosition2Evaluate.Y; physicSimu.OnGround = true; physicSimu.AllowJumping = true; return(false); } else { return(true); } }
public VA_SummonMinions(List <VisualEntity> visuals, VisualEntity source, List <Vector3> targetPositions, float targetScale) { Frames = DefaultFrames; Visuals = visuals; Source = source; TargetPositions = targetPositions; TargetScale = targetScale; foreach (VisualEntity vis in visuals) { vis.transform.localScale = new Vector3(0, 0, 0); vis.transform.position = Source.transform.position; } BobDuration = Frames * BobDurationRel; BobSourceScale = Source.transform.localScale; BobTargetScale = new Vector3(BobSourceScale.x * BobExtraScale, BobSourceScale.y * BobExtraScale, BobSourceScale.z * BobExtraScale); }
VisualEntity FindConnectedEntityHelper(string name, VisualEntity me) { if (me.State.Name == name) { return(me); } foreach (VisualEntity child in me.Children) { VisualEntity result = FindConnectedEntityHelper(name, child); if (result != null) { return(result); } } return(null); }
public VA_DestroyMinions(VisualEntity source, List <VisualEntity> targets, Color color) { Projectiles = new List <GameObject>(); Frames = DefaultFrames; Source = source; SourcePosition = source.transform.position; Targets = targets; TargetPositions = targets.Select(x => x.transform.position).ToList(); foreach (VisualEntity target in targets) { GameObject Projectile = GameObject.CreatePrimitive(PrimitiveType.Sphere); Projectile.GetComponent <Renderer>().material.color = color; Projectile.transform.position = SourcePosition; Projectile.transform.localScale = new Vector3(ProjectileSize, ProjectileSize, ProjectileSize); Projectiles.Add(Projectile); } BobDuration = Frames * BobDurationRel; BobSourceScale = Source.transform.localScale; BobTargetScale = new Vector3(BobSourceScale.x * BobExtraScale, BobSourceScale.y * BobExtraScale, BobSourceScale.z * BobExtraScale); }
private static void CollisionCheck(VerletSimulator physicSimu, VisualEntity entityTesting, ref BoundingBox entityBoundingBox, ref BoundingBox boundingBox2Evaluate, ref Vector3D newPosition2Evaluate, ref Vector3D previousPosition, out bool isSliding) { isSliding = false; if (entityTesting.WorldBBox.Intersects(ref boundingBox2Evaluate)) { if (entityTesting.Entity is Ladder) { physicSimu.isInContactWithLadder = true; return; } //Special treatment in case of an IOrientedSlope entity if (entityTesting.Entity is IOrientedSlope) { IOrientedSlope entity = (IOrientedSlope)entityTesting.Entity; if (entity.IsOrientedSlope) { SlopeCollisionDetection(physicSimu, entityTesting, ref entityBoundingBox, ref boundingBox2Evaluate, ref newPosition2Evaluate, ref previousPosition, entity.Orientation, entity.IsSlidingSlope, out isSliding); return; } } switch (entityTesting.Entity.CollisionType) { case Utopia.Shared.Entities.Entity.EntityCollisionType.BoundingBox: BoundingBoxCollision(physicSimu, entityTesting, ref entityBoundingBox, ref boundingBox2Evaluate, ref newPosition2Evaluate, ref previousPosition); break; case Utopia.Shared.Entities.Entity.EntityCollisionType.Model: ModelCollisionDetection(physicSimu, entityTesting, ref entityBoundingBox, ref boundingBox2Evaluate, ref newPosition2Evaluate, ref previousPosition); break; default: break; } } }
private static void SlopeCollisionDetection(VerletSimulator physicSimu, VisualEntity entityTesting, ref BoundingBox playerBoundingBox, ref BoundingBox playerBoundingBox2Evaluate, ref Vector3D newPosition2Evaluate, ref Vector3D previousPosition, ItemOrientation slopeOrientation, bool OnSlidingSlope, out bool isSliding) { if (IsSlopeCollisionDetection(physicSimu, entityTesting, ref playerBoundingBox, ref playerBoundingBox2Evaluate, ref newPosition2Evaluate, ref previousPosition, slopeOrientation, OnSlidingSlope, out isSliding)) { Vector3D newPositionWithColliding = previousPosition; newPositionWithColliding.X = newPosition2Evaluate.X; playerBoundingBox2Evaluate = new BoundingBox(playerBoundingBox.Minimum + newPositionWithColliding.AsVector3(), playerBoundingBox.Maximum + newPositionWithColliding.AsVector3()); if (entityTesting.WorldBBox.Intersects(ref playerBoundingBox2Evaluate)) { newPositionWithColliding.X = previousPosition.X; } newPositionWithColliding.Z = newPosition2Evaluate.Z; playerBoundingBox2Evaluate = new BoundingBox(playerBoundingBox.Minimum + newPositionWithColliding.AsVector3(), playerBoundingBox.Maximum + newPositionWithColliding.AsVector3()); if (entityTesting.WorldBBox.Intersects(ref playerBoundingBox2Evaluate)) { newPositionWithColliding.Z = previousPosition.Z; } newPosition2Evaluate = newPositionWithColliding; } }
private VisualEntity CreateEntity(Vector3 entityPosition, VisualEntity entityParent, Entities entityType) { VisualEntity e = null; #region Create Entity switch (entityType) { case Entities.HeightFieldEntity: { e = new HeightFieldEntity("ground", "03RamieSc.dds", new MaterialProperties("ground", 0.8f, 0.5f, 0.8f)); } break; case Entities.SkyDomeEntity: { e = new SkyDomeEntity("skydome.dds", "sky_diff.dds"); SimulationEngine.GlobalInstancePort.Insert(e); } break; case Entities.LightSourceEntity: { var lightEntity = new LightSourceEntity(); e = lightEntity; lightEntity.State.Name = "LightSourceEntity"; lightEntity.Type = LightSourceEntityType.Directional; lightEntity.Color = new Vector4(.8f, .8f, .8f, 1); lightEntity.State.Pose.Position = new Vector3(0, 1, 0); lightEntity.Direction = new Vector3(0, -1, 0); e.Flags = e.Flags | VisualEntityProperties.DisableRendering; } break; case Entities.MotorBaseWithDrive: { e = new MotorBaseWithDrive(entityPosition); e.State.Name = "MotorBaseWithDrive"; MakeNameUnique(e); } break; case Entities.SimulatedBrightnessSensorEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedBrightnessSensorEntity(32, 32, 2.0f * (float)Math.PI / 180.0f); e.State.Name = "SimulatedBrightnessSensorEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedColorSensorEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedColorSensorEntity(32, 32, 2.0f * (float)Math.PI / 180.0f); e.State.Name = "SimulatedColorSensorEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedCompassEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedCompassEntity(); e.State.Name = "SimulatedCompassEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedGPSEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedGPSEntity(); e.State.Name = "SimulatedGPSEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedIREntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedIREntity(new Pose(entityPosition)); e.State.Name = "SimulatedIREntity"; e.State.Flags |= EntitySimulationModifiers.DisableCollisions; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedLRFEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedLRFEntity(new Pose(entityPosition)); e.State.Name = "SimulatedLRFEntity"; e.State.Flags |= EntitySimulationModifiers.DisableCollisions; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedSonarEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedSonarEntity(new Pose(entityPosition)); e.State.Name = "SimulatedSonarEntity"; e.State.Flags |= EntitySimulationModifiers.DisableCollisions; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedWebcamEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedWebcamEntity(entityPosition, 320, 240, 45.0f * (float)Math.PI / 180.0f); e.State.Name = "SimulatedWebcamEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; } #endregion return(e); }
static void InitializeGame() { mats = new Material[] { Material.FromJSON(Settings.MaterialDirPath + "\\rustediron.mat.json"), Material.FromJSON(Settings.MaterialDirPath + "\\iron.mat.json"), Material.FromJSON(Settings.MaterialDirPath + "\\granite.mat.json"), Material.FromJSON(Settings.MaterialDirPath + "\\grass.mat.json"), Material.FromJSON(Settings.MaterialDirPath + "\\redplastic.mat.json") }; uint skyTex = Texture.LoadHDR(Settings.TextureDirPath + "\\sky2.hdr", true); uint skyEnv = Texture.LoadHDR(Settings.TextureDirPath + "\\skyenv2.hdr"); uint tex = Texture.FromColor(Color.FromHex("#7FFF00")); StaticMesh crate1 = new StaticMesh(Settings.ModelsDirPath + "\\Crate1.obj"); StaticMesh crate2 = new StaticMesh(Settings.ModelsDirPath + "\\Crate1.obj"); Material material = new Material("phong"); material.SetUniform(0, tex); Material material2 = new Material("phong"); material2.SetUniform(0, tex); Material material3 = new Material("phong"); material3.SetUniform(0, tex); Material material4 = Material.FromJSON(Settings.MaterialDirPath + "\\rustediron.mat.json"); Material material5 = Material.FromJSON(Settings.MaterialDirPath + "\\iron.mat.json"); VisualEntity crate1Ent = new VisualEntity(crate1, material4); VisualEntity crate2Ent = new VisualEntity(crate2, material5); sun = Entity.NewWithComponent(out DirectionalLight sunLight); sunLight.Direction = new Vector3(1f, 0.8f, 0.5f); var catTrans = crate1Ent.GetComponent <Transform>(); catTrans.Position += Vector3.UnitY * -10f + Vector3.UnitX * -5f; catTrans.Scale *= 1f; Entity camEnt = Entity.NewWithComponent(out PerspectiveCamera camera); camEnt.AddComponent <CameraController>(); Camera.Main = camera; camera.Position = new Vector3(0f, 0f, 20f); camera.FarPlane = 512f; mWorld = WorldManager.CreateWorld(); mWorld.skySettings.SetColor(Color.GRAY); mWorld.skySettings.gamma = 2.2f; mWorld.skySettings.exposure = 16f; mWorld.skySettings.ambient = 0.2f; mWorld.skySettings.hdrTex = skyTex; mWorld.skySettings.hdrEnv = skyEnv; phys = mWorld.PhysicsWorld; //phys.Gravity = new Vector3(0f, -2f, 0f); var crateBody = phys.CreateRigidBody(crate1Ent.Transform.Position); crateBody.AddCollisionShape(new BoxShape(1.1f)); crate1Ent.AddComponent(crateBody); var crate2Body = phys.CreateRigidBody(crate2Ent.Transform.Position); crate2Body.AddCollisionShape(new BoxShape(1.1f)); crate2Ent.AddComponent(crate2Body); mWorld.AddEntity(camEnt); mWorld.AddEntity(sun); mWorld.AddEntity(crate1Ent); mWorld.AddEntity(crate2Ent); // for now just keep the music in Models directory AudioClip clip = new AudioClip(Settings.ModelsDirPath + "\\ChillingMusic.wav"); Entity music = new Entity(); music.AddComponent(clip); mWorld.AddEntity(music); clip.Play(); clip.Looped = true; shotSound = new AudioClip(Settings.ModelsDirPath + "\\shot.wav"); visuals = new VisualEntity[50]; for (int i = 0; i < visuals.Length; i++) { StaticMesh m = new StaticMesh(Settings.ModelsDirPath + "\\Crate1.obj"); visuals[i] = new VisualEntity(m, mats[i % mats.Length]); VisualEntity visual = visuals[i]; visual.Transform.Position = Random.UnitSphere * 20; var bdy = phys.CreateRigidBody(visual.Transform.Position); bdy.AddCollisionShape(new BoxShape(1.1f)); visual.AddComponent(bdy); var cb = phys.CreateCollisionBody(visual.Transform.Position); var trigShape = new BoxShape(1.6f, true); trigShape.CollisionEvent += TrigShape_CollisionEvent; cb.AddCollisionShape(trigShape); visual.AddComponent(cb); mWorld.AddEntity(visual); } StaticMesh floor = new StaticMesh(Settings.ModelsDirPath + "\\Crate1.obj"); Material floorMat = Material.FromJSON(Settings.MaterialDirPath + "\\mahogfloor.mat.json"); VisualEntity floorVisual = new VisualEntity(floor, floorMat); floorVisual.Transform.Position = new Vector3(0f, -30f, 0f); floorVisual.Transform.Scale = new Vector3(100f, 0.2f, 100f); var floorBody = phys.CreateRigidBody(floorVisual.Transform.Position, floorVisual.Transform.Rotation); floorBody.AddCollisionShape(new BoxShape(50f, 0.1f, 50f)); floorBody.Type = BodyType.Static; floorVisual.AddComponent(floorBody); mWorld.AddEntity(floorVisual); Entity iblEnt = Entity.NewWithComponent(out IBLProbe iblProbe); iblProbe.Position = Vector3.Zero; mWorld.AddEntity(iblEnt); EventManager.UpdateEvent += Update; }
public VisualEntity CreateEntity(Vector3 entityPosition, VisualEntity entityParent, Entities entityType) { VisualEntity e = null; #region Create Entity switch (entityType) { case Entities.HeightFieldEntity: { e = new HeightFieldEntity("ground", "03RamieSc.dds", new MaterialProperties("ground", 0.8f, 0.5f, 0.8f)); } break; case Entities.SkyDomeEntity: { e = new SkyDomeEntity("skydome.dds", "sky_diff.dds"); SimulationEngine.GlobalInstancePort.Insert(e); } break; case Entities.LightSourceEntity: { var lightEntity = new LightSourceEntity(); e = lightEntity; lightEntity.State.Name = "LightSourceEntity"; lightEntity.Type = LightSourceEntityType.Directional; lightEntity.Color = new Vector4(.8f, .8f, .8f, 1); lightEntity.State.Pose.Position = new Vector3(0, 1, 0); lightEntity.Direction = new Vector3(0, -1, 0); e.Flags = e.Flags | VisualEntityProperties.DisableRendering; } break; case Entities.MotorBaseWithDrive: { e = new MotorBaseWithDrive(entityPosition); e.State.Name = "MotorBaseWithDrive"; MakeNameUnique(e); //Inserting Actual Rover instead of Simple Simulatd Robot. //Needs to be tested /*ColladaToMSRS e = new ColladaToMSRS(@"C:\.....\collada\rover.dae"); foreach (VisualEntity entity in e.Entities) { SimulationEngine.GlobalInstancePort.Insert(entity); }*/ } break; case Entities.SimulatedBrightnessSensorEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedBrightnessSensorEntity(32, 32, 2.0f * (float)Math.PI / 180.0f); e.State.Name = "SimulatedBrightnessSensorEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedColorSensorEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedColorSensorEntity(32, 32, 2.0f * (float)Math.PI / 180.0f); e.State.Name = "SimulatedColorSensorEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedCompassEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedCompassEntity(); e.State.Name = "SimulatedCompassEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedGPSEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedGPSEntity(); e.State.Name = "SimulatedGPSEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedIREntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedIREntity(new Pose(entityPosition)); e.State.Name = "SimulatedIREntity"; e.State.Flags |= EntitySimulationModifiers.DisableCollisions; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedLRFEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedLRFEntity(new Pose(entityPosition)); e.State.Name = "SimulatedLRFEntity"; e.State.Flags |= EntitySimulationModifiers.DisableCollisions; MakeNameUnique(e); //EntityUIService newservice = new EntityUIService(); //newservice.AddBoeBot(new Vector3(1,0,1)); //CreateService(laserrangefinder.Contract.Identifier, //Microsoft.Robotics.Simulation.Partners.CreateEntityPartner("http://localhost/" + e.State.Name)); entityParent.InsertEntity(e); } } break; case Entities.SimulatedSonarEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedSonarEntity(new Pose(entityPosition)); e.State.Name = "SimulatedSonarEntity"; e.State.Flags |= EntitySimulationModifiers.DisableCollisions; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; case Entities.SimulatedWebcamEntity: { if (entityParent == null) { ShowEntityRequiresParentError(entityType.ToString() + " requires parent"); } else { e = new SimulatedWebcamEntity(entityPosition, 320, 240, 45.0f * (float)Math.PI / 180.0f); e.State.Name = "SimulatedWebcamEntity"; MakeNameUnique(e); entityParent.InsertEntity(e); } } break; } #endregion return e; }
private void MakeNameUnique(VisualEntity entity) { int append = 1; string baseName = entity.State.Name; while (SimulationEngine.GlobalInstance.IsEntityNameInUse(entity.State.Name)) { entity.State.Name = baseName + append; ++append; } }
private static void BoundingBoxCollision(VerletSimulator physicSimu, VisualEntity entityTesting, ref BoundingBox entityBoundingBox, ref BoundingBox boundingBox2Evaluate, ref Vector3D newPosition2Evaluate, ref Vector3D previousPosition) { Vector3D newPositionWithColliding = previousPosition; newPositionWithColliding.Y = newPosition2Evaluate.Y; boundingBox2Evaluate = new BoundingBox(entityBoundingBox.Minimum + newPositionWithColliding.AsVector3(), entityBoundingBox.Maximum + newPositionWithColliding.AsVector3()); if (entityTesting.WorldBBox.Intersects(ref boundingBox2Evaluate)) { //If falling if (newPositionWithColliding.Y <= previousPosition.Y) { newPositionWithColliding.Y = entityTesting.WorldBBox.Maximum.Y; //previousPosition.Y; } else { newPositionWithColliding.Y = previousPosition.Y; } previousPosition.Y = newPositionWithColliding.Y; _onEntityTop = true; } newPositionWithColliding.X = newPosition2Evaluate.X; boundingBox2Evaluate = new BoundingBox(entityBoundingBox.Minimum + newPositionWithColliding.AsVector3(), entityBoundingBox.Maximum + newPositionWithColliding.AsVector3()); if (entityTesting.WorldBBox.Intersects(ref boundingBox2Evaluate, 0.001f)) { newPositionWithColliding.X = previousPosition.X; _onEntityTop = false; } newPositionWithColliding.Z = newPosition2Evaluate.Z; boundingBox2Evaluate = new BoundingBox(entityBoundingBox.Minimum + newPositionWithColliding.AsVector3(), entityBoundingBox.Maximum + newPositionWithColliding.AsVector3()); if (entityTesting.WorldBBox.Intersects(ref boundingBox2Evaluate, 0.001f)) { newPositionWithColliding.Z = previousPosition.Z; _onEntityTop = false; } //Set the NEW player position after collision tests newPosition2Evaluate = newPositionWithColliding; // ? Am I on "TOP" of an object ??? if (_onEntityTop == true) { physicSimu.OnGround = true; } if (entityTesting.Entity is IDynamicEntity) { //Send an impulse message to the Entity, following my "LookAtVector" ! float impulsePower = 1; if (_input.ActionsManager.isTriggered(UtopiaActions.Move_Run)) { impulsePower = 2; } _server.ServerConnection.Send(new EntityImpulseMessage { DynamicEntityId = (entityTesting.Entity as IDynamicEntity).DynamicId, Vector3 = MQuaternion.GetLookAtFromQuaternion(_player.Player.HeadRotation) * impulsePower } ); } }
private static bool SlidingSlope(VerletSimulator physicSimu, VisualEntity entityTesting, float Y, ref Vector3D newPosition2Evaluate, ref Vector3D previousPosition, ItemOrientation slopeOrientation, out bool isSliding) { isSliding = false; if (entityTesting.WorldBBox.Minimum.Y + Y >= newPosition2Evaluate.Y) { if (((entityTesting.WorldBBox.Minimum.Y + Y) - newPosition2Evaluate.Y) < 0.3f) { Y = Y - (float)(newPosition2Evaluate.Y - Math.Floor(newPosition2Evaluate.Y)); if (Y < 0.001 || Y >= 0.999) { Y = 0.02f; } newPosition2Evaluate.Y += entityTesting.Entity.Friction; isSliding = true; switch (slopeOrientation) { case ItemOrientation.North: newPosition2Evaluate.Z = newPosition2Evaluate.Z - Y; if (physicSimu.IsSliding == false) { physicSimu.SliddingForce = new Vector3(0, 0, -1); } break; case ItemOrientation.South: newPosition2Evaluate.Z = newPosition2Evaluate.Z + Y; if (physicSimu.IsSliding == false) { physicSimu.SliddingForce = new Vector3(0, 0, 1); } break; case ItemOrientation.East: newPosition2Evaluate.X = newPosition2Evaluate.X + Y; if (physicSimu.IsSliding == false) { physicSimu.SliddingForce = new Vector3(1, 0, 0); } break; case ItemOrientation.West: newPosition2Evaluate.X = newPosition2Evaluate.X - Y; if (physicSimu.IsSliding == false) { physicSimu.SliddingForce = new Vector3(-1, 0, 0); } break; default: break; } return(false); } else { return(true); } } else { physicSimu.AllowJumping = true; return(false); } }
/// <summary> /// Checks each part of the model for intersection /// </summary> /// <param name="entity"></param> /// <param name="pickRay"></param> /// <param name="intersectionPoint"></param> /// <param name="normal"></param> /// <param name="dist"></param> /// <returns></returns> public static bool ModelRayIntersection(VisualEntity entity, Ray pickRay, out Vector3 intersectionPoint, out Vector3I normal, out float dist) { intersectionPoint = new Vector3(); normal = new Vector3I(); dist = float.MaxValue; var visualVoxelEntity = entity as VisualVoxelEntity; if (visualVoxelEntity == null) { return(false); } var instance = visualVoxelEntity.VoxelEntity.ModelInstance; bool collisionDetected = false; //Check Against all existing "Sub-Cube" model //Get current Active state = A model can have multiple "State" (Like open, close, mid open, ...) var activeModelState = instance.State; var visualModel = visualVoxelEntity.VisualVoxelModel; if (visualModel == null || visualModel.VoxelModel != instance.VoxelModel) { return(false); } //For each Part in the model (A model can be composed of several parts) for (int partId = 0; partId < visualModel.VoxelModel.Parts.Count; partId++) { VoxelModelPartState partState = activeModelState.PartsStates[partId]; // it is possible that there is no frame, so no need to check it if (partState.ActiveFrame == byte.MaxValue) { continue; } BoundingBox frameBoundingBox = visualModel.VisualVoxelFrames[partState.ActiveFrame].BoundingBox; //Get Current Active part Frame = In animation case, the frame will be different when time passing by ... (Time depends) var activeframe = visualModel.VoxelModel.Frames[partState.ActiveFrame]; //one active at a time Matrix invertedEntityWorldMatrix = partState.GetTransformation() * Matrix.RotationQuaternion(instance.Rotation) * instance.World; Matrix entityWorldMatrix = invertedEntityWorldMatrix; invertedEntityWorldMatrix.Invert(); // convert ray to entity space var ray = pickRay.Transform(invertedEntityWorldMatrix); float partDistance; // if we don't intersect part BB then there is no reason to check each block BB if (!Collision.RayIntersectsBox(ref ray, ref frameBoundingBox, out partDistance)) { continue; } // don't check part that is far than already found intersection if (partDistance >= dist) { continue; } //Check each frame Body part Vector3I chunkSize = activeframe.BlockData.ChunkSize; byte[] data = activeframe.BlockData.BlockBytes; int index = -1; //Get all sub block not empty for (var z = 0; z < chunkSize.Z; z++) { for (var x = 0; x < chunkSize.X; x++) { for (var y = 0; y < chunkSize.Y; y++) { index++; //Get cube if (data[index] > 0) { //Collision checking against this ray var box = new BoundingBox(new Vector3(x, y, z), new Vector3(x + 1, y + 1, z + 1)); float blockDist; if (Collision.RayIntersectsBox(ref ray, ref box, out blockDist) && blockDist < dist) { dist = blockDist; Collision.RayIntersectsBox(ref ray, ref box, out intersectionPoint); normal = box.GetPointNormal(intersectionPoint); intersectionPoint = Vector3.TransformCoordinate(intersectionPoint, entityWorldMatrix); collisionDetected = true; } } } } } } return(collisionDetected); }
/// <summary> /// Update player picking /// </summary> /// <param name="pickingWorldPosition"></param> /// <param name="pickingLookAt"></param> /// <param name="blockPickingDistance"></param> /// <returns>return true if a new Item has been picked up !</returns> private bool RefreshPicking(ref Vector3D pickingWorldPosition, Vector3 pickingLookAt, float blockPickingDistance) { // first we will check entities // after that we will check blocks because they can be closer than the entity PlayerManager.Player.EntityState.IsEntityPicked = false; PlayerManager.Player.EntityState.IsBlockPicked = false; //Check the Ray against all entity first var pickingRay = new Ray(pickingWorldPosition.AsVector3(), pickingLookAt); var epr = CheckEntityPicking(pickingRay); if (epr.Found && epr.Distance > blockPickingDistance) { epr.Found = false; } var tool = PlayerManager.ActiveTool; if (tool.PickRange != 0f) { blockPickingDistance = Math.Min(tool.PickRange, blockPickingDistance); } var nbrPointToSample = (int)(Math.Min(blockPickingDistance, epr.Distance) / 0.02); float sliceLimitSquared = float.PositiveInfinity; if (_worldChunks.SliceValue != -1) { var topPlane = new Plane(new Vector3(0, _worldChunks.SliceValue, 0), Vector3.UnitY); var bottomPlane = new Plane(new Vector3(0, _worldChunks.SliceValue - 5, 0), Vector3.UnitY); Vector3 topIntersectionPoint; Vector3 bottomIntersectionPoint; var topIntersection = pickingRay.Intersects(ref topPlane, out topIntersectionPoint); var bottomIntersection = pickingRay.Intersects(ref bottomPlane, out bottomIntersectionPoint); if (!topIntersection && !bottomIntersection) { return(false); } if (topIntersection && bottomIntersection) { // find the closest one to the camera var topIsCloser = Vector3D.DistanceSquared(pickingWorldPosition, new Vector3D(topIntersectionPoint)) < Vector3D.DistanceSquared(pickingWorldPosition, new Vector3D(bottomIntersectionPoint)); if (topIsCloser) { pickingWorldPosition = new Vector3D(topIntersectionPoint); } else { pickingWorldPosition = new Vector3D(bottomIntersectionPoint); } sliceLimitSquared = Vector3.DistanceSquared(topIntersectionPoint, bottomIntersectionPoint); } else if (topIntersection) { sliceLimitSquared = (float)Vector3D.DistanceSquared(pickingWorldPosition, new Vector3D(topIntersectionPoint)); } else { sliceLimitSquared = (float)Vector3D.DistanceSquared(pickingWorldPosition, new Vector3D(bottomIntersectionPoint)); } if (epr.Found) { sliceLimitSquared = (float)Vector3D.DistanceSquared(pickingWorldPosition, new Vector3D(epr.PickPoint)); if (epr.PickPoint.Y > topIntersectionPoint.Y) { nbrPointToSample = 0; } } //if (Vector3D.DistanceSquared(prevPosition, prevPosition) > Vector3D.DistanceSquared(prevPosition, new Vector3D(bottomPoint))) // pickingWorldPosition = new Vector3D(intersectPoint); } var startPos = pickingWorldPosition; //Check for Cube Picking //Sample points in the view direction vector for (var ptNbr = 0; ptNbr < nbrPointToSample; ptNbr++) { pickingWorldPosition += new Vector3D(pickingLookAt * 0.02f); if (Vector3D.DistanceSquared(pickingWorldPosition, startPos) >= sliceLimitSquared) { break; } //Check if a block is picked up ! var result = _cubesHolder.GetCube(pickingWorldPosition); if (!result.IsValid) { break; } var blockProfile = _cubesHolder.Config.BlockProfiles[result.Cube.Id]; var yOffset = blockProfile.YBlockOffset; var pickType = tool.CanPickBlock(blockProfile); if (pickType == PickType.Stop) { // we found a block that is closer than entity (if any) // don't allow to pick the entity in this case epr.Found = false; break; } if (pickType == PickType.Pick) { var blockPos = pickingWorldPosition.ToCubePosition(); PlayerManager.Player.EntityState.PickedBlockPosition = blockPos; var cubeBB = new BoundingBox(blockPos, blockPos + new Vector3(1, 1f - (float)yOffset, 1)); Vector3 faceInteresection; if (cubeBB.Intersects(ref pickingRay, out faceInteresection)) { _prevCube = _pickedCube; _pickedCube = new TerraCubeWithPosition { Position = blockPos, BlockProfile = blockProfile, Cube = result.Cube }; PlayerManager.Player.EntityState.PickedBlockFaceOffset = Vector3.One - (_pickedCube.Position - faceInteresection); PlayerManager.Player.EntityState.PickPoint = faceInteresection; PlayerManager.Player.EntityState.PickPointNormal = cubeBB.GetPointNormal(faceInteresection); } bool newPlacechanged = false; //Find the potential new block place, by rolling back ! while (ptNbr > 0) { pickingWorldPosition -= new Vector3D(pickingLookAt * 0.02f); if (_cubesHolder.isPickable(ref pickingWorldPosition, out _newCube) == false) { PlayerManager.Player.EntityState.NewBlockPosition = _newCube.Position; newPlacechanged = true; break; } ptNbr--; } PlayerManager.Player.EntityState.IsEntityPicked = false; PlayerManager.Player.EntityState.IsBlockPicked = true; if (_prevCube.Position == PlayerManager.Player.EntityState.PickedBlockPosition) { if (!newPlacechanged) { return(false); } } break; } } // we need to decide what we have picked (block or entity) // if we found the block this means that it is closer than entity // (because we used limit as the closest picked entity) if (!PlayerManager.Player.EntityState.IsBlockPicked && epr.Found) { _pickedUpEntity = epr.PickedEntity; _pickedUpEntityPosition = _pickedUpEntity.Entity.Position; PlayerManager.Player.EntityState.PickedEntityPosition = _pickedUpEntity.Entity.Position; PlayerManager.Player.EntityState.PickedEntityLink = _pickedUpEntity.Entity.GetLink(); PlayerManager.Player.EntityState.PickPoint = epr.PickPoint; PlayerManager.Player.EntityState.PickPointNormal = epr.PickNormal; PlayerManager.Player.EntityState.IsEntityPicked = true; PlayerManager.Player.EntityState.IsBlockPicked = false; } return(PlayerManager.Player.EntityState.IsBlockPicked || PlayerManager.Player.EntityState.IsEntityPicked); }
private static bool IsCollidingWithModel(VisualEntity entityTesting, BoundingBox playerBoundingBox2Evaluate) { var visualVoxelEntity = entityTesting as VisualVoxelEntity; if (visualVoxelEntity == null) { return(false); } var instance = visualVoxelEntity.VoxelEntity.ModelInstance; if (instance == null) { return(false); } int index; bool collisionDetected = false; //Check Against all existing "Sub-Cube" model //Get current Active state = A model can have multiple "State" (Like open, close, mid open, ...) var activeModelState = instance.State; var visualModel = visualVoxelEntity.VisualVoxelModel; //For each Part in the model (A model can be composed of several parts) for (int partId = 0; partId < visualModel.VoxelModel.Parts.Count && !collisionDetected; partId++) { VoxelModelPartState partState = activeModelState.PartsStates[partId]; // it is possible that there is no frame, so no need to check anything if (partState.ActiveFrame == byte.MaxValue) { continue; } VoxelModelPart part = visualModel.VoxelModel.Parts[partId]; BoundingBox frameBoundingBox = visualModel.VisualVoxelFrames[partState.ActiveFrame].BoundingBox; //Get Current Active part Frame = In animation case, the frame will be different when time passing by ... (Time depends) var activeframe = visualModel.VoxelModel.Frames[partState.ActiveFrame]; //one active at a time Matrix invertedEntityWorldMatrix = partState.GetTransformation() * Matrix.RotationQuaternion(instance.Rotation) * instance.World; invertedEntityWorldMatrix.Invert(); BoundingBox PlayerBBInEntitySpace = playerBoundingBox2Evaluate.Transform(invertedEntityWorldMatrix); // if we don't intersect part BB then there is no reason to check each block BB if (!frameBoundingBox.Intersects(ref PlayerBBInEntitySpace)) { continue; } //Check each frame Body part Vector3I chunkSize = activeframe.BlockData.ChunkSize; byte[] data = activeframe.BlockData.BlockBytes; index = -1; //Get all sub block not empty for (int z = 0; z < chunkSize.Z && !collisionDetected; z++) { for (int x = 0; x < chunkSize.X && !collisionDetected; x++) { for (int y = 0; y < chunkSize.Y && !collisionDetected; y++) { index++; //Get cube if (data[index] > 0) { //Collision checking against this point. if (PlayerBBInEntitySpace.Minimum.X > x + 1 || x > PlayerBBInEntitySpace.Maximum.X) { continue; //No collision } if (PlayerBBInEntitySpace.Minimum.Y > y + 1 || y > PlayerBBInEntitySpace.Maximum.Y) { continue; //No collision } if (PlayerBBInEntitySpace.Minimum.Z > z + 1 || z > PlayerBBInEntitySpace.Maximum.Z) { continue; //No collision } //Collision HERE !!! collisionDetected = true; } } } } } return(collisionDetected); }
private static void ModelCollisionDetection(VerletSimulator physicSimu, VisualEntity entityTesting, ref BoundingBox playerBoundingBox, ref BoundingBox playerBoundingBox2Evaluate, ref Vector3D newPosition2Evaluate, ref Vector3D previousPosition) { if (entityTesting.SkipOneCollisionTest) { entityTesting.SkipOneCollisionTest = false; return; } Vector3D newPositionWithColliding = previousPosition; _onEntityTop = null; newPositionWithColliding.X = newPosition2Evaluate.X; playerBoundingBox2Evaluate = new BoundingBox(playerBoundingBox.Minimum + newPositionWithColliding.AsVector3(), playerBoundingBox.Maximum + newPositionWithColliding.AsVector3()); if (IsCollidingWithModel(entityTesting, playerBoundingBox2Evaluate)) { //logger.Debug("ModelCollisionDetection X detected tested {0}, assigned (= previous) {1}", newPositionWithColliding.X, previousPosition.X); newPositionWithColliding.X = previousPosition.X; _onEntityTop = false; Player.YForceApplying = entityTesting.Entity.YForceOnSideHit; } newPositionWithColliding.Z = newPosition2Evaluate.Z; playerBoundingBox2Evaluate = new BoundingBox(playerBoundingBox.Minimum + newPositionWithColliding.AsVector3(), playerBoundingBox.Maximum + newPositionWithColliding.AsVector3()); if (IsCollidingWithModel(entityTesting, playerBoundingBox2Evaluate)) { //logger.Debug("ModelCollisionDetection Z detected tested {0}, assigned (= previous) {1}", newPositionWithColliding.Z, previousPosition.Z); newPositionWithColliding.Z = previousPosition.Z; _onEntityTop = false; Player.YForceApplying = entityTesting.Entity.YForceOnSideHit; } newPositionWithColliding.Y = newPosition2Evaluate.Y; playerBoundingBox2Evaluate = new BoundingBox(playerBoundingBox.Minimum + newPositionWithColliding.AsVector3(), playerBoundingBox.Maximum + newPositionWithColliding.AsVector3()); if (IsCollidingWithModel(entityTesting, playerBoundingBox2Evaluate)) { //logger.Debug("ModelCollisionDetection Y detected tested {0}, assigned (= previous) {1}", newPositionWithColliding.Y, previousPosition.Y); newPositionWithColliding.Y = previousPosition.Y; if (_onEntityTop == null) { _onEntityTop = true; } } else { if (_isOnGround) { playerBoundingBox2Evaluate.Minimum.Y -= 0.01f; if (IsCollidingWithModel(entityTesting, playerBoundingBox2Evaluate)) { _onEntityTop = true; } } } //Set the NEW player position after collision tests newPosition2Evaluate = newPositionWithColliding; if (_onEntityTop == true) { physicSimu.OnGround = true; _isOnGround = true; physicSimu.AllowJumping = true; } else { _isOnGround = false; } playerBoundingBox2Evaluate = new BoundingBox(playerBoundingBox.Minimum + newPositionWithColliding.AsVector3(), playerBoundingBox.Maximum + newPositionWithColliding.AsVector3()); if (_onEntityTop != true && IsCollidingWithModel(entityTesting, playerBoundingBox2Evaluate)) { //I'm "Blocked" by this entity ! //Testing, inject Force to unblock myself ! var forceDirection = playerBoundingBox2Evaluate.GetCenter() - entityTesting.WorldBBox.GetCenter(); forceDirection.Normalize(); physicSimu.Impulses.Add(new Impulse { ForceApplied = forceDirection * 3 }); entityTesting.SkipOneCollisionTest = true; } }
public void SetPickedEntity(VisualEntity pickedEntity) { _pickedEntity = pickedEntity; RefreshpickedBoundingBox(false); }
public Aim() { radius = new VisualEntity(new Sprite(color: Color.White) { Source = new Rectangle(0, 192, 80, 80), Origin = new Vector2(40, 40), Width = 80, Height = 80 }, false) { Width = 80, Height = 80 }; angleEntity = new VisualEntity( new AnimatedSprite( new List <Animation>() { Animation.GetFromAtlas("BlinkingAngle", 0, 17, 16, 16, 2, 100) }, color: Color.White) { Name = "AngleEntity", Origin = new Vector2(8, 8), Width = 16, Height = 16 }, false) { Width = 16, Height = 16 }; trajectory = new VisualEntity( new AnimatedSprite( new List <Animation>() { new Animation() { Name = "TrajectoryAnimation", Frames = new List <Animation.Frame>() { new Animation.Frame() { Source = new Rectangle(64, 275, trajectoryWidth, 3), Duration = 100 }, new Animation.Frame() { Source = new Rectangle(64, 282, trajectoryWidth, 3), Duration = 100 } } } }, color: Color.White) { Name = "Trajectory", Origin = new Vector2(0, 1), Width = trajectoryWidth, Height = 3 }, false) { Width = trajectoryWidth, Height = 3 }; target = new VisualEntity( new AnimatedSprite( new List <Animation>() { Animation.GetFromAtlas("BlinkingAngleEnd", 2, 17, 16, 16, 2, 100) }, color: Color.White) { Name = "AngleEndEntity", Origin = new Vector2(8, 9), Width = 16, Height = 16 }, false) { Width = 16, Height = 16 }; }