public void PerformAction(ICvarcEngine engine, string actor, string action) { var robotBody = engine.GetBody(actor); if (action == "Grip") { Grip(engine, robotBody); } if (action == "Release") { Release(engine, robotBody); } }
private void Release(ICvarcEngine engine, Body Body) { var latestGripped = Body.FirstOrDefault(z => z.Type.StartsWith("D") && z.Type.Length == 2); if (latestGripped == null) { return; } var absoluteLocation = latestGripped.GetAbsoluteLocation(); Body.Remove(latestGripped); latestGripped.FrictionCoefficient = frictionCoefficientsById.SafeGet(latestGripped.Id); var targetColor = latestGripped.Type[1].ToString(); latestGripped.Location = absoluteLocation; latestGripped.Velocity = Body.Velocity; var toAtt = Body.TreeRoot.GetSubtreeChildrenFirst() .Where(a => (a.Type == "VW" + targetColor || a.Type == "HW" + targetColor) && Distance(latestGripped, a) < 30) .OfType <Box>() .FirstOrDefault(); if (toAtt != null) { Body.TreeRoot.Remove(toAtt); var wall = new Box { Type = toAtt.Type.Substring(0, 2), XSize = toAtt.XSize, YSize = toAtt.YSize, ZSize = toAtt.ZSize, Location = toAtt.Location, DefaultColor = SRCompetitions.WallColor, IsStatic = true, IsMaterial = true }; Body.TreeRoot.Add(wall); engine.RaiseOnCollision(Body.Id.ToString(), toAtt.Id.ToString(), GetCollisionType(targetColor)); } else { Body.TreeRoot.Add(latestGripped); } // gripped.RemoveRange(0, gripped.Count); }
private void Grip(ICvarcEngine engine, Body Body) { var gripped = Body.ToList(); if (gripped.Any()) { return; } var found = Body.TreeRoot.GetSubtreeChildrenFirst().FirstOrDefault(a => CanBeAttached(Body, a) && (a.Parent.Id == a.TreeRoot.Id)); if (found != null) { Body latestGripped = null; var oldGrippedCount = gripped.Count; if (oldGrippedCount > 0) { latestGripped = gripped.Last(); if (found.DefaultColor != latestGripped.DefaultColor) { return; } } var tempfound = found; while (tempfound.Any()) { tempfound = tempfound.FirstOrDefault(); gripped.Add(tempfound); } if (oldGrippedCount > 0) { Body.Remove(latestGripped); if (latestGripped != null) { latestGripped.Location = new Frame3D(0, 0, 8, Angle.Zero, Angle.Zero, Angle.Zero); tempfound.Add(latestGripped); } } CaptureDevicet(Body, found); gripped.Add(found); } }
public Body CreateWorld(ICvarcEngine engine, ISceneSettings _settings) { Settings = (SceneSettings)_settings; var root = new Body(); var first = new Cylinder { Height = 20, RTop = 10, RBottom = 10, Location = new Frame3D(-150 + 25 - 10, 100 - 25 + 10, 3), DefaultColor = Color.DarkViolet, IsMaterial = true, Density = Density.Iron, FrictionCoefficient = 0, Top = new PlaneImageBrush { Image = new Bitmap(GetResourceStream("red.png")) }, Type = "Robot" }; var second = new Cylinder { Height = 20, RTop = 10, RBottom = 10, Location = new Frame3D(150 - 25 + 10, 100 - 25 + 10, 3, Angle.Zero, Angle.Pi, Angle.Zero), DefaultColor = Color.DarkViolet, IsMaterial = true, Density = Density.Iron, FrictionCoefficient = 0, Top = new PlaneImageBrush { Image = new Bitmap(GetResourceStream("blue.png")) }, Type = "Robot" }; root.Add(first); root.Add(second); first.Collision += body => engine.RaiseOnCollision(first.Id.ToString(), body.Id.ToString(), CollisionType.RobotCollision); second.Collision += body => engine.RaiseOnCollision(second.Id.ToString(), body.Id.ToString(), CollisionType.RobotCollision); root.Add(new Box { XSize = 300, YSize = 200, ZSize = 3, DefaultColor = Color.White, Top = new SolidColorBrush { Color = Color.Yellow }, IsStatic = true, Type = "floor", }); foreach (var detail in Settings.Details) { Color color = Color.White; string name = "D"; switch (detail.Color) { case DetailColor.Red: color = Color.Red; name += "R"; break; case DetailColor.Blue: color = Color.Blue; name += "B"; break; case DetailColor.Green: color = Color.Green; name += "G"; break; } var box = new Box { XSize = 15, YSize = 15, ZSize = 15, Location = new Frame3D(-150 + 25 + detail.Location.X * 50, 100 - 25 - 50 * detail.Location.Y, 0), DefaultColor = color, Type = name, IsMaterial = true, IsStatic = false, FrictionCoefficient = 8 }; root.Add(box); box.Collision += body => { if (box.Parent.Id == first.Id && body.Id == second.Id) { engine.RaiseOnCollision(second.Id.ToString(), first.Id.ToString(), CollisionType.RobotCollision); } if (box.Parent.Id == second.Id && body.Id == first.Id) { engine.RaiseOnCollision(first.Id.ToString(), second.Id.ToString(), CollisionType.RobotCollision); } }; } CreateWalls(root, Settings.HorizontalWalls, 50, 10, 15, "HW", (x, y) => new Point(-150 + 25 + x * 50, 100 - (y + 1) * 50)); CreateWalls(root, Settings.VerticalWalls, 10, 50, 14, "VW", (x, y) => new Point(-150 + (x + 1) * 50, 100 - 25 - y * 50)); CreateBorders(root); return(root); }
public Body CreateWorld(ICvarcEngine engine, ISceneSettings _settings) { Settings = (SceneSettings)_settings; var root = new Body(); var first = new Cylinder { Height = 20, RTop = 10, RBottom = 10, Location = new Frame3D(-150 + 25 - 10, 100 - 25 + 10, 3), DefaultColor = Color.DarkViolet, IsMaterial = true, Density = Density.Iron, FrictionCoefficient = 0, Top = new PlaneImageBrush { Image = new Bitmap(GetResourceStream("red.png")) }, Type = "Robot" }; var second = new Cylinder { Height = 20, RTop = 10, RBottom = 10, Location = new Frame3D(150 - 25 + 10, 100 - 25 + 10, 3, Angle.Zero, Angle.Pi, Angle.Zero), DefaultColor = Color.DarkViolet, IsMaterial = true, Density = Density.Iron, FrictionCoefficient = 0, Top = new PlaneImageBrush { Image = new Bitmap(GetResourceStream("blue.png")) }, Type = "Robot" }; root.Add(first); root.Add(second); first.Collision += body => engine.RaiseOnCollision(first.Id.ToString(), body.Id.ToString(), CollisionType.RobotCollision); second.Collision += body => engine.RaiseOnCollision(second.Id.ToString(), body.Id.ToString(), CollisionType.RobotCollision); root.Add(new Box { XSize = 300, YSize = 200, ZSize = 3, DefaultColor = Color.White, Top = new SolidColorBrush { Color = Color.Yellow }, IsStatic = true, Type = "floor", }); foreach (var detail in Settings.Details) { Color color = Color.White; string name = "D"; switch (detail.Color) { case DetailColor.Red: color = Color.Red; name += "R"; break; case DetailColor.Blue: color = Color.Blue; name += "B"; break; case DetailColor.Green: color = Color.Green; name += "G"; break; } var box = new Box { XSize = 15, YSize = 15, ZSize = 15, Location = new Frame3D(-150 + 25 + detail.Location.X * 50, 100 - 25 - 50 * detail.Location.Y, 0), DefaultColor = color, Type = name, IsMaterial = true, IsStatic = false, FrictionCoefficient = 8 }; root.Add(box); box.Collision += body => { if (box.Parent.Id == first.Id && body.Id == second.Id) engine.RaiseOnCollision(second.Id.ToString(), first.Id.ToString(), CollisionType.RobotCollision); if (box.Parent.Id == second.Id && body.Id == first.Id) engine.RaiseOnCollision(first.Id.ToString(), second.Id.ToString(), CollisionType.RobotCollision); }; } CreateWalls(root, Settings.HorizontalWalls, 50, 10, 15, "HW", (x, y) => new Point(-150 + 25 + x * 50, 100 - (y + 1) * 50)); CreateWalls(root, Settings.VerticalWalls, 10, 50, 14, "VW", (x, y) => new Point(-150 + (x + 1) * 50, 100 - 25 - y * 50)); CreateBorders(root); return root; }
private void Grip(ICvarcEngine engine, Body Body) { var gripped = Body.ToList(); if (gripped.Any()) return; var found = Body.TreeRoot.GetSubtreeChildrenFirst().FirstOrDefault(a => CanBeAttached(Body, a) && (a.Parent.Id == a.TreeRoot.Id)); if (found != null) { Body latestGripped = null; var oldGrippedCount = gripped.Count; if (oldGrippedCount > 0) { latestGripped = gripped.Last(); if (found.DefaultColor != latestGripped.DefaultColor) return; } var tempfound = found; while (tempfound.Any()) { tempfound = tempfound.FirstOrDefault(); gripped.Add(tempfound); } if (oldGrippedCount > 0) { Body.Remove(latestGripped); if (latestGripped != null) { latestGripped.Location = new Frame3D(0, 0, 8, Angle.Zero, Angle.Zero, Angle.Zero); tempfound.Add(latestGripped); } } CaptureDevicet(Body, found); gripped.Add(found); } }
private void Release(ICvarcEngine engine, Body Body) { var latestGripped = Body.FirstOrDefault(z => z.Type.StartsWith("D") && z.Type.Length == 2); if (latestGripped == null) return; var absoluteLocation = latestGripped.GetAbsoluteLocation(); Body.Remove(latestGripped); latestGripped.FrictionCoefficient = frictionCoefficientsById.SafeGet(latestGripped.Id); var targetColor = latestGripped.Type[1].ToString(); latestGripped.Location = absoluteLocation; latestGripped.Velocity = Body.Velocity; var toAtt = Body.TreeRoot.GetSubtreeChildrenFirst() .Where(a => (a.Type == "VW" + targetColor || a.Type == "HW" + targetColor) && Distance(latestGripped, a) < 30) .OfType<Box>() .FirstOrDefault(); if (toAtt != null) { Body.TreeRoot.Remove(toAtt); var wall = new Box { Type = toAtt.Type.Substring(0, 2), XSize = toAtt.XSize, YSize = toAtt.YSize, ZSize = toAtt.ZSize, Location = toAtt.Location, DefaultColor = SRCompetitions.WallColor, IsStatic = true, IsMaterial = true }; Body.TreeRoot.Add(wall); engine.RaiseOnCollision(Body.Id.ToString(), toAtt.Id.ToString(), GetCollisionType(targetColor)); } else Body.TreeRoot.Add(latestGripped); // gripped.RemoveRange(0, gripped.Count); }
public void PerformAction(ICvarcEngine engine, string actor, string action) { var robotBody = engine.GetBody(actor); if (action == "Grip") Grip(engine, robotBody); if (action == "Release") Release(engine, robotBody); }