public Wall(Wall k) { name = k.name; //Console.WriteLine(name); colored = false; line = new Line2D(k.line); //set center point location = line.midpoint(); dynamic = false; visible = k.visible; }
private void MouseMoveHandler(object sender, MouseEventArgs e) { if (drawMode == drawModes.selectMode) { if (bDragDisplay) { //how far has the wall been dragged? int dx = e.X - x1; int dy = e.Y - y1; x1 = e.X; y1 = e.Y; float odx,ody; frame.offset_from_display((float)dx,(float)dy,out odx, out ody); frame.cx-=odx; frame.cy-=ody; } if (bSelected_wall) { //wall-dragging code //how far has the wall been dragged? int dx = e.X - x1; int dy = e.Y - y1; x1 = e.X; y1 = e.Y; float odx,ody; frame.offset_from_display((float)dx,(float)dy,out odx, out ody); if (selectMode == selectModes.dragMode) { //update wall's position selected_wall.line.p1.x += odx; selected_wall.line.p1.y += ody; selected_wall.line.p2.x += odx; selected_wall.line.p2.y += ody; } if (selectMode == selectModes.rotateMode) { Point2D midpoint = selected_wall.line.midpoint(); selected_wall.line.p1.rotate(dy / 180.0 * 3.14, midpoint); selected_wall.line.p2.rotate(dy / 180.0 * 3.14, midpoint); } if (selectMode == selectModes.scaleMode) { selected_wall.line.scale(1.0 + 0.05 * dy); } Invalidate(); } if (bSelected_robot) { //robot-dragging code //how far has the robot been dragged? int dx = e.X - x1; int dy = e.Y - y1; x1 = e.X; y1 = e.Y; float odx,ody; frame.offset_from_display((float)dx,(float)dy,out odx, out ody); //update robot's position if (selectMode == selectModes.dragMode) { selected_robot.location.x += odx; selected_robot.location.y += ody; } if (selectMode == selectModes.rotateMode) { selected_robot.heading += (dy / 10.0); } if (selectMode == selectModes.scaleMode) { selected_robot.radius += (dy / 2.0); if (selected_robot.radius < 1.0) selected_robot.radius = 1.0; selected_robot.circle.radius = selected_robot.radius; } Invalidate(); } } if (drawMode == drawModes.selectMode && !bSelected_wall && !bSelected_robot) { selected_wall = null; selected_robot = null; display_snap = false; //find snap Point2D newpoint = new Point2D((double)e.X, (double)e.Y); if (experiment != null) { if(experiment.robots!=null) foreach (Robot robot in experiment.robots) { Point2D screen_location = frame.to_display(robot.location); if (screen_location.distance(newpoint) < 20.0) { display_snap = true; snapx = (int)screen_location.x; snapy = (int)screen_location.y; selected_robot = robot; } } if(experiment.environment.walls!=null) foreach (Wall wall in experiment.environment.walls) { Point2D screen_location = frame.to_display(wall.location); if (screen_location.distance(newpoint) < 20.0) { display_snap = true; snapx = (int)screen_location.x; snapy = (int)screen_location.y; selected_wall = wall; } } int index = 0; selected_POI = -1; if (experiment.environment.walls != null) foreach (Point p in experiment.environment.POIPosition) { Point2D screen_location = frame.to_display(new Point2D(p.X, p.Y)); if (screen_location.distance(newpoint) < 20.0) { display_snap = true; snapx = (int)screen_location.x; snapy = (int)screen_location.y; selected_POI = index; } index++; } } Invalidate(); } if (drawMode == drawModes.wallMode) { display_snap = false; //find snap Point2D newpoint = new Point2D((double)e.X, (double)e.Y); foreach (Wall wall in experiment.environment.walls) { Point2D screen_location1 = frame.to_display(wall.line.p1); Point2D screen_location2 = frame.to_display(wall.line.p2); if (screen_location1.distance(newpoint) < 10.0) { display_snap = true; snapx = (int)screen_location1.x; snapy = (int)screen_location1.y; } if (screen_location2.distance(newpoint) < 10.0) { display_snap = true; snapx = (int)screen_location2.x; snapy = (int)screen_location2.y; } } x2 = e.X; y2 = e.Y; Invalidate(); } if (drawMode == drawModes.AOIMode && displayAOIRectangle) { float ox,oy; frame.from_display(e.X,e.Y,out ox,out oy); experiment.environment.AOIRectangle = new Rectangle(experiment.environment.AOIRectangle.X, experiment.environment.AOIRectangle.Y,(int) ox - experiment.environment.AOIRectangle.X, (int) oy - experiment.environment.AOIRectangle.Y); Invalidate(); } }
private void MouseUpHandler(object sender, MouseEventArgs e) { if (drawMode == drawModes.selectMode) { if (bSelected_wall && selected_wall != null) selected_wall.location = selected_wall.line.midpoint(); bSelected_wall = false; bDragDisplay = false; bSelected_robot = false; Invalidate(); } if (drawMode == drawModes.wallMode) { x2 = e.X; y2 = e.Y; if (display_snap) { x2 = snapx; y2 = snapy; } display_tempwall = false; float ox1,oy1,ox2,oy2; frame.from_display((float)x1,(float)y1,out ox1, out oy1); frame.from_display((float)x2,(float)y2,out ox2, out oy2); Wall new_wall = new Wall((double)ox1, (double)oy1, (double)ox2, (double)oy2, true, ""); experiment.environment.walls.Add(new_wall); Invalidate(); } if (drawMode == drawModes.AOIMode) { displayAOIRectangle = false; } }
//handles key down event...e.g. control keys like delete private void KeyDownHandler(object sender, System.Windows.Forms.KeyEventArgs e) { /*if (this.experiment.robots!=null) if(this.experiment.robots.Count>0) { if(this.experiment.robots[0] is Khepera3RobotModelJoy) { Khepera3RobotModelJoy k = (Khepera3RobotModelJoy)this.experiment.robots[0]; if(e.KeyCode ==Keys.Z) k.SetCommand(1); if(e.KeyCode ==Keys.X) k.SetCommand(0); if(e.KeyCode ==Keys.C) k.SetCommand(2); if(e.KeyCode ==Keys.Space) k.SetCommand(3); } }*/ if (drawMode == drawModes.selectMode) { if (e.KeyCode == Keys.W) { if (selected_wall != null) { Wall thewall = selected_wall; //InputBoxSample.InputBoxResult result; ObjectRenamer or = new ObjectRenamer(selected_wall.name); DialogResult result = or.ShowDialog(this); //result = InputBoxSample.InputBox.Show("Name wall", "Name wall", selected_wall.name, null); if (result == DialogResult.OK) { //System.Console.WriteLine(selected_wall.name); thewall.name = or.textBox1.Text; } } } if (e.KeyCode == Keys.D) robot_display_debug=!robot_display_debug; if (e.KeyCode == Keys.V) { if (selected_wall != null) { selected_wall.visible = !selected_wall.visible; } } if (e.KeyCode == Keys.Delete) { if (selected_wall != null) experiment.environment.walls.Remove(selected_wall); selected_wall = null; if (selected_POI != -1) { experiment.environment.POIPosition.RemoveAt(selected_POI); selected_POI = -1; } display_snap = false; Invalidate(); } } }
public static bool collide(Robot a, Wall b) { return EngineUtilities.collide(b, a); }
public static bool collide(Wall wall, Robot robot) { Point2D a1 = new Point2D(wall.line.p1); Point2D a2 = new Point2D(wall.line.p2); Point2D b = new Point2D(robot.location.x, robot.location.y); if (!wall.visible) return false; double rad = robot.radius; double r = ((b.x - a1.x) * (a2.x - a1.x) + (b.y - a1.y) * (a2.y - a1.y)) / wall.line.length_sq(); double px = a1.x + r * (a2.x - a1.x); double py = a1.y + r * (a2.y - a1.y); Point2D np = new Point2D(px, py); double rad_sq = rad * rad; if (r >= 0.0f && r <= 1.0f) { if (np.distance_sq(b) < rad_sq) return true; else return false; } double d1 = b.distance_sq(a1); double d2 = b.distance_sq(a2); if (d1 < rad_sq || d2 < rad_sq) return true; else return false; }
//wall-wall (should never collide) public static bool collide(Wall a, Wall b) { return false; }
public static bool collide(Robot a, Wall b) { return(EngineUtilities.collide(b, a)); }
//TODO dashedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //TODO how can a new object handle collsions differently? #region Collision Handling //wall-wall (should never collide) public static bool collide(Wall a, Wall b) { return(false); }