Hello I am making a game in java that requires collision from a ball into a rectangle.
pos.x = x value of ball
pos.y = y value of ball
vel.x = x velocity of ball
vel.y = y velocity of ball
sh = height of ball
sw = width of ball
W = width of rect
H = height of rect
So far I have :
boolean insideRect = pos.x >= X && pos.x <= X+W && pos.y >= Y && pos.y <= Y+H;
if(insideRect){
if(pos.y + sh/2 >= H){
vel.y = (-1*vel.y)*gravity;
vel.x *= .6;
if(pos.y < Y+H/2){
pos.y = Y;
} else{
pos.y = Y+H;
}
}
if(pos.x + sw/2 > W){
vel.x = -1*vel.x;
}
}
However this just says if it hit the left side of the rect or right, and if it hit the top or bottom.
So an output if you printed under the if statements would be : (left, up), (right, down), (left, down), (right, up)
So the problem is that for this to work I can only have one, either left, right, up, or down.
If I have two, then it thinks that it both hit the ceiling and the right wall, and has two visual outputs do to it.
How can I work around this?
/**
* checks to see if the obstacles and balls collide.
* Utilizes bounding boxes for obstacles and coordinates for ball:
* + = Obstacle
* ballTop
* : :
* : :
*ballLeft : : ballRight
* : :
* : :
* ballBottom
*
* ____
* |+++|
* |+++|
* |+++|
* ----
* #param ball
* #param rect
* #return boolean
*/
public boolean intersects(Ball ball, Obstacle rect){
double r = ball.getRadius();
double rectTop = rect.y;
double rectLeft = rect.x;
double rectRight = rect.x + rect.width;
double rectBottom = rect.y + rect.height;
double ballTop = ball.y - r;
double ballLeft = ball.x - r;
double ballRight = ball.x + r;
double ballBottom = ball.y + r;
//NO COLLISIONS PRESENT
if(ballRight < rectLeft){
return false;
}
if(ballTop > rectBottom){
return false;
}
if(ballBottom < rectTop){
return false;
}
if(ballLeft > rectRight){
return false;
}
//HAS TO BE A COLLISION
return true;
}
//In Logic File
private void collideBallObstacles(Ball ball,Obstacle rect){
if(rect.intersects(ball, rect)){
ball.velX *= -1;
ball.velY *= -1;
if (ball == player){
flashGreen();
obstaclesevaded--;
}
obstacles.remove(rect);
}
}
You will also have to render this, and change the variables or create ones that fit with this code.
Just curious-what would the name of this game happen to be?
I found a practice java class and I am trying to get all points on the line segment such that for every point x and y are both integers but I am having trouble of implementing it. I am just looking for a formula I can use to solve the problem. or explain me how it get point (3,2). Thanks
/**
*
* #return an array containing all points on the line segment such that for every point in the array,
* both x and y are integers
* For example, if there is a line from (1,1) to (5,3), the only other point for which both x and y are
* integers is (3,2). Thus the method should return the array containing (1,1), (3,2) and (5,3)
* The order of points in the array returned should be from the point with lower x
* (use Point a as the starting point in case a.x == b.x).
*
* Hence if a = (5,1) and b=(1,3), the method returns an array such that
* first item is (1,3), second is (3,2) and third is (5,1)
*/
public Point[] getPointsOnCrosshair() {
Point[] points = new Point[20];
int counter = 0;
//getting slope of the line
double rise = this.b.y - this.a.y;
double run = this.b.x - this.a.x;
double m = rise/run;
System.out.println(m);
//getting value of c -> y = mx + c
double c = a.y - (m * a.x);
int start = 0;
int end = 0;
if (b.x >= a.x){
start = a.x;
end = b.x;
}
else{
start = b.x;
end = a.x;
}
// slope intercept y - y1 = m (x-x1)
// y = (m (x-x1)) + y1
double y = 0;
for (int a = start; a <= end; a++){
y = (m * a) + c;
if (y == (int) y){
points[counter] = new Point(a, (int) y);
counter++;
}
}
return points;
}
First, determine if the line is mostly horizontal or mostly vertical.
▉ ▉ ▉
▉ ▉ ▉
▉ ▉ ▉
▉
▉
▉
If it is mostly horizontal, you iterate x from a.x to b.x and calculate y.
If it is mostly vertical, you iterate y from a.y to b.y and calculate x.
I'll leave it to you to build the right formula, or to go find it on the web, i.e. do some research.
Brute force solution is really simple:
int p1x = 1;
int p1y = 1;
int p2x = 5;
int p2y = 3;
int xdiff = p2x - p1x;
int ydiff = p2y - p1y;
for (int x = p1x + 1; x < p2x; ++x) {
float y = ((float) x - p1x) / xdiff * ydiff + p1y;
if (((int) y) == y) { // check if xx.0
System.out.print(x);
System.out.print(", ");
System.out.println((int)y);
}
}
But I'm sure there's a more elegant way, maybe using prime factorization
So I am working on a blackjack game, I have wrote a render process which will render a card going out of the cards stack and sliding to the place where it shows all dealer's cards.
My method works fine, except one problem which I will elaborate:
Whenever Y coordinate reaches the target Y coordinate first, the sprite will only move on X-asis because it cant move Y anymore, instead of making a straight angle to the point.
So what it will do is move up diagonally and then instantly go to the right (in my case)
GIF:
(source: gyazo.com)
MP4 (choose mp4 in the (..) menu http://gyazo.com/bec6daadcb46bedc4777a3e4c5ff8c77)
As you can see, it does what I just said.
What did I do wrong? how can I make it motion in a straight angle to the target without going diagonal up and than turn right away?
My process code:
// If the target is on the right side of the start point
if (startPoint.getX() < endPoint.getX()) {
if (current.getX() < endPoint.getX()) {
current.x += moveSpeed;
if (current.getX() > endPoint.getX()) {
current.x = (int) endPoint.getX();
}
}
else {
xDone = true;
}
}
else {
if (current.getX() > endPoint.getX()) {
current.x -= moveSpeed;
if (current.getX() < endPoint.getX()) {
current.x = (int) endPoint.getX();
}
}
else {
xDone = true;
}
}
// Vise-versa
if (startPoint.getY() < endPoint.getY()) {
if (current.getY() < endPoint.getY()) {
current.y += moveSpeed;
if (current.getY() > endPoint.getY()) {
current.y = (int) endPoint.getY();
}
}
else {
yDone = true;
}
}
else {
if (current.getY() > endPoint.getY()) {
current.y -= moveSpeed;
if (current.getY() < endPoint.getY()) {
current.y = (int) endPoint.getY();
}
}
else {
yDone = true;
}
}
// Callback, dw about it
CardContainer.getCardSprite(CardContainer.SPECIAL, 0).drawSprite((int) current.getX(), (int) current.getY());
// Alert finished, in poisiuton
if (xDone && yDone) {
ch.submitCard(card);
}
current = current position
startPoint = the start point
endPoint = the end point
Thanks!
EDited code:
private void applyMovement(double alpha) {
double dx = endPoint.getX() - startPoint.getX();
double dy = endPoint.getY() - startPoint.getY();
this.current.setLocation(startPoint.getX() + alpha * dx, startPoint.getY() + alpha * dy);
}
public void process() {
double alpha = (double) stepsDone / distance;
applyMovement(alpha);
stepsDone++;
// Callback, dw about it
CardContainer.getCardSprite(CardContainer.SPECIAL, 0).drawSprite((int) current.getX(), (int) current.getY());
// Alert finished, in poisiuton
if (stepsDone >= distance) {
ch.submitCard(card);
}
}
Distance calculation:
this.distance = (int) start.distance(end);
Used Point2D distance method:
public double distance(Point2D pt) {
double px = pt.getX() - this.getX();
double py = pt.getY() - this.getY();
return Math.sqrt(px * px + py * py);
}
I would recommend to not use any form of "slope" in such a computation. You will run into problems when the difference in x-direction approaches zero, because then the slope will tend towards infinity.
Assuming that your points are Point2D.Double (or something similar - you should include this kind of information in your questions!), you can compute the movement as follows:
private Point2D.Double initial = ... // The initial position
private Point2D.Double current = ... // The current position
private Point2D.Double target = ... // The target position
void applyMovment(double alpha) {
double dx = target.getX() - initial.getX();
double dy = target.getY() - initial.getY();
current.x = initial.getX() + alpha * dx;
current.y = initial.getY() + alpha * dy;
}
The applyMovment method sketched here can be called with a double value between 0.0 and 1.0, where 0.0 corresponds to the initial position and 1.0 corresponds to the target position. This is just a Linear Interpolation.
So for example, when you have some sort of loop for the animation, you can use the method as follows:
int numberOfSteps = 10;
for (int i=0; i<=numberOfSteps; i++)
{
double alpha = (double)i / numberOfSteps;
applyMovement(alpha);
repaint();
}
This works for any arrangement of the start- and end points, without any sign- or direction issues. It just interpolates between the two positions.
Your calculation needs to be based upon moving the currentY and currentX along a specific line, not a specific set of intervals (moveSpeed). The formula for graphing points on a line is:
y = mx + b
Where x and y are the varying points, m is equal to the slope of the line, and b is what's called the y-intercept.
Your slope is calculated by the formula:
double slope = ((double) endPoint.getY() - startPoint.getY()) / ((double) endPoint.getX() - startPoint.getX());
And the Y intercept can be calculated by just plugging in a bunch of known values once you have them:
double yIntercept = (double) endPoint.getY() - (slope * endPoint.getX())
Then, just loop through the count of the difference in X:
for (int xVal = startPoint.getX(); xVal < endPoint.getX(); xVal++){
currentX = xVal;
currentY = (slope * xVal) + yIntercept;
}
And you should be good.
Warning: this is all off of the top of my head, I don't know if it'll compile.
I have been searching the internet for about a day and I can not seem to find an algorithm to find all points within a bounding box.
Picture:
The accuracy also needs to be 8 decimal digits long.
Example:
(0,0)
(0,0.00000001)
(0,0.00000002)
etc..
(80,80.45356433)
I don't know what exactly you mean by finding all points inside a box. There is just too many!
I would suggest using an Identifier function, i.e., given a point (x, y), I(x, y) = 1 if and only if the point (x, y) is inside your box.
A call to the identifier in your example requires $4$ comparisons.
public class Box {
/**
* Identifier function.
* #param x x-coordinate of input point
* #param y y-coordinate of input point
* #return True if and only if the given point is inside the box.
*/
public boolean IsInside(double x, double y) {
return (this.x_min <= x) && (this.x_max >= x) &&
(this.y_max >= y) && (this.y_min <= y);
}
double x_min, x_max, y_min, y_max; // or do it with left top width and height
}
I hope it helps.
long p = (long)Math.pow(10, 8);
long start = 90 * p;
long end = 180 * p;
for (long i = start; i < end; i++) {
for (long j = start; j < end; j++) {
System.out.println("(" + (double)i / p + "," + (double)j / p + ")");
}
}
will take a while
Alright, so I am using Bukkit API (Minecraft), which should not be too much of a problem in this as it is used minimally. So a Location contains world, x, y, z, yaw and pitch. This may come in handy, but I doubt it.
My problem is that I go to shoot using the Shot class (below), and there appears to be a +-5 difference in either one when approximately 3 blocks away, and the HitBox is instantiated about 5 blocks away (this could be the issue (move/rotate methods)). I have tried working this out on paper, and have used half a notepad doing so, but I am not yet able to figure out a solution. What I need is someone who understands trigonometry and java well, so they can help out.
Other information that may be of use:
+z is 0 degrees yaw, -x is 90 degrees, -z is 180, and +x is 270.
The variables seem to be incorrect sporadically, and under certain circumstances they work correctly and constitute a hit.
The Location (from) parameter in the Shot constructor is of the player's location in the world, therefore from is not (0, 0, 0).
ShotData should not affect any values, as wind speed and wind direction in my case are 0 (if there is a problem with the math involving this though, feel free to let me know, haha)
Pitch appears to be fine, although +y is -90, and -y is 90 (weird right?)
So my question is... Where is the problem, and how do I fix it? I am sorry that it is such a general question and that it is a very common one, but this is one of those times when it is truly necessary. I tried to remove all unnecessary code, but you can remove more if needed. Also, if you want to see anything else that may be referenced in here, I can get that for you.
Shot.java:
private final Location from;
private ShotData data;
public Shot(Location from, ShotData data) {
this.from = from;
this.data = data;
}
// TODO - Checking for obstacles
public List<Hit> shoot(List<HitBox> hitBoxes) {
List<Hit> hits = new ArrayList<Hit>();
for (HitBox hitBox : hitBoxes) {
hitBox.update();
float fromYaw = from.getYaw() % 360;
float fromPitch = from.getPitch() % 360;
// making sure the center location is within range
if (hitBox.getCenter().distanceSquared(from) > Math.pow(data.getDistanceToTravel(), 2)) {
continue;
}
/* TODO Only allow hits on parts of the rectangle that are within range,
* not just the whole thing if the center is within range. */
// accounting for wind speed/direction
float windCompassDirection = data.getWindCompassDirection(from.getWorld());
float windSpeed = data.getWindSpeedMPH(from.getWorld());
fromYaw += (windCompassDirection > fromYaw ? 1 : windCompassDirection < fromYaw ? -1 : 0) * windSpeed;
fromYaw %= 360;
int[] orderClockwise = new int[] {0, 1, 4, 3};
Location thisSideCorner = hitBox.getCorner(0);
Location oppositeSideCorner = hitBox.getCorner(0);
for (int i = 0; i < orderClockwise.length; i++) {
int num = orderClockwise[i];
Location corner = hitBox.getCorner(num);
Location clockwise = hitBox.getCorner(orderClockwise[(i + 1) % 3]);
if ((Math.atan2(from.getZ() - corner.getZ(), from.getX() - corner.getX()) * 180 / Math.PI) > 0 && corner.distanceSquared(from) < clockwise.distanceSquared(from)) {
thisSideCorner = corner;
int exitCornerClockwiseAmount = (Math.atan2(from.getZ() - clockwise.getZ(), from.getX() - clockwise.getX()) * 180 / Math.PI) < 0 ? 2 : 3;
oppositeSideCorner = hitBox.getCorner((i + exitCornerClockwiseAmount) % 3);
}
}
Location entrance = getProjectileLocation(thisSideCorner, data, hitBox, fromYaw, fromPitch);
double distance = entrance.distance(from);
double deltaX = data.getDeltaX(distance, fromYaw);
double deltaY = data.getDeltaY(distance, fromPitch);
double deltaZ = data.getDeltaZ(distance, fromYaw);
entrance.add(deltaX, deltaY, deltaZ);
Location exit = getProjectileLocation(oppositeSideCorner, data, hitBox, deltaX, deltaY, deltaZ, fromYaw, fromPitch);
// hit detection and reaction
boolean hitX = entrance.getX() <= hitBox.getHighestX() && entrance.getX() >= hitBox.getLowestX();
boolean hitY = entrance.getY() <= hitBox.getHighestY() && entrance.getY() >= hitBox.getLowestY();
boolean hitZ = entrance.getZ() <= hitBox.getHighestZ() && entrance.getZ() >= hitBox.getLowestZ();
if (hitX && hitY && hitZ) {
hits.add(new Hit(from, entrance, exit, hitBox, data));
}
}
return hits;
}
private Location getProjectileLocation(Location thisSideCorner, ShotData data, HitBox hitBox, float fromYaw, float fromPitch) {
return getProjectileLocation(thisSideCorner, data, hitBox, 0, 0, 0, fromYaw, fromPitch);
}
private Location getProjectileLocation(Location thisSideCorner, ShotData data, HitBox hitBox, double addX, double addY, double addZ, float fromYaw, float fromPitch) {
double deltaFromToSideCornerX = thisSideCorner.getX() - from.getX();
double deltaFromToSideCornerY = thisSideCorner.getY() - from.getY();
double deltaFromToSideCornerZ = thisSideCorner.getZ() - from.getZ();
double xzDistFromSideCorner = Math.sqrt(Math.pow(deltaFromToSideCornerX, 2) + Math.pow(deltaFromToSideCornerZ, 2));
double yawToSideCorner = Math.atan2(deltaFromToSideCornerX, deltaFromToSideCornerZ) * 180 / Math.PI;// flipped x and z from normal
double theta1 = yawToSideCorner - fromYaw;
double theta2 = yawToSideCorner - theta1;
double outerAngle = 180 - yawToSideCorner - 90;// previously theta1
double outerAngleInShotCone = outerAngle + 90 + hitBox.getYawRotation();
double lastAngleInShotCone = 180 - theta1 - outerAngleInShotCone;
double xzDistanceFromHit = (xzDistFromSideCorner * Math.sin(Math.toRadians(outerAngleInShotCone))) / Math.sin(Math.toRadians(lastAngleInShotCone));
double deltaX = xzDistanceFromHit * Math.sin(Math.toRadians(theta2));// leaves out sin 90 because its just equal to 1...
double deltaZ = xzDistanceFromHit * Math.sin(Math.toRadians(90 - theta2));// leaves out sin 90 because its just equal to 1...
double xyzDistFromSideCorner = Math.sqrt(Math.pow(xzDistFromSideCorner, 2) + Math.pow(deltaFromToSideCornerY, 2));
double theta3 = Math.atan2(Math.abs(deltaFromToSideCornerY), xzDistFromSideCorner) * 180 / Math.PI;
double theta4 = Math.abs(fromPitch) - theta3;
double theta5 = 90 + theta3;
double theta6 = 180 - theta4 - theta5;
double hitDistance = (xyzDistFromSideCorner * Math.sin(Math.toRadians(theta5))) / Math.sin(Math.toRadians(theta6));
double deltaY = hitDistance * Math.sin(Math.toRadians(Math.abs(fromPitch)));// leaves out sin 90 because its just equal to 1...
if (deltaFromToSideCornerX < 0 && deltaX > 0) {
deltaX *= -1;
}
if (fromPitch > 0 && deltaY > 0) {// pitch in minecraft is backwards, normally it would be fromPitch < 0
deltaY *= -1;
}
if (deltaFromToSideCornerZ < 0 && deltaZ > 0) {
deltaZ *= -1;
}
Location hit = from.clone().add(deltaX + addX, deltaY + addY, deltaZ + addZ);
hit.setYaw(fromYaw);
hit.setPitch(fromPitch);
return hit;
}
HitBox.java:
private float yawRotation;
private double x, y, z;
private double[][] additions;
private Location center;
private Location[] corners = new Location[8];
private List<DataZone> dataZones = new ArrayList<DataZone>();
private UUID uuid = UUID.randomUUID();
//#formatter:off
/*
* O = origin
* X = x-axis
* Y = y-axis
* Z = z-axis
* C = center
*
* ---------------------
* / /|
* / / |
* Y-------------------- |
* | 90 | | 0 yaw
* | ^ | | /
* | | | |
* | | | | /
* | HEIGHT C | |
* | | | |/
* | | | Z
* | v | /
* | <---WIDTH---> |/<---LENGTH
* O-------------------X - - - - - - - - - -270 yaw
*/
/**
* An invisible box in the world that can be hit with a shot.
* Additionally, {#link DataZone} instances can be added to this,
* allowing for different damage and thickness on an area of the box.
*
* #param center The center of the hit box
* #param length The length (z axis) of the hit box
* #param width The width (x axis) of the hit box
* #param height The height (y axis) of the hit box
* #param yawRotation The rotation around the center of the origin (or any other point)
*/
public HitBox(Location center, double length, double width, double height, float yawRotation) {
corners[0] = center.clone().add(-1 * width / 2, -1 * height / 2, -1 * length / 2);
this.center = center;
this.x = width;
this.y = height;
this.z = length;
rotate(yawRotation);
}
//#formatter:on
public Location[] getCorners() {
return corners;
}
public Location getCorner(int corner) {
return corners[corner];
}
public Location getOrigin() {
return corners[0];
}
public void update() {};
public boolean isZoneOpen(DataZone zone) {
for (DataZone placed : dataZones) {
boolean Xs = overlap_1D(placed.xFrom, placed.xTo, zone.xFrom, zone.xTo);
boolean Ys = overlap_1D(placed.yFrom, placed.yTo, zone.yFrom, zone.yTo);
boolean Zs = overlap_1D(placed.zFrom, placed.zTo, zone.zFrom, zone.zTo);
if (Xs && Ys && Zs) {
return true;
}
}
return false;
}
public void rotate(float degrees) {
Location origin = corners[0];
this.yawRotation = (yawRotation + degrees) % 360;
additions = new double[][] { {0, 0, 0}, {x, 0, 0}, {0, y, 0}, {0, 0, z}, {x, 0, z}, {x, y, 0}, {x, y, z}, {0, y, z}};
for (int i = 0; i < 8; i++) {
double[] addition = additions[i];
double xPrime = center.getX() + (center.getX() - (origin.getX() + addition[0])) * Math.cos(Math.toRadians(yawRotation)) - (center.getZ() - (origin.getZ() + addition[2])) * Math.sin(Math.toRadians(yawRotation));
double zPrime = center.getZ() + (center.getX() - (origin.getX() + addition[0])) * Math.sin(Math.toRadians(yawRotation)) + (center.getZ() - (origin.getZ() + addition[2])) * Math.cos(Math.toRadians(yawRotation));
corners[i] = new Location(center.getWorld(), xPrime, origin.getY() + addition[1], zPrime, yawRotation, 0);
}
}
public void move(Location center) {
double deltaX = center.getX() - this.center.getX();
double deltaY = center.getY() - this.center.getY();
double deltaZ = center.getZ() - this.center.getZ();
for (int i = 0; i < 8; i++) {
corners[i].add(deltaX, deltaY, deltaZ);
}
this.center = center;
}
protected void setY(double y) {
int[] toChange = new int[] {2, 5, 6, 7};
for (int i : toChange) {
corners[i].setY(corners[0].getY() + y);
}
this.y = y;
}
public double getHighestX() {
double highestX = Double.MIN_VALUE;
for (Location location : corners) {
if (location.getX() > highestX) {
highestX = location.getX();
}
}
return highestX;
}
public double getHighestY() {
return corners[0].getY() + y;
}
public double getHighestZ() {
double highestZ = Double.MIN_VALUE;
for (Location location : corners) {
if (location.getZ() > highestZ) {
highestZ = location.getZ();
}
}
return highestZ;
}
public double getLowestX() {
double lowestX = Double.MAX_VALUE;
for (Location location : corners) {
if (location.getX() < lowestX) {
lowestX = location.getX();
}
}
return lowestX;
}
public double getLowestY() {
return corners[0].getY();
}
public double getLowestZ() {
double lowestZ = Double.MAX_VALUE;
for (Location location : corners) {
if (location.getZ() < lowestZ) {
lowestZ = location.getZ();
}
}
return lowestZ;
}
public float getYawRotation() {
return yawRotation;
}
Perhaps consider drawing a line following the same vector that your bullet travels along, this will provide a visual indicator for what is happening, pass in the same calculations and etc.
As other have mentioned also include lots of debug printouts. Hopefully once you have a visual cue you can see when/where the problem calculations are occuring.
Also you should aim to use a standard data type for calculations, a float or a double, NOT both as this can cause some weird rounding and calculation problems.
I know this is extremely late (almost 5 years in fact), but I developed a solution a few weeks after this question. After revisiting StackOverflow I decided to provide my solution for any who may find it useful.
The issue with the massive wall of code found in the question is that many values are being computed and every computation loses precision, resulting in some degree of variation (like I said, +/-5 blocks).
The solution source may be found here:
https://github.com/JamesNorris/MCShot
To compute intersect (found in util/Plane3D.java):
public Vector getIntersect(LineSegment3D segment) {
Vector u = segment.getEnd().subtract(segment.getStart());
Vector w = segment.getStart().subtract(point);
double D = normal.dot(u);
double N = -normal.dot(w);
if (Math.abs(D) < .00000001) {
/* if N == 0, segment lies in the plane */
return null;
}
double sI = N / D;
if (sI < 0 || sI > 1) {
return null;
}
return segment.getStart().add(u.multiply(sI));
}
As you can see, this requires way fewer computations and as a result provides much greater precision. This function gets the intersection of a 3D plane. A 3D plane is constructed using the values:
point - some point found within the plane normal - the normal
vector of the plane
I hope someone finds this solution helpful, or at least can use it as a lesson in computational precision!