Rotate player to face objective java slick - java

So I am currently working on learning slick. I was doing fine until I ran into a problem. I have been trying to find a answer for a good hour and could not. So I decided to post it on here.
My Problem: I have player on a 800 X 800 Grid. I am trying to make the player move to a certain point on the grid in a straight line. Now I can make him move on the X then turn and move on the Y, But I want to make him get there as fast as possible. So I figured if I could make a right triangle from the following points (Player pos, Target Pos, and the X,Y intercept see my image bellow).
My code:
Adj = (int) (TargetX-x); // Get The size of the Adjacent leg.
Opp = (int) (TargetY-y); // Get the size of the Opposite leg.
OppAdj = Opp/Adj; //Inverse tan is Opposite/Adjacent
TargetAngle = Math.abs(Math.atan(Opp/Adj)*100); //Keep the angle positive, and use inverse tan to get the missing angle.
Now what I thought this would do, is solve for the missing angle so I could rotate the player by that amount so the player can move in a straight line and hit the objective.
What this ends up doing though is is giving me a target angle of 73 degrees and a the Variable OppAdj ends up being 1.0.
What is wrong with my code?
Any help is appreciated!
Thanks,
Kyle

OppAdj = Opp/Adj;
That is the problem. You should do this:
OppAdj = (double)(Opp)/Adj;
That way it will give you a double for accuracy. By the way:
TargetAngle = Math.abs(Math.atan(Opp/Adj)*100);
Should be:
TargetAngle = Math.abs(Math.atan(OppAdj)*100);

Related

Rotate player using libgdx touchpad to face moving direction

i have this Problem now for over 2 days. Constantly tweaking. I just cant get it done.
I have a Player texture ( Player is facing the left on it ) which i want to rotate using the touchpad. So the player will be facing his running direction.
So far i have this :
double facerotation = Math.atan2(touchpad.getKnobPercentY(), touchpad.getKnobPercentX());
spriteBatch.draw(runningFrame, player.getPosition().x, player.getPosition().y, Player.getSize() / 2, Player.getSize() / 2, Player.getSize(), Player.getSize(), 1, 1, facerotation * 100, false);
But with "roation*100" he spins like 2 times around and without he barely rotates. I even tried switching the X and Y values for the atan2 function above. But i never got him rotate only in the direction i am Moving. I also tried the atan function, also with swapping the X and Y values.
Please help me. I tried thousands of ways, Different calculations and things i saw on google. Nothing brought me the desired effect.
Just use a Vector2. Use it to store your knob percent y and x. Then you can get the rotatation in degrees with vector2.angle().
Vector2 v = new Vector2(touchpad.getKnobPercentX(), touchpad.getKnobPercentY());
float angle = v.angle();
runningFrame.setRotation(angle);

rotating the player via a control stick(touchpad) in libGDX

In my libGDX project I want to move a player around the screen with a touchpad. That already works.
Now I want to rotate the "face" of the player in the direction he is moving.
I have tried it with this little piece of code, but I doesn´t really work
double degree;
degree = Math.tan(touchpad.getKnobPercentX()/touchpad.getKnobPercentY());
playerSprite.setRotation((float) degree);
I hope u can help me (I have bee searching on Google for about 1 hour before I asked this question, so don't tell me to Google it;))
You might want to use the atan(y/x) function or even better the atan2(y,x) function to get the angle. Check the documentation where you get results in radians and expect inputs in degrees.
tan transforms an angle (in radians) to the tangent value, the slope of the angle.
phi=atan(y/x) is the arc tan, centuries ago written as arg(y/x=tan(phi)). It gives the same result for opposite angles, so you would have to correct for the right quadrant.
phi=atan2(y,x) already performs that correction.

Java move player smoothly from one point to another

All I need is to claculate the new coordinites of a player based on a click and have the player move towards it smoothly. For example player is at (20, 20) and you click at (50, 80) how would i get it to move smoothly. I have tried many different thing mostly based on the pythagorean formula trying to calculate the new coord's by finding the hypotenuse then finding the same triangle with a smaller hypotenuse but there must be an easier way.
You can:
Calculate the slope of the line segment formed by the origin and destination.
For each time period until you reach the destination, adjust the new coordinate based on the slope calculated in step 1.
Although I program in javascript, you should be able to read and understand it
function move(x1,y1,x2,y2,speed){//executed each time frame. Returns coords as array [x,y]
if(!speed)speed=1;
var dis=Math.sqrt(Math.pow(x1-x2)+Math.pow(y1-y2));
return [(x1-x2)/dis*speed,(y1-y2)/dis*speed];
}
The speed is constant at however many units per frame and it will stop when it reaches its destination. It might shake a little but that can be easily fixed.
EDIT: I forgot to mention. This function returns velocity. Not the coordinates

Making a rectangle go around in a circle - Java/Swing

My maths isn't that good so I'm having a bit of trouble in one of my applications that I'm trying to do where I want a rectangle to represent a vehicle and I want that vehicle/rectangle to "drive" around in a circle. Imagine a roundabout with only 1 vehicle in it, just circling around forever.
If I can get some help how to do that then I'll be able to build on the example and most importantly learn.
If someone could write up a simple example for me I'd be grateful. No background no images, just a rectangle "driving" around in a circle. I'm using java and Swing.
Sorry, I am not sure if could understand clear you exactly need. If you need to draw rectangle which is moving around inside of circle, you can use sin/cos functions.
Something like that:
double r = 50.0; // radius (it might radius of your circle, but consider dimensions of rectangle to make sure you are drawing inside of circle, e.g. circleRadius - rectangeDimesion / 2.0)
for (int f = 0; f < 360; f++) {
double x = Math.sin(Math.toRadians((double)f)) * r;
double y = Math.cos(Math.toRadians((double)f)) * r;
// draw rectangle on [x, y] coordinates
}
If you know the radius of the round about, all the you would need would be a trigonometric function and the angle which the vehicle makes to the round about. You could take a look at this simple introduction which should get you started in the right direction.
On another hand, another approach would be to use a Transformation Matrix where you start with a matrix containing two points (your X and Y co-ordinates) and you transform them to become the new co-ordinates.
You can then rotate the rectangle to mimic a vehicle turning.
If you have a limited background in Mathematics, the first option might be easier for you to grasp.
This is more an extended comment than an answer.
I would divide the problem up into several easier problems, and work on each of them separately:
Draw your rectangle with a specified center location and long axis orientation.
Determine the center point and long axis orientation for an object orbiting around the origin. Note that to get make the long axis a tangent it needs to be perpendicular to the radius through the center.
Translate the whole system so that it orbits the desired point, rather than the origin.

Generic Simple 3D Matrix Rotation Issue

I have a problem with my rotation on my 3D object (It's in java but it doesn't really matter)
The background:
I have a simple 3d model, and you are a first person player on it, you move your mouse up to look up (I.E Rotate by the 3D's x-axis) and move your mouse down to look down (Rotate in opposite direction)
But:
I also have the left and right arrow keys to turn left/right (Which rotates the 3D's y-axis)
Now the problem is, when I have turned, when I rotate by the x-axis it no longer turns as expected i.e if you turn 180 degrees, by moving your mouse down you actually look up and if you move your mouse up you actually look down.
What rotation can I perform on the x/y axis to fix this?
-So no matter how much/little I have turned, moving mouse up will look up and moving mouse down will look down.
Sorry I can't explain it better, but if you need any more info just make a comment.
Thanks alot,
It's probabaly a simple transformation, but i can't think :(
Some of Java rotation code:
Mouse Up/Down:
public void rotateXY(double radians, double Yradians)
{
vpTrans.getTransform(T3D);
Transform3D test = new Transform3D();
Transform3D testX = new Transform3D();
Transform3D testY = new Transform3D();
translate.set(lastXcord, lastYcord, lastZcord);
testX.rotX(radians);
testY.rotY(Yradians);
test.mul(testX, testY);
test.setTranslation(translate);
vpTrans.setTransform(test);
//System.out.println(test);
} // end of rotateXY()
Left: (right is just similar but with minus instead of plus on angle changes)
public void run() {
Transform3D test = new Transform3D();
Transform3D rotX = new Transform3D();
Transform3D rotY = new Transform3D();
vpTrans.getTransform(T3D);
translate.set(lastXcord, lastYcord, lastZcord);
angle += 0.05;
trueangle += 0.05;
rotX.rotX(angle);
rotY.rotY(Yangle);
test.mul(rotX, rotY);
test.setTranslation(translate);
vpTrans.setTransform(test);
}
What transformations/rotations I need to add to that so no matter my y-axis, camera will look up when mouse is moved up and down when mouse is moved down??
It appears you have a problem with the order of transformations. Unlike numerical multiplication, matrix multiplication is not commutative. 3D applications are very sensitive to the order of transformations.
In English:
if x and y are numbers, then x * y = y * x
However, if x and y are matrices, then x * y ≠ y * x!
With that in mind, let's look at what you are doing.
test.mul(rotX, rotY);
To visualize what is happening with this multiplication, imagine you have a straight line coming out of your head. We'll call this LineUp. Now look down at your keyboard, imagine LineUp tilting with you. Now turn your head 90 degrees on LineUp. That is what is happening to your rotation matrix. Crazy huh?
Now let's reverse the multiplication like so: test.mul(rotY, rotX);
So sit straight up and look forward, and turn your head/body/chair. Now look down a bit. You rotated on a true LineUp axis first, then applied the up/down transformation. Fun yeah?
A great way to think about matrix transformations is like an instruction set that has to be in order.
Turn head right. (rotY)
Turn head up. (rotX)
Move to computer (translate)
...etc
Changing the order of one operation changes all the subsequent instructions in funny and outrageous ways.
What you are building is a Camera, so I highly recommend reading how to build one.
A great exercise to understand matrix transformations is building a solar system, complete with orbiting moons and spinning planets. It will be eye opening and very informative.

Categories

Resources