So I have a Java2D game when I have to rotate a cannon to the mouse's direction and save the angle. It works but I have the real issue. I can't calculate correct X and Y velocity from this answer i got from this equation:
double angle = Math.atan2(centerY - c.mouseY, centerX - c.mouseX) - Math.PI / 2;
then i use this equation:
int x = (int) Math.acos(c.rotation*Math.PI/180);
int y = (int) Math.sin(c.rotation*Math.PI/180);
I don't get the right velocity for y. can you guys help me?
Maybe because your using cosine when calculating y value. Shouldn't it be sine?
Related
I have a camera that has X, Y and Z Coordinates.
The camera also has a Yaw and a pitch.
int cameraX = Camera.getX();
int cameraY = Camera.getY();
int cameraZ = Camera.getZ();
int cameraYaw = Camera.getYaw();
int cameraPitch = Camera.getPitch();
The yaw has 2048 units in 360 degrees, so at 160 degrees the getYaw() method will return 1024.
Currently I move the camera forward by just setting the Y + 1 in each loop.
Camera.setY(Camera.getY() + 1);
How would I set the camera X and Y to the direction I'm facing (The Yaw)?
I don't want to use the pitch in this situation, just the Yaw.
If I understand your question correctly, you're trying to get the camera to move in the direction you're looking (in 2D space, you're only moving horizontally).
I made a small LookAt header-only library for C++, but here is part of it rewritten in Java. What this code does is it takes a rotation and a distance, then calculates how far you need to move (in both the x and y coordinate) to get there.
// Returns how far you need to move in X and Y to get to where you're looking
// Rotation is in degrees, distance is how far you want to move
public static double PolarToCartesianX(double rotation, double distance) {
return distance * Math.cos(rotation * (Math.PI / 180.0D));
}
public static double PolarToCartesianY(double rotation, double distance) {
return distance * Math.sin(rotation * (Math.PI / 180.0D));
}
I have an Android game where I need to draw an object based on coordinates chosen by the player. It is then sent to the opponent. I need to draw the object in the same position on the new screen. This is what I have right now
float x = originalX;
float y = originalY;
x *= myWidth / originalWidth;
y *= myHeight / originalHeight;
drawRect(x, y, x+width, y+width);
It doesn't seem to work. Any guesses?
I'm trying to find the angle of a circle(2d) where I intersect with another (2d)object
I have a spaceship and a planet, I know what the X and Y coordinates are of both.
Now i need to know what the angle/degree is of the planet WHERE the spaceship intersected with the planet.
Thanks in advance.
,Cheers
Ozcan
If the origin is at (0, 0), the angle (in radians) would be calculated like this:
int deltaX = planet.centerX - spaceship.centerX;
int deltaY = planet.centerY - spaceship.centerY;
double radians = Math.atan2(deltaY, deltaX);
I'm working on some AI stuff in Java. When an entity moves towards a point, the movement does not look natural. If its starting point and target are not equal X and Y distances away from each other, the movement looks like this:
What I want it to look move like is this:
Currently, movement is done like so:
int tX = target.x;
int tY = target.y;
if(tX> oX){
getOwner().addX(getOwner().getMoveSpeed());
}
if(tX < oX){
getOwner().addX(-getOwner().getMoveSpeed());
}
if(tY> oY){
getOwner().addY(getOwner().getMoveSpeed());
}
if(tY< oY){
getOwner().addY(-getOwner().getMoveSpeed());
}
I'm guessing that there is a much better method for handling movement than this.
So what I want to know is probably how to work out the angle I need to move along, and then the x and y velocitys needed to do so.
You just need to scale the x and y speeds according to the total distance to be traveled in each direction.
It will help to do the calculations in floating point and round to an int only when you need to assign a position.
int tX = target.x;
int tY = target.y;
float speed = getOwner.getMoveSpeed();
float dX = tX - oX;
float dY = tY - oY;
float dist = Math.sqrt(dX * dX + dY * dY);
float sX = speed * dX / dist;
float sY = speed * dY / dist;
getOwner().addX((int) (sX + 0.5));
getOwner().addY((int) (sY + 0.5));
You're describing the process of drawing a line between two points.
There are relatively simple integer-only algorithms, such as Bresenham that may help.
I need to be able to move my player x and y pixels in the same direction as a point to a point. It's hard to explain, but this is what I am trying to do:
Angles 1 and 2 have to be the same. Point 1 stays the same at (100, 100), however point 2 constantly changes and so must the angles. I have tried this:
moveRectangle.setX(touchEvent.getX());
moveRectangle.setY(touchEvent.getY());
float theta = (float) Math.toDegrees(Math.atan2(touchEvent.getY() - 100,touchEvent.getY() - 100));
float velX = (float) (getSpeed() * Math.cos(theta));
float velY = (float) (getSpeed() * Math.sin(theta));
player.move(velX, velY);
The above code is constantly run when the user puts his finger on moveRectangle (Point 2) and moves it. But the above code does not work. The player just moves in one of two directions. player.move just adds velX and velY velocity. So how can I get the two angles and move the player in the right direction? Thanks.
Would it be easier to approach this problem using a cartesian approach (vectors) versus polar approach (angle and magnitude)? So, if the player is at point p0 and the "finger" is at point p1, then the direction the player should be moving v is given by the vector p1-p0. You can then scale the resulting vector v by the player's speed, and add the player's speed to his position. You can do this easily on a frame-by-frame basis.
Do you need just to know velocity on X and Y axis? You can do it without using trigonometry (just use Pythagorean theorem).
final float deltaX = touchEvent.getX() - player.x; // player.x is point1.x
final float deltaY = touchEvent.getY() - player.y; // player.y is point1.y
final float length = Maths.sqrt((deltaX)^2 + (deltaY)^2);
final float itterations = length / getSpeed();
final float velX = deltaX / itterations;
final float velY = deltaY / itterations;
player.move(velX, velY);
Or you need a code of player moving in cycle?
Remove Math.toDegrees().
From the Math.sin() / cos() Javadoc:
Parameters:
a - an angle, in radians.
Returns:
the sine of the argument.