Hey everyone, Im looking for code that would make it so that if your pointing at 30 degress off of 0 degrees, and you want to go straight that you actually go straight, and not along the x or y axisif you get what Im saying. any help would be good, BTW, I have an angle variable if that can help.
TL;DW version: sin is the Y component of movement, cos is X
I know about trigonometry and all, im just looking for any code to help because I cant find it anywhere
Trig / vector math, in a 2D environment that means sine and cosine.
float angle = (float) Math.toRadians(30);
float xDir = (float) Math.cos(angle);
float yDir = (float) Math.sin(angle);
float pixelSpeed = 10;
xPos += xDir * pixelSpeed;
yPos += yDir * pixelSpeed;
what about a 3d environment, and here is the code for the class
package com.game.Fearless.input;
public class Controller {
public double x, z, rotation, xa, za, rotationa, angle;
public void tick(boolean forward, boolean left, boolean back, boolean right, boolean turnLeft, boolean turnRight) {
double rotationSpeed = 0.025;
double walkSpeed =0.0025;
double xMove = 0;
double zMove = 0;
if (forward) {
if (back == false) {
zMove++;
z += za;
}
}
if (left) {
if (right == false) {
xMove--;
x -= xa;
}
}
if (back) {
if (forward == false) {
zMove--;
z -= za;
}
}
if (right) {
if (left == false) {
xMove++;
x += xa;
}
}
if (turnLeft) {
rotationa = 0;
if (turnRight == false) {
angle += rotationa * rotationSpeed;
}
rotation -= angle;
}
if (turnRight) {
rotationa = 0;
if (turnLeft == false) {
angle += rotationa * rotationSpeed;
}
rotation += angle;
}
xa += (xMove * Math.cos(angle) + zMove * Math.sin(angle)) * walkSpeed;
za += (xMove * Math.cos(angle) - zMove * Math.sin(angle)) * walkSpeed;
System.out.println("X= " + x + " Z= " + z + " Rotation= " + rotation);
}
}
Don’t think he’s ready for 3D if he can’t figure this out in 2D. :point:
Furthermore, 3D in pure Java is pain
Most of distance techniques in Java2D can be used in 3D world, just need to increase the degree.