Raycasting giving erratic values - java

Using LWJGL3 and JOML.
I am trying to work out how to get the point on the terrain using a raycast system. I use the point to set a characters position to see what point is being outputted.
I don't think it is my terrain causing the issue since moving the character (with the keyboard or by just adding a value each frame) and attaching it to the terrain works fine.
The issues I get:
Inverting the projection matrix causes the point to flicker between the correct position and some other point but I am not sure about the relationship with this other value.
Not inverting the projection matrix stops the flickering but now the point moves away from the mouse position exponentially.
Near the center of the screen the 2 positions will merge together.
If I print out the terrain point vector it comes out in scientific notation for some reason:
( 5.335E+1 3.849E-2 -9.564E+1)
( 8.804E+1 -6.256E-3 -2.815E+2)
( 5.335E+1 3.849E-2 -9.564E+1)
( 8.804E+1 -6.256E-3 -2.815E+2)
( 5.335E+1 3.849E-2 -9.564E+1)
If I print out each of the x, y and z values individually it is actually showing the correct position but is also flicking to another (the difference between which increases the further from the center of the screen that the mouse moves):
5.8912144, 0.016174316, -7.771721
6.1992702, 0.01574707, -11.79966
5.8912144, 0.016174316, -7.771721
6.1992702, 0.01574707, -11.79966
6.609352, 0.01815033, -8.793705
Raycasting class:
public class Raycast
{
private static final int RECURSION_COUNT = 200;
private static final float RAY_RANGE = 600;
private Input input;
private Vector3f currentRay = new Vector3f();
private Matrix4f projectionMatrix;
private Matrix4f viewMatrix;
private Camera camera;
private Terrain terrain;
private Vector3f currentTerrainPoint;
public Raycast(Camera camera, Matrix4f projectionMatrix, Terrain terrain, Input input) {
this.camera = camera;
this.projectionMatrix = projectionMatrix;
this.input = input;
this.viewMatrix = MathUtils.createViewMatrix(camera);
this.terrain = terrain;
}
public void update()
{
viewMatrix = MathUtils.createViewMatrix(camera);
currentRay = calculateRay();
if (intersectionInRange(0, RAY_RANGE, currentRay)) {
currentTerrainPoint = binarySearch(0, 0, RAY_RANGE, currentRay);
} else {
currentTerrainPoint = null;
}
}
private Vector3f calculateRay()
{
float mouseX = (float) input.getMouseDx();
float mouseY = (float) input.getMouseDy();
Vector2f deviceCoords = getNormalizedDeviceCoordinates(mouseX, mouseY);
//System.out.println(deviceCoords.x+", "+deviceCoords.y);
Vector4f clipCoords = new Vector4f(deviceCoords.x, deviceCoords.y, -1f, 1f);
Vector4f eyeCoords = toEyeCoords(clipCoords);
Vector3f worldRay = toWorldCoords(eyeCoords);
return worldRay;
}
private Vector3f toWorldCoords(Vector4f eyeCoords)
{
Matrix4f invertedView = viewMatrix.invert();
Vector4f rayWorld = invertedView.transform(eyeCoords);
Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
mouseRay.normalize();
return mouseRay;
}
private Vector4f toEyeCoords(Vector4f clipCoords)
{
Matrix4f invertedProjection = projectionMatrix.invert();
Vector4f eyeCoords = invertedProjection.transform(clipCoords);
return new Vector4f(eyeCoords.x, eyeCoords.y, -1f, 0f);
}
private Vector2f getNormalizedDeviceCoordinates(float mouseX, float mouseY)
{
float x = (2f * mouseX) / Constants.DISPLAY_WIDTH - 1f;
float y = (2f * mouseY) / Constants.DISPLAY_HEIGHT - 1f;
return new Vector2f(x, -y);
}
private Vector3f getPointOnRay(Vector3f ray, float distance) {
//Vector3f camPos = new Vector3f(camera.getPosX(), camera.getPosY(), camera.getPosZ());
Vector3f start = new Vector3f(camera.getPosX(), camera.getPosY(), camera.getPosZ());
Vector3f scaledRay = new Vector3f(ray.x * distance, ray.y * distance, ray.z * distance);
return start.add(scaledRay);
}
private Vector3f binarySearch(int count, float start, float finish, Vector3f ray) {
float half = start + ((finish - start) / 2f);
if (count >= RECURSION_COUNT) {
Vector3f endPoint = getPointOnRay(ray, half);
Terrain terrain = getTerrain(endPoint.x, endPoint.z);
if (terrain != null) {
return endPoint;
} else {
return null;
}
}
if (intersectionInRange(start, half, ray)) {
return binarySearch(count + 1, start, half, ray);
} else {
return binarySearch(count + 1, half, finish, ray);
}
}
private boolean intersectionInRange(float start, float finish, Vector3f ray) {
Vector3f startPoint = getPointOnRay(ray, start);
Vector3f endPoint = getPointOnRay(ray, finish);
if (!isUnderGround(startPoint) && isUnderGround(endPoint)) {
return true;
} else {
return false;
}
}
private boolean isUnderGround(Vector3f testPoint) {
Terrain terrain = getTerrain(testPoint.x, testPoint.z);
float height = 0;
if (terrain != null) {
height = terrain.getTerrainHeight(testPoint.x, testPoint.z);
}
if (testPoint.y < height) {
return true;
} else {
return false;
}
}
private Terrain getTerrain(float worldX, float worldZ) {
return terrain;
}
public Vector3f getCurrentTerrainPoint() {
return currentTerrainPoint;
}
public Vector3f getCurrentRay() {
return currentRay;
}
}
MathUtils class with view and projection matrices:
public class MathUtils {
public static float baryCentric(Vector3f p1, Vector3f p2, Vector3f p3, Vector2f pos) {
float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z)) / det;
float l2 = ((p3.z - p1.z) * (pos.x - p3.x) + (p1.x - p3.x) * (pos.y - p3.z)) / det;
float l3 = 1.0f - l1 - l2;
return l1 * p1.y + l2 * p2.y + l3 * p3.y;
}
public static Matrix4f createTransformationMatrix(Vector2f translation, Vector2f scale) {
Matrix4f matrix = new Matrix4f();
matrix.identity();
matrix.translate(translation.x,translation.y,0f);
matrix.scale(scale.x,scale.y,1f);
return matrix;
}
public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry, float rz, float scale) {
Matrix4f transformationMatrix = new Matrix4f();
transformationMatrix.identity();
transformationMatrix.translate(translation);
transformationMatrix.rotate((float) Math.toRadians(rx), 1,0,0);
transformationMatrix.rotate((float) Math.toRadians(ry), 0,1,0);
transformationMatrix.rotate((float) Math.toRadians(rz), 0,0,1);
transformationMatrix.scale(scale);
return transformationMatrix;
}
public static Matrix4f createViewMatrix(Camera camera) {
Matrix4f viewMatrix = new Matrix4f();
viewMatrix.identity();
viewMatrix = viewMatrix.rotate((float) Math.toRadians(camera.getPitch()), 1,0,0);//((float) Math.toRadians(camera.getPitch()), new Vector3f(1, 0, 0), viewMatrix);
viewMatrix = viewMatrix.rotate((float) Math.toRadians(camera.getYaw()),0, 1, 0);
Vector3f cameraPos = new Vector3f(camera.getPosX(), camera.getPosY(), camera.getPosZ());
Vector3f negativeCameraPos = new Vector3f(-cameraPos.x, -cameraPos.y, -cameraPos.z);
viewMatrix = viewMatrix.translate(negativeCameraPos);
return viewMatrix;
}
public static Matrix4f createProjectionMatrix() {
Matrix4f projectionMatrix = new Matrix4f();
float aspectRatio = (float) Constants.DISPLAY_WIDTH / (float) Constants.DISPLAY_HEIGHT;
float fov = Constants.FOV;
float near = Constants.NEAR_PLANE;
float far = Constants.FAR_PLANE;
projectionMatrix = projectionMatrix.perspective((float) java.lang.Math.toRadians(fov), aspectRatio, near, far);
return projectionMatrix;
}

A coordinate in view space is a Cartesian coordinates with 3 components x, y and z. The projection matrix transforms from view space to clip space. Clip space coordinates are Homogeneous coordinates with 4 components x, y, z and w.
Clip space coordinates can be transformed to normalized device coordinates by a Perspective divide.
This means the x, y and z component is divided by w component.
If you want to transform from normalized device space to view space, then you've to do the inverse operation. this means you've to transform by the inverse projection matrix and to divide the x, y and z component of the result by the w component of the result.
private Vector4f toEyeCoords(Vector4f ndcCoords)
{
Matrix4f invertedProjection = projectionMatrix.invert(new Matrix4f());
Vector4f eyeCoords = invertedProjection.transform(clipCoords);
return new Vector4f(eyeCoords.x/eyeCoords.w, eyeCoords.y/eyeCoords.w, eyeCoords.z/eyeCoords.w, 0.0f);
}

Related

Transformation Hierarchy (Problem with modelling transformation of parent on children), OpenGL, LWJGL 3

I am currently building a scene graph in OpenGL and I trying to model parent transformations on their children, but it does not seem to be working. :(
My main problem is that the child's rotation does not follow the parent's rotation correctly. The child rotates around the parent object (center), which is what it is supposed to do, but the speed of it orbiting around the parent increases as the parent's rotation increase and slows back down once the parent does a full circle (when the parent's rotation reaches back to the rotation it started of at). Also I did have some problems with the correct translation of the child in relation to the parent, but I managed to temporarily fix them (I think). More info in the code. Sorry for the lengthy explanation, I would have definitely attached a video if I could, of the problem.
This is my transform class, where the transformations are set, particularly the getTransformation() method:
public class Transform {
private Vector3f position, lastPosition;
private Vector3f rotation, lastRotation;
private Vector3f scale, lastScale;
private Transform parent;
private Matrix4f parentMatrix;
public Transform() {
this(null, null, null);
}
public Transform(Vector3f position, Vector3f rotation, Vector3f scale) {
this.position = position != null ? position : new Vector3f(0, 0, 0);
this.rotation = rotation != null ? rotation : new Vector3f(0, 0, 0);
this.scale = scale != null ? scale : new Vector3f(1, 1, 1);
this.lastPosition = new Vector3f(0, 0, 0);
this.lastRotation = new Vector3f(0, 0, 0);
this.lastScale = new Vector3f(1, 1, 1);
parentMatrix = new Matrix4f().identity();
}
public boolean requireTMUpdate() { //checks if the matrix has changed and requires an update
if(parent != null) {
return parent.requireTMUpdate();
}
if(!position.equals(lastPosition)) {
lastPosition.set(position);
return true;
}
if(!rotation.equals(lastRotation)) {
lastRotation.set(rotation);
return true;
}
if(!scale.equals(lastScale)) {
lastScale.set(scale);
return true;
}
return false;
}
public Matrix4f getTransformation() {
if((parent != null) && (requireTMUpdate())) {
if(!(getParent().equals(getParent().getParent()))) {
parentMatrix.set(parent.getTransformation());
//The above line sets the updated parentMatrix
setPosition(parentMatrix.transformPosition(position));
//The above line sets the position to where child is supposed to be in
//relation the parent, otherwise once the key is stopped being pressed,
//it will return to the position at the start of the game / program.
}
}else {
parentMatrix.rotationXYZ(0, 0, 0).translation(0, 0, 0).scale(1);
// The above line is supposed to reset the matrix, otherwise the previous
//transformations or rotations add up each frame and the it just gets messed
//up.
}
//System.out.println(parentMatrix.toString());
return parentMatrix.mul(Matrices.transformationMatrix(position, rotation, scale))
// The transformationMatrix() method above from the Matrices class is
//supposed to return a worldMatrix.
}
public Vector3f getPosition() {
return position;
}
public void setPosition(Vector3f position) {
this.position = position;
}
public Vector3f getRotation() {
return rotation;
}
public void setRotation(Vector3f rotation) {
this.rotation = rotation;
}
public Vector3f getScale() {
return scale;
}
public void setScale(Vector3f scale) {
this.scale = scale;
}
public Transform getParent() {
return parent;
}
public void setParent(Transform parent) {
this.parent = parent;
}
}
Matrices class:
public class Matrices {
public static Matrix4f transformationMatrix(Vector3f pos, Vector3f rot, Vector3f scale) {
Matrix4f result = new Matrix4f();
result.identity();
result.translate(pos.x, pos.y, pos.z);
Quaternionf rotation =
new Quaternionf().
identity().
rotateX((float) Math.toRadians(rot.x)).
rotateY((float) Math.toRadians(rot.y)).
rotateZ((float) Math.toRadians(rot.z));
/*result.rotate((float) Math.toRadians(rot.getX()), 1, 0, 0);
result.rotate((float) Math.toRadians(rot.getY()), 0, 1, 0);
result.rotate((float) Math.toRadians(rot.getZ()), 0, 0, 1);*/
result.rotate(rotation);
result.scale(scale.x, scale.y, scale.z);
return result;
}
public static Matrix4f transformationMatrix(Vector3f pos, Quaternionf rotation, Vector3f scale) {
Matrix4f result = new Matrix4f();
result.identity();
result.translate(pos.x, pos.y, pos.z);
/*Quaternionf rotation =
new Quaternionf().
identity().
rotateX((float) Math.toRadians(rot.x)).
rotateY((float) Math.toRadians(rot.y)).
rotateZ((float) Math.toRadians(rot.z));*/
/*result.rotate((float) Math.toRadians(rot.getX()), 1, 0, 0);
result.rotate((float) Math.toRadians(rot.getY()), 0, 1, 0);
result.rotate((float) Math.toRadians(rot.getZ()), 0, 0, 1);*/
result.rotate(rotation);
result.scale(scale.x, scale.y, scale.z);
return result;
}
public static Matrix4f viewMatrix(Vector3f pos, Vector3f rot) {
Matrix4f result = new Matrix4f();
result.identity();
Quaternionf rotation =
new Quaternionf().
identity().
rotateX((float) Math.toRadians(rot.x)).
rotateY((float) Math.toRadians(rot.y)).
rotateZ((float) Math.toRadians(rot.z));
/*result.rotate((float) Math.toRadians(rot.getX()), new org.joml.Vector3f(1, 0, 0));
result.rotate((float) Math.toRadians(rot.getY()), new org.joml.Vector3f(0, 1, 0));*/
result.rotate(rotation);
result.translate(-pos.x, -pos.y, -pos.z);
return result;
}
public static Matrix4f viewMatrix(Vector3f pos, float pitch, float yaw, float roll) {
Matrix4f result = new Matrix4f();
result.identity();
Quaternionf rotation =
new Quaternionf().
identity().
rotateX((float) Math.toRadians(pitch)).
rotateY((float) Math.toRadians(yaw)).
rotateZ((float) Math.toRadians(roll));
/*result.rotate((float) Math.toRadians(rot.getX()), new org.joml.Vector3f(1, 0, 0));
result.rotate((float) Math.toRadians(rot.getY()), new org.joml.Vector3f(0, 1, 0));*/
result.rotate(rotation);
result.translate(-pos.x, -pos.y, -pos.z);
return result;
}
public static Matrix4f projectionMatrix(float FOV, float aspectRatio,
float NearPlaneDis, float FarPlaneDis) {
Matrix4f result = new Matrix4f();
result.identity();
result.perspective(FOV, aspectRatio, NearPlaneDis, FarPlaneDis);
/*float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
float x_scale = y_scale / aspectRatio;
float frustum_length = FarPlaneDis - NearPlaneDis;
result.m00(x_scale);
result.m11(y_scale);
result.m22(-((FarPlaneDis + NearPlaneDis) / frustum_length));
result.m23(-1);
result.m32(-((2 * NearPlaneDis * FarPlaneDis) / frustum_length));
result.m33(0);*/
return result;
}
public static Matrix4f translate(Vector3f vec, Matrix4f src, Matrix4f dest) {
if (dest == null)
dest = new Matrix4f();
dest.m30(dest.m30() + src.m00() * vec.x + src.m10() * vec.y + src.m20() * vec.z);
dest.m31(dest.m31() + src.m01() * vec.x + src.m11() * vec.y + src.m21() * vec.z);
dest.m32(dest.m32() + src.m02() * vec.x + src.m12() * vec.y + src.m22() * vec.z);
dest.m33(dest.m33() + src.m03() * vec.x + src.m13() * vec.y + src.m23() * vec.z);
return dest;
}
public static Vector4f transform(Matrix4f left, Vector4f right, Vector4f dest) {
if (dest == null)
dest = new Vector4f(0, 0, 0, 0);
float x = left.m00() * right.x + left.m10() * right.y + left.m20() * right.z + left.m30() * right.w;
float y = left.m01() * right.x + left.m11() * right.y + left.m21() * right.z + left.m31() * right.w;
float z = left.m02() * right.x + left.m12() * right.y + left.m22() * right.z + left.m32() * right.w;
float w = left.m03() * right.x + left.m13() * right.y + left.m23() * right.z + left.m33() * right.w;
dest.x = x;
dest.y = y;
dest.z = z;
dest.w = w;
return dest;
}
public static Vector3f scale(Vector3f vector, float scale) {
vector.x *= scale;
vector.y *= scale;
vector.z *= scale;
return vector;
}
public static float[] getAll(Matrix4f matrix) {
float[] f = new float[16];
return matrix.get(f);
}
public static float barryCentric(Vector3f p1, Vector3f p2, Vector3f p3, Vector2f pos) {
float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z)) / det;
float l2 = ((p3.z - p1.z) * (pos.x - p3.x) + (p1.x - p3.x) * (pos.y - p3.z)) / det;
float l3 = 1.0f - l1 - l2;
return l1 * p1.y + l2 * p2.y + l3 * p3.y;
}
}
Finally, the Renderer class, where the objects are initialized:
public class Renderer {
private Model model;
private Model model2;
private GameObject root;
private GameObject child;
public Camera camera;
private float time = 0;
/*private float vertices[] = {
0.5f, 0.5f, 0f, //0 - top right
0.5f, -0.5f, 0f, //1 - bottom right
-0.5f, -0.5f, 0f, //2 - bottom left
-0.5f, 0.5f, 0f //3 - top left
};
private int indices[] = {
0, 1, 3, // first triangle
3, 1, 2 // second triangle
};*/
public Renderer() {
model = new Model("/backpack.obj");
model2 = new Model("/backpack.obj");
camera = new Camera(new Vector3f(0, 0, 0), new Vector3f(0, 0, 0));
root = new GameObject();
child = new GameObject();
root.addComponent(camera);
root.addComponent(model);
child.addComponent(model2);
root.addChild(child);
model.getTransform().setPosition(new Vector3f(0, 0, 0));
model2.getTransform().setPosition(new Vector3f(10, 0, 10));
}
public void render(Shader shader) {
if(Input.isKeyDown(GLFW.GLFW_KEY_RIGHT)) {
model.getTransform().setRotation(new Vector3f(0, time += 1f, 0));
}
//model2.getTransform().setRotation(new Vector3f(0, time += 0.01f, 0));
shader.bind();
root.input();
root.update();
root.render(shader, camera);
shader.unbind();
}
public void cleanUp() {
for(Mesh mesh: model.getMeshes()) {
mesh.cleanUp();
}
}
}
What exactly am I doing wrong here? Any help is appreciated!
Nvm, I found the solution after a day's worth of debugging:
This line:
return parentMatrix.mul(Matrices.transformationMatrix(position, rotation, scale));
should actually be:
return new Matrix4f(parentMatrix).mul(Matrices.transformationMatrix(position, rotation, scale));.
Turns out the mul method in the JOML Matrix4f class does not return a new Matrix4f() class of the worldMatrix that should be outputted instead multiplies it to the current matrix (parentMatrix here) itself, creating a completely different parentMatrix for the next frame.

How to calculate if 3D model is intersecting terrain surface LWJGL

I am currently working on a project using 3d simplex noise and the marching cubes algorithm in order to procedurally generated terrain. I am trying to implement collision detection between the player object and terrain mesh but I have no clue how to start. I have read some articles and posts about using JBullet and other libraries but they do not support complex meshes such as the ones generated by simplex noise. In order to simplify things for myself I decided to make it so that the player can only move in the direction I point meaning that I would only need to check if a singular point on the player is intersecting with the terrain. Are there any methods to implement such a process? (edit: I've already looked into barycentric coordinates but I have no idea how to implement into the game)
Current Player Code
package Entities;
import org.lwjgl.glfw.GLFW;
import Engine.Input;
import Maths.Vector3f;
import Models.TexturedModel;
public class Player extends Entity {
public float xspeed = 0,zspeed = 0, yspeed = 0;
public Vector3f mousePos;
public float yrotation = 0, zrotation = 0;
public float maxYRotation = 75f;
private double lastMousePosX = 600 , newMousePosX;
private double lastMousePosY = 500 , newMousePosY;
private float speed = 3;
public Player(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale) {
super(model, position, rotX, rotY, rotZ, scale);
}
public void move(){
checkInput();
System.out.println("x ="+this.position.x+" y ="+this.position.y+" z ="+this.position.z);
checkCollision();
}
public boolean checkCollision(){
if(terrain != null){
for(int i = 0; i<terrain.getVertices().length; i+=9){
Vector3f vertex1 = new Vector3f(terrain.getVertices()[i],terrain.getVertices()[i+1],terrain.getVertices()[i+2]);
Vector3f vertex2 = new Vector3f(terrain.getVertices()[i+3],terrain.getVertices()[i+4],terrain.getVertices()[i+5]);
Vector3f vertex3 = new Vector3f(terrain.getVertices()[i+6],terrain.getVertices()[i+7],terrain.getVertices()[i+8]);
//Check if point p is interseting triangle (vertex1, vertex2, vertex3)
if(someCalculationFunction(position, vertex1, vertex2, vertex3){
return true;
}
}
}
return false;
}
public void checkInput(){
newMousePosX = Input.getMouseX();
newMousePosY = Input.getMouseY();
float dx = (float)(newMousePosX-lastMousePosX)*0.07f;
float dy = (float)(newMousePosY-lastMousePosY)*0.07f;
if(!Input.isMouseDown(GLFW.GLFW_MOUSE_BUTTON_1)){
this.rotY -= dx/2;
this.rotX -= dy*0.8f;
}
if(Math.abs(rotX) > 50){
this.rotX = Math.abs(rotX)/rotX*50;
}
if(this.rotY<0){
this.rotY = 360;
}
float horizontalDistance = speed*(float)(Math.cos(Math.toRadians(rotX)));
float verticleDistance = speed*(float)(Math.sin(Math.toRadians(rotX)));
if(Input.isKeyDown(GLFW.GLFW_KEY_W)){
this.position.x += horizontalDistance*Math.sin(Math.toRadians(-rotY));
this.position.z -= horizontalDistance*Math.cos(Math.toRadians(-rotY));
this.position.y += verticleDistance;
}else if(Input.isKeyDown(GLFW.GLFW_KEY_S)){
this.position.x -= horizontalDistance*Math.sin(Math.toRadians(-rotY));
this.position.z += horizontalDistance*Math.cos(Math.toRadians(-rotY));
this.position.y -= verticleDistance;
}
lastMousePosX = newMousePosX;
lastMousePosY = newMousePosY;
}
}
I'm not positive if I understood the question right, but this answer will address the problem of ensuring the players height is that of the terrain that it is standing on
With barycentric coordinates you can calculate what the players height is supposed to be by using the heights of the three vertices that make up that triangle:
public static float baryCentric(Vector3f p1, Vector3f p2, Vector3f p3, Vector2f pos) {
float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z)) / det;
float l2 = ((p3.z - p1.z) * (pos.x - p3.x) + (p1.x - p3.x) * (pos.y - p3.z)) / det;
float l3 = 1.0f - l1 - l2;
return l1 * p1.y + l2 * p2.y + l3 * p3.y;
}
In order to get these three points you can make a calculation using the world coordinates of your player:
//Assuming the world is constructed of equal height and width sized triangles
float gridSquareSize = SIZE_OF_TERRAIN_MESH / NUM_TRIANGLES_PER_ROW;
float xCoord = worldX % gridSquareSize / gridSquareSize;
float zCoord = worldZ % gridSquareSize / gridSquareSize;
With the xCoord and zCoord you can determine the 3 points that you need to use for your baryCentric calculation

create percentage square in java instead circles

I am working in a existing project for an amazfit watchface. Code is based in java. The question is: In original project, for show battery, steps and sport percentage, show three circles. My idea is to draw a rectangle (or a line) instead the original circle. The problem is I am new programming in java and I donĀ“t know for change this without FC app.
this watch has two screens: one active and other in stand-by mode (8colors only)
active mode draws circle, standby mode works with an png image.
This is the code (for circles):
package es.xxxx.xxxx.widget;
private final float startAngleBattery = 30;
private final float arcSizeBattery = 360 - startAngleBattery - startAngleBattery;
#Override
public void init(Service service) {
this.thickness = (int) service.getResources().getDimension(R.dimen.xxxx_circles_thickness);
this.textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
this.textPaint.setTypeface(ResourceManager.getTypeFace(service.getResources(), ResourceManager.Font.BEBAS_NEUE));
this.textPaint.setTextSize(service.getResources().getDimension(R.dimen.xxxx_circles_font_size));
this.textPaint.setColor(service.getResources().getColor(R.color.xxxx_time_colour));
this.textPaint.setTextAlign(Paint.Align.CENTER);
this.ring = new Paint(Paint.ANTI_ALIAS_FLAG);
this.ring.setStrokeCap(Paint.Cap.ROUND);
this.ring.setStyle(Paint.Style.STROKE);
this.ring.setStrokeWidth(this.thickness);
this.circle = new Paint(Paint.ANTI_ALIAS_FLAG);
this.circle.setColor(Color.BLACK);
this.circle.setStrokeWidth(1f);
this.circle.setStyle(Paint.Style.STROKE);
#Override
public void draw(Canvas canvas, float width, float height, float centerX, float centerY) {
int count = canvas.save();
int radius = Math.round(Math.min(width / 2, height / 2)) - this.thickness;
RectF oval = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
// rotate from 0 to 270 degrees
canvas.rotate(90, centerX, centerY);
this.ring.setColor(this.backgroundColour);
canvas.drawArc(oval, startAngleBattery, arcSizeBattery, false, ring);
if (batterySweepAngle != null) {
float px = getPointX(oval, centerX, startAngleBattery, batterySweepAngle);
float py = getPointY(oval, centerY, startAngleBattery, batterySweepAngle);
this.ring.setColor(this.batteryColour);
canvas.drawArc(oval, startAngleBattery, batterySweepAngle, false, ring);
canvas.drawCircle(px, py, this.thickness / 3f, circle);
canvas.drawCircle(px, py, this.thickness / 6f, circle);
}
canvas.restoreToCount(count);
if (this.batteryData != null) {
String text = String.format("%02d", this.batteryData.getLevel() * 100 / this.batteryData.getScale());
canvas.drawText(text, batteryTextLeft, batteryTextTop, textPaint);
}
}
#Override
public void onDataUpdate(DataType type, Object value) {
switch (type) {
case BATTERY:
onBatteryData((Battery) value);
break;
}
}
#Override
public List<DataType> getDataTypes() {
return Arrays.asList(DataType.BATTERY);
private void onBatteryData(Battery battery) {
this.batteryData = battery;
if (batteryData == null) {
this.batterySweepAngle = 0f;
} else {
float scale = batteryData.getLevel() / (float) batteryData.getScale();
this.batterySweepAngle = Math.min(arcSizeBattery, arcSizeBattery * scale);
}
}
private RectF nextOval(RectF oval) {
oval.left = oval.left + this.thickness + MARGIN;
oval.top = oval.top + this.thickness + MARGIN;
oval.right = oval.right - this.thickness - MARGIN;
oval.bottom = oval.bottom - this.thickness - MARGIN;
return oval;
}
private float getPointX(RectF oval, float cx, float startAngle, float sweepAngle) {
float width = oval.right - oval.left;
return (float) (cx + (width / 2D) * Math.cos((sweepAngle + startAngle) * Math.PI / 180));
}
private float getPointY(RectF oval, float cy, float startAngle, float sweepAngle) {
float height = oval.bottom - oval.top;
return (float) (cy + (height / 2D) * Math.sin((sweepAngle + startAngle) * Math.PI / 180));
}
#Override
public List<SlptViewComponent> buildSlptViewComponent(Service service) {
Typeface timeTypeFace = ResourceManager.getTypeFace(service.getResources(), ResourceManager.Font.BEBAS_NEUE);
SlptLinearLayout power = new SlptLinearLayout();
power.alignX = 2;
power.alignY = 2;
power.add(new SlptPowerNumView());
power.setTextAttrForAll(
service.getResources().getDimension(R.dimen.xxxx_circles_font_size_slpt),
-1,
timeTypeFace
);
power.setStart(
(int) service.getResources().getDimension(R.dimen.xxxx_battery_text_left_slpt),
(int) service.getResources().getDimension(R.dimen.xxxx_battery_text_top_slpt));
SlptPowerArcAnglePicView powerArcView = new SlptPowerArcAnglePicView();
powerArcView.setImagePicture(Util.assetToBytes(service, "battery_splt.png"));
powerArcView.start_angle = (int) startAngleBattery + 180 - 3;
powerArcView.full_angle = (int) arcSizeBattery + 6;
return Arrays.asList(power, powerArcView);
}
}
Thanks in advance.
For anyone still searching...
You can draw the rectangular in the "draw" function that runs in loop constantly when screen is on, however, screen off (SLPT mode) uses ingenic's libraries to draw (function buildSlptViewComponent) and there is the real problem.
I don't want to get into details because it would be pages, so have a look at GreatFit project.

Strange PolyBatch Positioning in libGDX

Excuse the basic question, just getting into the guts of LibGDX
I'm creating a radial bar to show my countdown timer
I've found some code that does what I need it to, the problem is the radial sprite's positioning. I can't seem to get it to center in the Image object (Since it seems to be ignoring the Image's local coordinates and is defaulting to the stage's) so 0,0 places it close to the bottom left of my screen.
I've tried using a localtoStage and vice versa to calculate the correct positions, but that doesn't seem to give me the right values either.
Please advise
package com.goplayplay.klpoker.CSS.Classes;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.EarClippingTriangulator;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.ShortArray;
public class ProgressCircle extends Image {
TextureRegion texture;
PolygonSpriteBatch polyBatch;
Vector2 center;
Vector2 centerTop;
Vector2 leftTop;
Vector2 leftBottom;
Vector2 rightBottom;
Vector2 rightTop;
Vector2 progressPoint;
float[] fv;
IntersectAt intersectAt;
public ProgressCircle(TextureRegion region, PolygonSpriteBatch polyBatch) {
super(region);
this.texture = region;
this.polyBatch = polyBatch;
center = new Vector2(this.getWidth() / 2, this.getHeight() / 2);
centerTop = new Vector2(this.getWidth() / 2, this.getHeight());
leftTop = new Vector2(0, this.getHeight());
leftBottom = new Vector2(0, 0);
rightBottom = new Vector2(this.getWidth(), 0);
rightTop = new Vector2(this.getWidth(), this.getHeight());
progressPoint = new Vector2(this.getWidth() / 2, this.getHeight() / 2);
setPercentage(0);
}
private Vector2 IntersectPoint(Vector2 line) {
Vector2 v = new Vector2();
boolean isIntersect;
//check top
isIntersect = Intersector.intersectSegments(leftTop, rightTop, center, line, v);
//check bottom
if (isIntersect) {
intersectAt = IntersectAt.TOP;
return v;
} else isIntersect = Intersector.intersectSegments(leftBottom, rightBottom, center, line, v);
//check left
if (isIntersect) {
intersectAt = IntersectAt.BOTTOM;
return v;
} else isIntersect = Intersector.intersectSegments(leftTop, leftBottom, center, line, v);
//check bottom
if (isIntersect) {
intersectAt = IntersectAt.LEFT;
return v;
} else isIntersect = Intersector.intersectSegments(rightTop, rightBottom, center, line, v);
if (isIntersect) {
intersectAt = IntersectAt.RIGHT;
return v;
} else {
intersectAt = IntersectAt.NONE;
return null;
}
}
public void setPercentage(float percent) {
//100 % = 360 degree
//==> percent % => (percent * 360 / 100) degree
float angle = convertToRadians(90); //percent = 0 => angle = -90
angle -= convertToRadians(percent * 360 / 100);
float len = this.getWidth() > this.getHeight() ? this.getWidth() : this.getHeight();
float dy = (float) (Math.sin(angle) * len);
float dx = (float) (Math.cos(angle) * len);
Vector2 line = new Vector2(center.x + dx, center.y + dy);
Vector2 v = IntersectPoint(line);
if (intersectAt == IntersectAt.TOP) {
if (v.x >= this.getWidth() / 2)
{
fv = new float[]{
center.x,
center.y,
centerTop.x,
centerTop.y,
leftTop.x,
leftTop.y,
leftBottom.x,
leftBottom.y,
rightBottom.x,
rightBottom.y,
rightTop.x,
rightTop.y,
v.x,
v.y
};
} else {
fv = new float[]{
center.x,
center.y,
centerTop.x,
centerTop.y,
v.x,
v.y
};
}
} else if (intersectAt == IntersectAt.BOTTOM) {
fv = new float[]{
center.x,
center.y,
centerTop.x,
centerTop.y,
leftTop.x,
leftTop.y,
leftBottom.x,
leftBottom.y,
v.x,
v.y
};
} else if (intersectAt == IntersectAt.LEFT) {
fv = new float[]{
center.x,
center.y,
centerTop.x,
centerTop.y,
leftTop.x,
leftTop.y,
v.x,
v.y
};
} else if (intersectAt == IntersectAt.RIGHT) {
fv = new float[]{
center.x,
center.y,
centerTop.x,
centerTop.y,
leftTop.x,
leftTop.y,
leftBottom.x,
leftBottom.y,
rightBottom.x,
rightBottom.y,
v.x,
v.y
};
} else // if (intersectAt == IntersectAt.NONE)
{
fv = null;
}
}
//
#Override
public void draw(Batch batch, float parentAlpha) {
// super.draw(batch, parentAlpha);
if (fv == null) return;
batch.end();
drawMe();
batch.begin();
}
public void drawMe() {
Vector2 acc = new Vector2();
acc.set(getWidth() / 2, getHeight() / 2);
localToStageCoordinates(acc);
EarClippingTriangulator e = new EarClippingTriangulator();
ShortArray sv = e.computeTriangles(fv);
PolygonRegion polyReg = new PolygonRegion(texture, fv, sv.toArray());
PolygonSprite poly = new PolygonSprite(polyReg);
poly.setOrigin(this.getOriginX(), this.getOriginY());
poly.setPosition(this.getX(), this.getY());
// poly.setPosition(acc.x, acc.y); //Attempting to calculate correct positioning - Doesnt work
poly.setRotation(this.getRotation());
poly.setColor(this.getColor());
polyBatch.begin();
poly.draw(polyBatch);
polyBatch.end();
}
float convertToDegrees(float angleInRadians) {
float angleInDegrees = angleInRadians * 57.2957795f;
return angleInDegrees;
}
//-----------------------------------------------------------------
float convertToRadians(float angleInDegrees) {
float angleInRadians = angleInDegrees * 0.0174532925f;
return angleInRadians;
}
public enum IntersectAt {
NONE, TOP, BOTTOM, LEFT, RIGHT
}
}
You forgot to set the camera's projection matrix on the polygon batch. You can get a copy of it from the Batch that's passed in:
public void draw(Batch batch, float parentAlpha) {
// super.draw(batch, parentAlpha);
if (fv == null) return;
batch.end();
drawMe(batch.getProjectionMatrix());
batch.begin();
}
public void drawMe(Matrix4 projection) {
polyBatch.setProjectionMatrix(projection);
//...
}
Or more simply, you can use a PolygonBatch as your Stage's batch, so you don't have to be swapping batches:
stage = new Stage(myViewport, new PolygonBatch());
//...
public void draw(Batch batch, float parentAlpha) {
// super.draw(batch, parentAlpha);
if (fv == null) return;
//don't need to call begin and end on the batch
drawMe((PolygonBatch)batch);
}
public void drawMe(PolygonBatch polyBatch) {
//...
//don't need to call begin or end on the batch
}
By the way, your drawMe method instantiates quite a few objects, some large. You should avoid this if you have more than a few actors that do this, or you'll get stutters from the GC. Try to instantiate objects only once in the constructor and reuse them.

How to unbind a sprite in an updating vector?

How to unbind an updating vector to a sprite? I am using Libgdx framework. here is my code
public class VectorSample extends GDX_TEST implements InputProcessor {
Texture ball,bullet,guider;
OrthographicCamera camera;
Vector2 vector = new Vector2();
Vector2 vectoralt = new Vector2();
long lastDropTime;
SpriteBatch batch;
Rectangle rect = new Rectangle();
float angle = 0,anglealt = 0;
private ShapeRenderer renderer;
TextureRegion tr;
Sprite sprite,sprite2;
ShapeRenderer sr;
Ship ship;
BallShit shit;
Vector2 nvec;
protected Vector2 center = new Vector2();
Array<Vector2> bullets;
Array<Rectangle> bulletsRect;
Boolean fire = false;
Rectangle tmpRect = new Rectangle();
#Override
public void create() {
renderer = new ShapeRenderer();
ball = new Texture(Gdx.files.internal("data/player.png"));
bullet = new Texture("data/ball_black.png");
tr = new TextureRegion(ball);
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
ship = new Ship(new Vector2(5, 50), 1, 1, 0, 5f);
sr = new ShapeRenderer();
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);
rect.width = ball.getWidth();
rect.height = ball.getHeight();
vector.add(200,200);
sprite = new Sprite(ball);
vectoralt.add(200 + sprite.getWidth(),200);
shit = new BallShit(new Vector2(10,0),50f,50f);
bullets = new Array<Vector2>();
getCenter();
bulletsRect = new Array<Rectangle>();
fireatwill2();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
{
batch.draw(sprite,vector.x,vector.y,sprite.getWidth()/2,sprite.getHeight()/2,sprite.getWidth(),sprite.getHeight(),1,1,angle);
if(fire == true){
for(Rectangle raindrop: bulletsRect) {
batch.draw(bullet,raindrop.x,raindrop.y);
}
}
}
batch.end();
if(Gdx.input.isKeyPressed(Input.Keys.D)){
vector.add(1,0);
if(vector.x > Gdx.graphics.getWidth()){
Gdx.app.log("x","x");
}
}
if(Gdx.input.isKeyPressed(Input.Keys.A)){
vector.add(-1,0);
if(vector.x < 0){
Gdx.app.log("x","-x");
}
}
float w = camera.frustum.planePoints[1].x - camera.frustum.planePoints[0].x;
float distance = w - (vector.x + sprite.getWidth());
if(distance <=0){
vector.x = w - sprite.getWidth();
}
Gdx.app.log("camera x" , " " + distance);
if(distance >= Gdx.graphics.getWidth() - sprite.getWidth()) {
vector.x = 0;
}
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
fire = true;
if(TimeUtils.nanoTime() - lastDropTime > 100000000) fireatwill2();
}
Iterator<Rectangle> iter = bulletsRect.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
double angletry = getAngle() * MathUtils.degreesToRadians;
float speed = 5;
float scale_x = (float)Math.cos(angletry);
float scale_y = (float)Math.sin(angletry);
float velocity_x = (speed* scale_x);
float velocity_y = (speed* scale_y);
raindrop.x += velocity_x;
raindrop.y += velocity_y;
if(raindrop.y < 0) iter.remove();
if(raindrop.y > Gdx.graphics.getHeight() || raindrop.x > Gdx.graphics.getWidth()) iter.remove();
}
//getting the angle
float angle = findAngle(Gdx.input.getX(),
Gdx.graphics.getHeight() - Gdx.input.getY());
this.angle = angle % 360;
}
private float getAngle(){
float angle = findAngle(Gdx.input.getX(),
Gdx.graphics.getHeight() - Gdx.input.getY());
return angle;
}
private void fireatwill2() {
Rectangle raindrop = new Rectangle();
raindrop.x = vector.x + sprite.getWidth() / 2 - (bullet.getWidth() / 2);
raindrop.y = vector.y + sprite.getHeight() / 2 - (bullet.getHeight() / 2);
bulletsRect.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
public Vector2 getCenter() {
center.x = vector.x + sprite.getWidth() / 2;
center.y = vector.y + sprite.getHeight() / 2;
return center.cpy();
}
#Override
public void pause() {
super.pause(); //To change body of overridden methods use File | Settings | File Templates.
}
#Override
public void resume() {
super.resume(); //To change body of overridden methods use File | Settings | File Templates.
}
public float findAngle(float x1, float y1) {
Vector2 center = getCenter();
float x0 = center.x;
float y0 = center.y;
float a = MathUtils.atan2(y1 - y0, x1 - x0);
return a * MathUtils.radiansToDegrees;
}
}
and here is the running demo and source is here
I cant explain it well but if you could run the demo u will get what i am saying.
Im really stuck in here.. Thanks.

Categories