Distance between coordinates - angles > 180 (Processing/Java) - java

The following is a Processing sketch.
Dead centre coordinates are 0,0.
This means the far-left x coord is -250 and the far right x coord is 250. Similar dimension for y.
I want to move the mouse around the centre of the screen and have the relative coordinate appear on the radius, (i.e. mouse at coordinates 45,0 should mark point at 90,0).
The below code works but only for the right side of the screen, in short, angles up to 180. What is missing for this to work for the right hand side?
void draw(){
background(0);
fill(255);
stroke(255);
strokeWeight(5);
translate(width/2,height/2);
// mark center
point(0,0);
strokeWeight(12);
// mark mouse position (follow with point)
float x = mouseX - width/2;
float y = mouseY - height/2;
point(x,y);
// trace point on radius of circle same radius as width
float radius = width/2;
float sinAng = y/radius;
float cosAng = x/radius;
float m = sinAng/cosAng;
float angle = atan(m);
float boundaryX = cos(angle)*width/2;
float boundaryY = sin(angle)*height/2;
stroke(255,0,0);
point(boundaryX,boundaryY);
}

You're loosing the quadrant when calculating m...
-x/-y = x/y
Just correct the angle to the right quadrant by using the sign of x and y.

You can do this in fewer steps by using the atan2() function.
The atan2() function takes two parameters: the y distance between two points, and the x distance between two points, and it returns the angle between those points:
angle = atan2(y1-y0, x1-x0);
You can get rid of a few lines in your program by just doing this:
float x = mouseX - width/2;
float y = mouseY - height/2;
float angle = atan2(y, x);
float boundaryX = cos(angle)*width/2;
float boundaryY = sin(angle)*height/2;
No need to calculate the sinAng, cosAng, or m variables.

Related

Java LibGDX relative value addition based on angle and position

I create a 2d game similar to a classic diepio. I created a system for positioning the player's barrel in a specific direction. The updated angle is sent to the server. When the player clicks, the server creates a missile. This only works correctly when the barrel is attached to the center of the player's body. When I want to move the barrel away from the center of the body, there is a problem. I don't know how to update the server-side position where the projectile spawns.
In the image below, the barrel rotates around the center of the player's body. I marked the missile's flight path with the red line.
On this image, the barrels also have a rotation axis in the player's center, but have been shifted to the side. The green line marked the route the missile should take. Unfortunately, I don't know how to do it correctly.
How to update the projectile's spawn point by a given distance (e.g. 10) from the basic distance (if the barrel was not moved) based on the player's angle of rotation and his position?
Projectile spawn method:
float angle = (float) ((player.getRotation() + 90) * Math.PI / 180f);
float forceX = (float) Math.cos(angle);
float forceY = (float) Math.sin(angle);
spawnProjectile(player.getPosition().x + (forceX * 3f), player.getPosition().y + (forceY * 3f));
If I understood your question correctly, you want to find the two points marked in orange in the following image:
Since you know the direction in which the missiles should fly (the red line), the distance from the center position (e.g. 10) and you know that there is a 90° angle between the movement vector of the missile (red line) and the connection line between the two starting positions of the missiles (marked as black line in the image) you can calculate the resulting positions like this:
float angle = (float) ((player.getRotation() + 90) * Math.PI / 180f);
float forceX = (float) Math.cos(angle);
float forceY = (float) Math.sin(angle);
// the center point (start of the red line in the image)
float centerX = player.getPosition().x + (forceX * 3f);
float centerY = player.getPosition().y + (forceY * 3f);
float offsetFromCenterDistanceFactor = 1f; // increase this value to increase the distance between the center of the player and the starting position of the missile
// the vector towards one of the starting positions
float offsetX1 = forceY * offsetFromCenterDistanceFactor;
float offsetY1 = -forceX * offsetFromCenterDistanceFactor;
// the vector towards the other starting position
float offsetX2 = -offsetX1;
float offsetY2 = -offsetY1;
//spawn the upper missile
spawnProjectile(centerX + offsetX1, centerY + offsetY1);
//spawn the lower missile
spawnProjectile(centerX + offsetX2, centerY + offsetY2);
For more detail on the calculation of the orthogonal vectors see this answer.

Java Swing - Calculating what angle the mouse position is to the center of the screen

I'm making a 2D topdown view shooter game with Java Swing. I want to calculate what angle the mouse pointer is compared to the center of the screen so some of my Sprites can look toward the pointer and so that I can create projectiles described by an angle and a speed. Additionally If the pointer is straight above the middle of the screen, I want my angle to be 0°, if straight to its right, 90°, if straight below 180°, and straight left 270°.
I have made a function to calculate this:
public static float calculateMouseToPlayerAngle(float x, float y){
float mouseX = (float) MouseInfo.getPointerInfo().getLocation().getX();
float mouseY = (float)MouseInfo.getPointerInfo().getLocation().getY();
float hypotenuse = (float) Point2D.distance(mouseX, mouseY, x, y);
return (float)(Math.acos(Math.abs(mouseY-y)/hypotenuse)*(180/Math.PI));
}
The idea behind it is that I calculate the length of the hypotenuse then the length of the side opposite of the angle in question. The fraction of the 2 should be a cos of my angle, so taking that result's arc cos then multiplying that by 180/Pi should give me the angle in degrees. This does work for above and to the right, but straight below returns 0 and straight left returns 90. That means that I currently have 2 problems where the domain of my output is only [0,90] instead of [0,360) and that it's mirrored through the y (height) axis. Where did I screw up?
You can do it like this.
For a window size of 500x500, top left being at point 0,0 and bottom right being at 500,500.
The tangent is the change in Y over the change in X of two points. Also known as the slope it is the ratio of the sin to cos of a specific angle. To find that angle, the arctan (Math.atan or Math.atan2) can be used. The second method takes two arguments and is used below.
BiFunction<Point2D, Point2D, Double> angle = (c,
m) -> (Math.toDegrees(Math.atan2(c.getY() - m.getY(),
c.getX() - m.getX())) + 270)%360;
BiFunction<Point2D, Point2D, Double> distance = (c,
m) -> Math.hypot(c.getY() - m.getY(),
c.getX() - m.getX());
int screenWidth = 500;
int screenHeight = 500;
int ctrY = screenHeight/2;
int ctrX = screenWidth/2;
Point2D center = new Point2D.Double(ctrX,ctrY );
Point2D mouse = new Point2D.Double(ctrX, ctrY-100);
double straightAbove = angle.apply(center, mouse);
System.out.println("StraightAbove: " + straightAbove);
mouse = new Point2D.Double(ctrX+100, ctrY);
double straightRight = angle.apply(center, mouse);
System.out.println("StraightRight: " + straightRight);
mouse = new Point2D.Double(ctrX, ctrY+100);
double straightBelow = angle.apply(center, mouse);
System.out.println("StraightBelow: " + straightBelow);
mouse = new Point2D.Double(ctrX-100, ctrY);
double straightLeft = angle.apply(center, mouse);
System.out.println("Straightleft: " + straightLeft);
prints
StraightAbove: 0.0
StraightRight: 90.0
StraightBelow: 180.0
Straightleft: 270.0
I converted the radian output from Math.atan2 to degrees. For your application it may be more convenient to leave them in radians.
Here is a similar Function to find the distance using Math.hypot
BiFunction<Point2D, Point2D, Double> distance = (c,m) ->
Math.hypot(c.getY() - m.getY(),
c.getX() - m.getX());

How to make a camera move in the direction it is facing in java?

I need a way(in java) to calculate which direction my camera will move forward on the x and z axes based on the direction my camera is facing(yaw), and for y(the vertical axis), the pitch. I'm making my own game engine and my own camera.
With all values defaulting at zero, moving the camera correctly directs all movement along the positive z axis. However, when I pan the camera to the left or right(thereby changing the yaw), the camera still only moves along the z axis... So how do I calculate the change in direction to the x, y, and z axes?
The value range on the yaw is 0(south), 45(southwest), 90(west), 135(northwest), 180(north), 225(northeast), 270(east), 315(southeast), and back to 360(south, same as 0).
What I'm looking for with the compass directions(where a '+' or '-' indicates a change in value along that axis):
South = x, y, z+
SouthWest = x+, y, z+
West = x+, y, z
NorthWest = x+, y, z-
North = x, y, z-
NorthEast = x-, y, z-
East = x-, y, z
SouthEast = x-, y, z+
The value range on the pitch is 0.0(middle), 100.0(up all the way), and -100.0(down all the way).
If I need to post some code, I can, but it might get complicated. I hope I'm making some kind of sense so that someone can help me!
Suppose you want to move the camera in direction y with distance k.
For the sake of simplicity, i will convert your directions to angles, where 0.0 represents right, Pi/2 represents up, Pi represents left, 3*Pi/2 represents down and so.
EDIT: As you say y of camera will be affected only by the pitch. X and z, instead will be affected by both the yaw and the pitch.
You can calculate the new x, y and z by the following:
float y = // Yaw angle
float p = // Pitch angle
float k = // Move distance
float xzLength = cos(p) * k;
float dx = xzLength * cos(y);
float dz = xzLength * sin(y);
float dy = k * sin(p);
// Update your camera:
camera.x += dx;
camera.y += dy;
camera.z += dz;
Obviously, yaw and pitch do not change, so you don't need to update them.

Move Object along a straight line

I am trying to move a Sprite along a straight line path. I want to move it 5 pixels on the slope, or the hypotenuse each time I go through the method until I reach the end point.
I have the slope and y-intercept of the line, I also have the current X and Y values of the sprite through getX() and getY(). The final X and Y points to stop at are variables finalX and finalY.
I have tried so many equations but I can't seem to get any of them to work. What am I missing!!?
My latest equation was trying to use y=mx+b.
float X = (getY() + 5 - interceptY)/slope;
float Y = slope*(getX() + 5) + interceptY;
setPosition(X, Y);
Can help you with a few equations from my recent game, the code moves an object given its rotation:
float xDirection = FloatMath.sin((float) Math.toRadians(getRotation()))
* currentSpeed;
float yDirection = FloatMath.cos((float) Math.toRadians(getRotation()))
* -currentSpeed;
float newX = getX() + xDirection;
float newY = getY() + yDirection;
You just need to derive the angle in which you need your sprite to move and this will do for you. Hope this helps.

Put points round into circular arc used in java

I have me math question: I have known a circle center and radius, and have some uncertain number of points called N, my question is how to put the points on the circular arc, I cannot like put the points around the whole circumference, other as this link: http://i.6.cn/cvbnm/2c/93/b8/05543abdd33b198146d473a43e1049e6.png
in this link, you can read point is circle center, other color is some points, you can see these points around the arc.
Edit - in short: I have known a circle center and radius, so I want to generate some point around the circle center
I am not sure, but I checked this with simple Swing JComponent and seems ok.
Point center = new Point(100, 100); // circle center
int n = 5; // N
int r = 20; // radius
for (int i = 0; i < n; i++)
{
double fi = 2*Math.PI*i/n;
double x = r*Math.sin(fi + Math.PI) + center.getX();
double y = r*Math.cos(fi + Math.PI) + center.getY();
//g2.draw(new Line2D.Double(x, y, x, y));
}
It's not entirely clear what you're trying to accomplish here. The general idea of most of it is fairly simple though. There are 2*Pi radians in a circle, so once you've decided what part of a circle you want to arrange your points over, you multiply that percentage by 2*pi, and divide that result by the number of points to get the angle (in radians) between the points.
To get from angular distances to positions, you take the cosine and sine of the angle, and multiply each by the radius of the circle to get the x and y coordinate of the point relative to the center of the circle. For this purpose, an angle of 0 radians goes directly to the right from the center, and angles progress counter-clockwise from there.

Categories

Resources