public double getDistance(Vector<VivaeObject> objects) { GeneralPath thisPath = new GeneralPath(this.getTransformedShape()); PathIntersection pi = new PathIntersection(); Point2D.Double[] points; double dist = ray_length; double nd; for (VivaeObject vivaeObject : objects) { if (vivaeObject != this.owner) { GeneralPath gp = new GeneralPath(vivaeObject.getTransformedShape()); points = pi.getIntersections(thisPath, gp); for (Point2D.Double point : points) { nd = pi.euclideanDistance(point.getX(), point.getY(), owner.getX(), owner.getY()); if (nd < dist) { dist = nd; } // System.out.println(dist); } } } opacity = Math.max((float) (1d - (dist / ray_length)), opacity); opacityOfRay = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity); return 1d - (dist / ray_length); }
/** * Method intersects area of Sensor and all VivaeObjects and returns those that have non-zero * intersection. * * @param objects Vector of VivaeObjects that are checked for collision with the body of Sensor. * @return Vector of VivaeObjects that are in collision with the body of Sensor. */ public Vector<VivaeObject> getVivaesOnSight(Vector<VivaeObject> objects) { Vector<VivaeObject> objectsOnSight = new Vector<VivaeObject>(); // for (VivaeObject vivaeObject : objects) { GeneralPath thisPath = new GeneralPath(this.getTransformedShape()); PathIntersection pi = new PathIntersection(); Point2D.Double[] points; for (VivaeObject vivaeObject : getCloseVivaes(objects)) { if (vivaeObject != this.owner) { GeneralPath gp = new GeneralPath(vivaeObject.getTransformedShape()); points = pi.getIntersections(thisPath, gp); /* for(Point2D.Double point : points){ System.out.println("x = "+point.getX()+" y = "+point.getY()); System.out.println("owner x = "+owner.getX()+" owner y = "+owner.getY()); System.out.println(pi.euclideanDistance(point.getX(),point.getY(),owner.getX(),owner.getY())); }*/ if (points.length > 0) { objectsOnSight.add(vivaeObject); } /*Area actArea = (Area) vivaeObject.getArea().clone(); actArea.intersect(this.getArea()); if (!actArea.isEmpty()) objectsOnSight.add(vivaeObject); */ // if(vivaeObject instanceof roboneat.RoboNeatRobot)System.out.println("robot // seen by"+this.owner); } } return objectsOnSight; }
/** * Method intersects area of Sensor and all VivaeObjects and returns those that have non-zero * intersection. * * @param objects Vector of VivaeObjects that are checked for collision with the body of Sensor. * @return Vector of VivaeObjects that are in collision with the body of Sensor. */ public Vector<VivaeObject> getVivaesOnSight(Vector<VivaeObject> objects) { Vector<VivaeObject> objectsOnSight = new Vector<VivaeObject>(); // for (VivaeObject vivaeObject : objects) { for (VivaeObject vivaeObject : getCloseVivaes(objects, owner.getArena().getWalls())) { if (vivaeObject != this.owner) { Area actArea = (Area) vivaeObject.getArea().clone(); actArea.intersect(this.getArea()); if (!actArea.isEmpty()) objectsOnSight.add(vivaeObject); // if(vivaeObject instanceof roboneat.RoboNeatRobot)System.out.println("robot // seen by"+this.owner); } } return objectsOnSight; }
/** * This method removes all VivaeObjects that are further from owner of this Sensor than length of * the Sensor is. * * @param objects Vector of all VivaeObjects that are checked for distance from owner of this * Sensor. * @param walls Vector of walls that can contain enclosing walls in Arena. * @return new Vector of VivaeObjects that are close enough to be in range of Sensor. */ public Vector<VivaeObject> getCloseVivaes(Vector<VivaeObject> objects, Vector<Fixed> walls) { Vector<VivaeObject> closeObjects = new Vector<VivaeObject>(); for (VivaeObject vivae : objects) { if (vivae.getBoundingCircleRadius() + ray_length > vivae.getBody().getPosition().distance(ownerBody.getPosition())) { closeObjects.add(vivae); } } if (!owner.getArena().isEnclosedWithWalls()) return closeObjects; float xPos = owner.getBody().getPosition().getX(); float yPos = owner.getBody().getPosition().getY(); if (ray_length > yPos) closeObjects.add(walls.get(0)); else if (owner.getArena().screenHeight - ray_length < yPos) closeObjects.add(walls.get(1)); if (ray_length > xPos) closeObjects.add(walls.get(3)); else if (owner.getArena().screenWidth - ray_length < xPos) closeObjects.add(walls.get(2)); return closeObjects; }
@Override /** Paints up the arena and all the surfaces and objects inside using DoubleBuffering. */ public void paint(Graphics g) { if (offscreen == null) { return; } bufferGraphics.setColor(Color.WHITE); bufferGraphics.fillRect(this.getX(), this.getY(), screenWidth, screenHeight); for (Surface vivaeObject : surfaces) { vivaeObject.paintComponent(bufferGraphics); } for (Passive vivaeObject : getPassives()) { vivaeObject.paintComponent(bufferGraphics, isObjectsOnSightBlinking); } for (VivaeObject vivaeObject : getPaintable()) { vivaeObject.paintComponent(bufferGraphics, isObjectsOnSightBlinking); } for (Active active : actives) { active.paintComponent(bufferGraphics, isObjectsOnSightBlinking); } g.drawImage(offscreen, 0, 0, this); }
public Surface getSurfaceUnderVivaeObject(VivaeObject actor) { Vector<Surface> surfacesActorIsOn = new Vector<Surface>(); Surface srfc = null; for (Surface surface : surfaces) { srfc = surface; Area actArea = actor.getArea(); actArea.intersect(srfc.getArea()); if (!actArea.isEmpty()) { surfacesActorIsOn.add(srfc); } } if (surfacesActorIsOn.isEmpty()) { return null; } srfc = surfacesActorIsOn.get(surfacesActorIsOn.size() - 1); return srfc; }
/** * Returns a coefficient of the surface the VivaeObject is on or 0 if there is no surface. * * @param actor */ public float getFrictionOfSurface(VivaeObject actor) { Vector<Surface> surfacesActorIsOn = new Vector<Surface>(); Surface srfc; for (Surface surface : surfaces) { srfc = surface; Area actArea = actor.getArea(); actArea.intersect(srfc.getArea()); if (!actArea.isEmpty()) { surfacesActorIsOn.add(srfc); } } if (surfacesActorIsOn.isEmpty()) { return 0f; } srfc = surfacesActorIsOn.get(surfacesActorIsOn.size() - 1); return srfc.getFriction(); }
/** * Returns a coefficient of the surface the VivaeObject is on or 0 if there is no surface. * * @param actor */ public float getFrictionOfSurface(VivaeObject actor) { ArrayList<Surface> surfacesActorIsOn = new ArrayList<Surface>(); Surface srfc; Area actArea = actor.getArea(); for (Surface surface : surfaces) { srfc = surface; Area actor2intersect = (Area) actArea.clone(); actor2intersect.intersect(srfc.getArea()); if (!actor2intersect.isEmpty()) { surfacesActorIsOn.add(srfc); } } if (surfacesActorIsOn.isEmpty()) { return 0f; } srfc = surfacesActorIsOn.get(surfacesActorIsOn.size() - 1); float res = srfc.getFriction(); // System.out.println("Surface = " + srfc.getClass().toString() + " has friction " + // res); return res; }