Okay. My problem is kinda mind-blowing. Let's say that I have a constructor which looks like this:
public Ball(Random r, float halfphonewidth, float halfphoneheight, float cx, float cy){
//te wartosci odpowiadaja za losowe polozenie i losowe wektory
x = (halfphonewidth-48)*0.1f;
y = (halfphoneheight-48)*0.1f;
vx = -0.2f + r.nextFloat();
vy = -0.2f + r.nextFloat();
Log.i("", "\n\n" + this.vx + " " +this.vy+"\n\n");
health = 3;
}
Now let's say that I have a step() method declared somewhere - it works. Step:
public void step(){
x += vx;
y += vy;
if(x<2f || x > 98f)
vx =- vx;
if(y<2f || y > 98f)
vy =- vy;
}
I have an onTouch event that listens and checks the 'click coords'. I catch them and pass to the constructor (float cx, float cy).
The first position of the ball is static - I set it permanently in the constructor and move by 48 pixels (cause of image size). It starts from the bottom-center and floats to the random direction + when it collides with a wall it reverses it's vx & vy.
So! I need to point the ball in a direction I click! :D
I tried by scaling screen w/h with coordinate system, tried with implementing Vector2D class (cause it's missing in the newest java I think) but im not that good with linear algebra, tried with trygonometry(even cyclometry) (but I may be blind).
Is there anyone that can point me a solution? Which is the best way and which should I try to implement?
You need to calculate the difference between the coordinates of the current position and the click position:
double xDiff = clickPoint.x - ball.x;
double yDiff = clickPoint.y - ball.y;
then you move the ball with a velocity proportional to that difference, in your case you have to set the vx and vy like:
vx = xDiff * n;
vy = yDiff * n;
where the bigger n is, the faster the ball will reach the target. You need to update the xDiff and yDiff variables as the ball move, so when the difference is 0 the movement will stop.
Related
I know the startpoint (the middle of the screen) and the angle (in my example 20°). Now I want to know the position on the edge of the screen, like an invisible line is drawn from the center to the edge in the given angle. For better explanation, I included an image:
One way to do this is to calculate a point on a circle with a radius equal to or greater than the maximum diagonal, and then just clip it to the screen boundary.
Using Pythagoras' theorem, the length of the maximum diagonal will be
float d = Math.sqrt((width/2)*(width/2) + (height/2)*(height/2));
So you can calculate the point on the circle like this (angle is in radians clockwise from the top):
float x = Math.sin(angle) * d;
float y = -Math.cos(angle) * d;
Then you have to clip the vector from the origin to the point to each of the 4 sides, e.g for the right and left sides:
if(x > width/2)
{
float clipFraction = (width/2) / x; // amount to shorten the vector
x *= clipFraction;
y *= clipFraction;
}
else if(x < -width/2)
{
float clipFraction = (-width/2) / x; // amount to shorten the vector
x *= clipFraction;
y *= clipFraction;
}
Also do this for height/2 and -height/2. Then finally you can add width/2, height/2 to x and y to get the final position (with the center of the screen being width/2, height/2 not 0,0):
x += width/2
y += height/2
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.
Top of the morning to ye people on various surfaces of earth.
The problem: How to get the angle of direction from the arrow keys.
Preamble: The standard way to move in a basic top-down game is to add to x and/or y from within an update method, depending on the direction.
if (moveUp) playerY -= delta * speed;
if (moveDown) playerY += delta * speed;
if (moveLeft) playerX -= delta * speed;
if (moveRight) playerX += delta * speed;
This is elegant for 4-direction movement (I believe) because no matter what key combinations are pressed, the direction of movement will be consistent. Eg pressing up-down-left will move left, as up and down cancel out. But when moving diagonally, the steps will be too large. If speed is 20, moving left will move left by 20 per second, up will move up by 20 per second. But moving up-left will move by a little over 28.24 per second.
The solution here is to use cos and sin to get the new x and y, which is easy once you know the angle:
playerX += Math.cos(Math.toRadians(angle)) * delta * speed;
playerY -= Math.sin(Math.toRadians(angle)) * delta * speed; //y is inverted
But, for me at least, this raises a new problem: what's the angle? In my KeyListener I'm currently setting/clearing booleans for each arrow key. I can use a bulky set of if statements:
if (moveUp) angle = 90;
if (moveDown) angle = 270;
if (moveRight) angle = 0;
if (moveLeft) angle = 180;
if (moveUp && moveLeft) angle = 135;
if (moveUp && moveRight) angle = 45;
if (moveDown && moveLeft) angle = 225;
if (moveDown && moveRight) angle = 315;
//...etc... for all combinations
For the life of me, I cannot find a sexy way to get the movement angle from what direction keys are pressed down. It strikes me like this should be a common problem, game design 101, but intense googling hasn't led me to anything (made harder by the fact that it's difficult to put the problem into words). In all instances of examples, either they just retained the diagonal-is-faster functionality (as with my first snippet), or know the angle ahead of time (ie. move towards the mouse), or are 2D side scrollers.
Surely there's a sexy mathy way (or something) to work it out in a few lines? Or am I approaching this completely wrong?
Edit: Post-answer code (as posted by korona below):
double x=0, y=0;
if (moveLeft) x -= 1;
if (moveRight) x += 1;
if (moveUp) y -= 1;
if (moveDown) y += 1;
double length = Math.sqrt(x * x + y * y);
if (length != 0) {
x /= length;
y /= length;
x *= delta*speed;
y *= delta*speed;
playerX += x;
playerY += y;
}
Use a 2-dimensional vector. Something like this:
movement = new Vector2D();
if (moveLeft) movement.x += 1;
if (moveRight) movement.x -= 1;
if (moveUp) movement.y -= 1;
if (moveDown) movement.y += 1;
movement.normalize(); // Caps the movement vector at a length of one, even when it's at an odd angle
movement *= desiredMovementSpeed * frameDeltaTime; // Plug in suitable values here
// FIXME: Do some collision detection here, probably
playerX += movement.x;
playerY += movement.y;
I assume there's a suitable 2D vector class available for you. If not, normalizing a vector is as easy as dividing all of its components by its length, as such:
length = sqrt(this.x * this.x + this.y * this.y);
this.x /= length;
this.y /= length;
I got x and y (My position) and also destination.x and destination.y (where I want to get). This is not for homework, just for training.
So what I did already is
float x3 = x - destination.x;
float y3 = y - destination.y;
float angle = (float) Math.atan2(y3, x3);
float distance = (float) Math.hypot(x3, y3);
I got angle and distance but don't know how to make it move directly.
Please help!
Thanks!
Maybe using this will help
float vx = destination.x - x;
float vy = destination.y - y;
for (float t = 0.0; t < 1.0; t+= step) {
float next_point_x = x + vx*t;
float next_point_y = y + vy*t;
System.out.println(next_point_x + ", " + next_point_y);
}
Now you have the coordinates of the points on the line. Choose step to small enough according to your need.
To calculate the velocity from a given angle use this:
velx=(float)Math.cos((angle)*0.0174532925f)*speed;
vely=(float)Math.sin((angle)*0.0174532925f)*speed;
*speed=your speed :) (play with the number to see what is the right)
I recommend calculating the x and y components of your movement independently.
using trigonometric operations slows your program down significantly.
a simple solution for your problem would be:
float dx = targetX - positionX;
float dy = targetY - positionY;
positionX = positionX + dx;
positionY = positionY + dy;
in this code example, you calculate the x and y distance from your position to your target
and you move there in one step.
you can apply a time factor (<1) and do the calculation multiple times, to make it look like your object is moving.
Note that + and - are much faster than cos(), sin() etc.