public void collisionOccured(CollisionEvent event) { Body inputBody = null; if (event.getBodyA() == in.getBody()) { inputBody = event.getBodyB(); } else if (event.getBodyB() == in.getBody()) { inputBody = event.getBodyA(); } if (inputBody == null) return; in.handleWorldInput((KObject) inputBody.getUserData()); }
public void drawBoxBody(Graphics g, Body body, Box box) { Vector2f[] pts = box.getPoints(body.getPosition(), body.getRotation()); Vector2f v1 = pts[0]; Vector2f v2 = pts[1]; Vector2f v3 = pts[2]; Vector2f v4 = pts[3]; g.translate(camX, camY); g.setColor(new Color(255, 255, 255)); g.drawLine((int) v1.x, (int) v1.y, (int) v2.x, (int) v2.y); g.drawLine((int) v2.x, (int) v2.y, (int) v3.x, (int) v3.y); g.drawLine((int) v3.x, (int) v3.y, (int) v4.x, (int) v4.y); g.drawLine((int) v4.x, (int) v4.y, (int) v1.x, (int) v1.y); g.translate(-camX, -camY); }
/** * What happens with the widget when another widget touches it. * * @param other_body The body that touched it. * @param f The x,y coordinate that is the contact point. */ public void reactToTouchingBody(CollisionEvent e) { float speed = MOVEMENT_SPEED + (MOVEMENT_SPEED * bounces * 0.05f); if (currentDirection == Direction.WEST) { currentDirection = direction.EAST; body.setForce(2 * speed, 0); } else if (currentDirection == Direction.EAST) { currentDirection = Direction.WEST; body.setForce(-2 * speed, 0); } else if (currentDirection == Direction.NORTH) { currentDirection = Direction.SOUTH; body.setForce(0, 2 * speed); } else if (currentDirection == Direction.SOUTH) { currentDirection = direction.NORTH; body.setForce(0, -2 * speed); } bounces++; }
/** Activates the widget. */ public void activateWidget() { // Enforce that reset is called at least once before activation. if (!reset) { return; } // Make active. active = true; body.setEnabled(true); }
/** * Gets the boundary of the widget, should be used as a way to detect overlapping widgets. * * @return The four corners x,y coordinates. */ public Vector2f[] getBoundary() { Box box = (Box) body.getShape(); Vector2f[] bounds = new Vector2f[4]; bounds[0] = new Vector2f(position.x, position.y); bounds[1] = new Vector2f(position.x + WIDTH, position.y); bounds[2] = new Vector2f(position.x + WIDTH, position.y + HEIGHT); bounds[3] = new Vector2f(position.x, position.y + HEIGHT); return bounds; }
/** * 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<VivaeObject> closeObjects = new Vector<VivaeObject>(); int i = objects.indexOf(owner); int d = i + 1; while (d < objects.size() && objects.get(d).getBody().getPosition().getX() - ownerBody.getPosition().getX() < ray_length) { if (objects.get(d).getBoundingCircleRadius() + ray_length > objects.get(d).getBody().getPosition().distance(ownerBody.getPosition())) { closeObjects.add(objects.get(d)); } d++; } d = i - 1; while (d >= 0 && (ownerBody.getPosition().getX() - objects.get(d).getBody().getPosition().getX()) < ray_length) { if (objects.get(d).getBoundingCircleRadius() + ray_length > objects.get(d).getBody().getPosition().distance(ownerBody.getPosition())) { closeObjects.add(objects.get(d)); } d--; } // Vector<VivaeObject> closeObjects = new Vector<VivaeObject>(); // for (VivaeObject vivae : objects) { // if (vivae.getBoundingCircleRadius() + ray_length > // vivae.getBody().getPosition().distance(ownerBody.getPosition())) { // // closeObjects.add(vivae); // // } // } return closeObjects; }
@Override public void moveComponent() { inMotion = true; direction = owner.getDirection() - (float) Math.PI / 2; direction += angle; net.phys2d.math.ROVector2f op = ownerBody.getPosition(); x = op.getX(); y = op.getY(); float newX = (float) (x + (ray_length / 2) * Math.cos(direction)); float newY = (float) (y + (ray_length / 2) * Math.sin(direction)); body.setPosition(newX, newY); body.setRotation(direction); }
/** * Draws the widget. * * @param g The object that draws things. */ public void draw(Graphics2D screen) { // Animate! if (System.nanoTime() - timestamp >= CENTISECOND * 50) { timestamp = System.nanoTime(); if (image == frames.get(0)) { image = frames.get(1); } else { image = frames.get(0); } } ROVector2f position = body.getPosition(); float angle = body.getRotation(); boolean hflip = false; boolean vflip = false; if (currentDirection == Direction.WEST) { hflip = false; } else if (currentDirection == Direction.EAST) { hflip = true; } else if (currentDirection == Direction.NORTH) { angle += Math.PI / 2; vflip = false; } else if (currentDirection == Direction.SOUTH) { angle += Math.PI / 2; vflip = true; } drawImage( position.getX(), position.getY(), getWidth(), getHeight(), angle, hflip, vflip, image, screen); }
/** * 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; }
public void setPosition(float x, float y) { body.setPosition(x, y); }
public void drawBody(Graphics g, Body body) { if (body.getShape() instanceof Box) { drawBoxBody(g, body, (Box) body.getShape()); } }
/** * Get the position of the widget. * * @return The x,y coordinates within the container. */ public Vector2f getPosition() { return new Vector2f(body.getPosition()); }
/** * Retrieves the width of the widget based on the specified boundary. * * @return The width of the widget (based on the boundary). */ private float getWidth() { return ((Box) body.getShape()).getSize().getX(); }
/** Resets the widget to start state. */ public void resetWidget() { // Reset has been made, so we CAN activate this later. reset = true; // Make inactive active = false; // Not collided. collided = false; bounces = 0; currentDirection = direction; image = frames.get(0); body.setEnabled(false); // Move to initial position. body.set(shape, MASS); body.setPosition(position.x + WIDTH / 2, position.y + HEIGHT / 2); // Make sure the gameplay didn't mess with any initial properties. body.setCanRest(true); body.setDamping(0.0f); body.setFriction(0.01f); body.setGravityEffected(false); body.setIsResting(true); body.setMoveable(true); body.setRestitution(1.5f); body.setRotatable(true); body.setRotation(0.0f); body.setRotDamping(0.0f); body.setMaxVelocity(50f, 50f); body.setForce(-body.getForce().getX(), -body.getForce().getY()); if (currentDirection == Direction.WEST) { body.setForce(-MOVEMENT_SPEED, 0); } else if (currentDirection == Direction.EAST) { body.setForce(MOVEMENT_SPEED, 0); } if (currentDirection == Direction.NORTH) { body.setForce(0, -MOVEMENT_SPEED); } else if (currentDirection == Direction.SOUTH) { body.setForce(0, MOVEMENT_SPEED); } body.adjustVelocity(new Vector2f(-body.getVelocity().getX(), -body.getVelocity().getY())); body.adjustAngularVelocity(-body.getAngularVelocity()); }
/** * Retrieves the height of the widget based on the specified boundary. * * @return The height of the widget (based on the boundary). */ private float getHeight() { return ((Box) body.getShape()).getSize().getY(); }
public Point getPosition() { return new Point(body.getPosition()); }
/** * Set the position of the widget. * * @param f The x,y coordinates within the container. */ public void setPosition(Vector2f f) { body.setPosition(f.x + WIDTH / 2, f.y + HEIGHT / 2); position = f; }