I want to move an object based on its own direction. The object has a translation vector and a rotation vector(degrees). I can call a method move and give it a direction and the units the object should be moved(speed if you so wish). If I give this method the direction "FORWARD" than it should move in the direction the object is currently facing.
I have this code currently:
/*
* MIT License
*
* Copyright (c) 2017 Ralph Niemitz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package de.ralleytn.engine.lunatic;
import javax.vecmath.Vector3f;
/**
* Merges rotation with translation and adds translation based on rotation.
* @author Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
* @version 1.0.0
* @since 1.0.0
*/
public interface Movable extends Translatable, Rotatable {
/**
* Moves the object into the given direction based on its own rotation.
* @param direction the direction in which the object should be moved
* @param units the number of units it should be moved
* @since 1.0.0
*/
public default void move(Direction direction, float units) {
float nUnits = -units;
Vector3f rotation = this.getRotation();
if(direction == Direction.LEFT) {
float rY = (float)Math.toRadians(rotation.y);
float rZ = (float)Math.toRadians(rotation.z);
float x = (float)Math.cos(rY) * nUnits;
float y = (float)Math.sin(rZ) * units;
float z = (float)Math.sin(rY) * nUnits;
this.translate(x, y, z);
} else if(direction == Direction.RIGHT) {
// float rX = (float)Math.toRadians(rotation.x);
float rY = (float)Math.toRadians(rotation.y);
float rZ = (float)Math.toRadians(rotation.z);
float x = (float)Math.cos(rY) * units;
float y = (float)Math.sin(rZ) * nUnits;
float z = (float)Math.sin(rY) * units;
this.translate(x, y, z);
} else if(direction == Direction.FORWARD) {
float rX = (float)Math.toRadians(rotation.x);
float rY = (float)Math.toRadians(rotation.y - 90.0F);
float x = (float)Math.cos(rY) * units;
float y = (float)Math.sin(rX) * nUnits;
float z = (float)Math.sin(rY) * units;
this.translate(x, y, z);
} else if(direction == Direction.BACKWARD) {
float rX = (float)Math.toRadians(rotation.x);
float rY = (float)Math.toRadians(rotation.y - 90.0F);
float x = (float)Math.cos(rY) * nUnits;
float y = (float)Math.sin(rX) * units;
float z = (float)Math.sin(rY) * nUnits;
this.translate(x, y, z);
} else if(direction == Direction.UP) {
float rX = (float)Math.toRadians(rotation.x);
float rZ = (float)Math.toRadians(rotation.z);
float x = (float)Math.sin(rZ) * nUnits;
float y = (float)Math.cos(rX) * units;
float z = (float)Math.sin(rX) * nUnits;
this.translate(x, y, z);
} else if(direction == Direction.DOWN) {
float rX = (float)Math.toRadians(rotation.x);
float rZ = (float)Math.toRadians(rotation.z);
float x = (float)Math.sin(rZ) * units;
float y = (float)Math.cos(rX) * nUnits;
float z = (float)Math.sin(rX) * units;
this.translate(x, y, z);
}
}
}
It doesn't really work how I want it to. I'm fairly new to 3D so I don't have that much knowledge. All the solutions I've seen so far are for Unity and C# which doesn't really help me.
I'm using the javax.vecmath package if that is important.