home / homework help / questions and answers / engineering / computer science /
ID: 649510 • Letter: H
Question
home / homework help / questions and answers / engineering / computer science / this potd can be used as a component in your joust ...
Your question has been posted.
We'll notify you when a Chegg Expert has answered. Post another question.
Question
Edit question
This POTD can be used as a component in your Joust game - all the methods here can be useful for detecting and resolving collisions between moving objects, though you might not need to use them all in Joust.
Java has a built in rectangle class that has methods that tell you if two rectangles are intersecting, and can give you the intersecting rectangle of those two rectangles, shown below:
In the image above, A. intersects(B) is true and A.intersection(B) is the green rectangle.
Please implement the following API in your class:
+ CollisionBox(Rectangle rect)
+ CollisionBox(x : int, y : int, width : int, height : int)
+ getRectangle() : Rectangle
+ collidesWith(other : CollisionBox) : boolean
+ moveTo(x : int, y : int) : void
+ moveCenterTo(x : int, y : int) : void
+ isHigherThan(other : CollisionBox) : boolean
+ isLeftOf(other : CollisionBox) : boolean
+ verticalDifference(other : CollisionBox) : int
+ horizontalDifference(other : CollisionBox) : int
+ isSmallerOverlapVertical(other : CollisionBox) : boolean
(+ = public, - = private)
Constructors
Both constructors should initialize the rect field to an appropriate rectangle - either the parameter passed in, or a rectangle created from the parameters passed in.
collidesWith(CollisionBox other)
This method should return a boolean indicating whether or not this CollisionBox
Collision Box ? rect : Rectangle+ CollisionBox(Rectangle rect)
+ CollisionBox(x : int, y : int, width : int, height : int)
+ getRectangle() : Rectangle
+ collidesWith(other : CollisionBox) : boolean
+ moveTo(x : int, y : int) : void
+ moveCenterTo(x : int, y : int) : void
+ isHigherThan(other : CollisionBox) : boolean
+ isLeftOf(other : CollisionBox) : boolean
+ verticalDifference(other : CollisionBox) : int
+ horizontalDifference(other : CollisionBox) : int
+ isSmallerOverlapVertical(other : CollisionBox) : boolean
(+ = public, - = private)
Constructors
Both constructors should initialize the rect field to an appropriate rectangle - either the parameter passed in, or a rectangle created from the parameters passed in.
collidesWith(CollisionBox other)
This method should return a boolean indicating whether or not this CollisionBox
home / homework help / questions and answers / engineering / computer science / this potd can be used as a component in your joust ... Your question has been posted. We'll notify you when a Chegg Expert has answered. Post another question. Question Edit question This POTD can be used as a component in your Joust game - all the methods here can be useful for detecting and resolving collisions between moving objects, though you might not need to use them all in Joust. Java has a built in rectangle class that has methods that tell you if two rectangles are intersecting, and can give you the intersecting rectangle of those two rectangles, shown below: In the image above, A. intersects(B) is true and A.intersection(B) is the green rectangle. Please implement the following API in your class: Collision Box ? rect : Rectangle + CollisionBox(Rectangle rect) + CollisionBox(x : int, y : int, width : int, height : int) + getRectangle() : Rectangle + collidesWith(other : CollisionBox) : boolean + moveTo(x : int, y : int) : void + moveCenterTo(x : int, y : int) : void + isHigherThan(other : CollisionBox) : boolean + isLeftOf(other : CollisionBox) : boolean + verticalDifference(other : CollisionBox) : int + horizontalDifference(other : CollisionBox) : int + isSmallerOverlapVertical(other : CollisionBox) : boolean (+ = public, - = private) Constructors Both constructors should initialize the rect field to an appropriate rectangle - either the parameter passed in, or a rectangle created from the parameters passed in. collidesWith(CollisionBox other) This method should return a boolean indicating whether or not this CollisionBox?????s rectangle intersects with other?????s rectangle. moveTo(int x, int y) This method should move the cornder of CollisionBox to the given parameters. moveCenterTo(int x, int y) This method should move the center of CollisionBox to the given parameters. If the CollisionBox has odd-number width or height, you may round either way. isHigherThan(CollisionBox other) This method should return a boolean indicating whether or not this CollisionBox is higher on the screen (smaller y) than other. Compare the center of the rectangles, not a corner. isLeftOf(CollisionBox other) This method should return a boolean indicating whether or not this CollisionBox is to the left of other on the screen (smaller x). Compare the center of the rectangles, not a corner. verticalDifference(CollisionBox other) This method should return the minimal y distance one of the two boxes would need to move in order for them not to overlap anymore. That will be the difference between the minimal y of one box and the maximal y of the other. You may assume this method is only called on boxes that do overlap. horizontalDifference(CollisionBox other) This method should return the minimal x distance one of the two boxes would need to move in order for them not to overlap anymore. That will be the difference between the minimal x of one box and the maximal x of the other. You may assume this method is only called on boxes that do overlap. isSmallerOverlapVertical(CollisionBox other) This method should return a boolean indicating whether or not the vertical difference between these two CollisionBox objects is smaller than the horizontal difference. You may assume this method is only called on boxes that do overlap. A simple solution includes invoking the horizontalDifference and verticalDifference methods. Example CollisionBox box1 = new CollisionBox(new Rectangle(1, 1, 3, 3)); CollisionBox box2 = new CollisionBox(2, 2, 1, 3); CollisionBox box3 = new CollisionBox(0, 0, 5, 2); box1.collidesWith(box2);// should return true box1.collidesWith(box3);// should return true. box2.collidesWith(box3);// should return false. box1.isSmallerOverlapVertical(box2); // should return false. box1.isSmallerOverlapVertical(box3);// should return true. box2.moveTo(3, 2); box1.isHigherThan(box2);// should return true. box1.verticalDifference(box2); // should return 2. box2.verticalDifference(box1); // should return 2. box2.isLeftOf(box1); // should return false. box1.horizontalDifference(box2); // should return 1. box2.horizontalDifference(box1); // should return 1.Explanation / Answer
Program code:
//CollisionBoxImplementaion.java
import java.awt.Rectangle;
//implementation class of the CollisionBox class
public class CollisionBoxImplementaion
{
// main method
public static void main(String args[])
{
CollisionBox box1 = new CollisionBox(new Rectangle(1, 1, 3, 3));
System.out.println("Box 1 is present at: ");
display(box1);
CollisionBox box2 = new CollisionBox(2, 2, 1, 3);
System.out.println("Box 2 is present at: ");
display(box2);
CollisionBox box3 = new CollisionBox(0, 0, 5, 2);
System.out.println("Box 3 is present at: "); display(box3);
System.out.println();
if (box1.collidesWith(box2))
System.out.println("Box1 collides with box2.");// should return
// true
else
System.out.println("Box1 does not collide with box2.");
if (box1.collidesWith(box3))// should return true.
System.out.println("Box1 collides with box3");
else
System.out.println("Box1 does not collide with box3");
if (box2.collidesWith(box3))// should return false.
System.out.println("Box2 collides with box3.");
else
System.out.println("Box2 does not collide with box3.");
if (box1.isSmallerOverlapVertical(box2)) // should return false.
System.out.println("Box1 contains the smaller overlap value over the box2.");
else
System.out.println("Box1 contains the larger overlap value over the box2.");
if (box1.isSmallerOverlapVertical(box3))// should return true.
System.out.println("Box1 contains the smaller overlap value over the box3.");
else
System.out.println("Box1 contains the larger overlap value over the box3.");
System.out.println();
box2.moveTo(3, 2);
System.out.println("Box2 is moved to new position(3,2).");
System.out.println("The coordinates of box2 are: ");
display(box2);
if (box1.isHigherThan(box2))// should return true.
System.out.println("Box1 is on top of the box2.");
else
System.out.println("Box1 is not on top of the box2.");
int distance = box1.verticalDifference(box2); // should return 2.
// box2.verticalDifference(box1);
// // should return 2.
// box2.isLeftOf(box1);
// // should return
// false.
// box1.horizontalDifference(box2);
// // should return 1.
// box2.horizontalDifference(box1);
// // should return 1.
System.out.println("Box1 should move at most " + distance
+ " units distance in order not to overlap.");
}
public static void display(CollisionBox box)
{
System.out.println("x = " + (int) box.getRectangle().getX());
System.out.println("y = " + (int) box.getRectangle().getY());
System.out.println("width = " + (int) box.getRectangle().getWidth());
System.out.println("height = " + (int) box.getRectangle().getHeight());
System.out.println();
}
}
//CollisionBox.java
import java.awt.Rectangle;
//definition of CollisionBox by defining the given API methods
public class CollisionBox
{
private Rectangle rect;
// Constructor
public CollisionBox(Rectangle rect)
{
this.rect = rect;
}
// Parameterized constructor with dimensions
public CollisionBox(int x, int y, int width, int height)
{
rect = new Rectangle(x, y, width, height);
}
public Rectangle getRectangle()
{
return rect;
}
// collidesWith method checks whether the two rectangles
// intersect with each other or not
public boolean collidesWith(CollisionBox other)
{
boolean flag = this.getRectangle().intersects(other.getRectangle());
return flag;
}
// moveTo method moves the position of the rectangle
public void moveTo(int x, int y)
{
rect.setLocation(x, y);
}
// moveCenterTo method moves the current rectangle to its new coordinates
public void moveCenterTo(int x, int y)
{
rect.setLocation(x - (int) (rect.getWidth() / 2), y - (int) (rect.getHeight() / 2));
}
// isHigherThan method this will check present box is higher than other box
public boolean isHigherThan(CollisionBox other)
{
int Cy = (int) rect.getY() + (int) (rect.getHeight() / 2);
int otherCy = (int) other.rect.getY() + (int) (other.rect.getHeight() / 2);
if (Cy < otherCy)
return true;
else
return false;
}
// isLeftOf method this will check present box is left side of other box
public boolean isLeftOf(CollisionBox other)
{
int Cx = (int) rect.getX() + (int) (rect.getWidth() / 2);
int otherCx = (int) other.rect.getX() + (int) (other.rect.getWidth() / 2);
if (Cx < otherCx)
return true;
else
return false;
}
// verticalDifference method return the vertical distance of which
// one rectangle can be moved so that they wont overlap with each other
public int verticalDifference(CollisionBox other)
{
int minimaly = 0;
int min, max;
if (this.collidesWith(other))
{
min = (int) rect.getY();
max = (int) other.rect.getHeight();
minimaly = Math.abs(min - max);
}
return minimaly;
}
// horizontalDifference method return the horizontal distance of which
// one rectangle can be moved so that they wont overlap with each other
public int horizontalDifference(CollisionBox other)
{
int minimalx = 0;
int min, max;
if (this.collidesWith(other))
{
min = (int) rect.getX();
max = (int) other.rect.getY() + (int) other.rect.getWidth();
minimalx = Math.abs(min - max);
}
return minimalx;
}
// isSmallerOverlapVertical method checks whether the vertical difference
// is
// smaller than horizontal differences
public boolean isSmallerOverlapVertical(CollisionBox other)
{
boolean flag = false;
int vertical;
int horizontal;
if (this.collidesWith(other))
{
vertical = this.verticalDifference(other);
horizontal = this.horizontalDifference(other);
if (vertical < horizontal)
flag = true;
else
flag = false;
}
return flag;
}
}
Sample Output:
Box 1 is present at:
x = 1
y = 1
width = 3
height = 3
Box 2 is present at:
x = 2
y = 2
width = 1
height = 3
Box 3 is present at:
x = 0
y = 0
width = 5
height = 2
Box1 collides with box2.
Box1 collides with box3
Box2 does not collide with box3.
Box1 contains the larger overlap value over the box2.
Box1 contains the smaller overlap value over the box3.
Box2 is moved to new position(3,2).
The coordinates of box2 are:
x = 3
y = 2
width = 1
height = 3
Box1 is on top of the box2.
Box1 should move at most 2 units distance in order not to overlap.
Note:
The comments that are provided in the question are not removed if required you can modify them.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.