Commit c1e025bf authored by Timothy  Borunov's avatar Timothy Borunov
Browse files

Object integrated with the testfunction

parent f7656e26
Loading
Loading
Loading
Loading
+124 −1
Original line number Diff line number Diff line
package Shape;public class MovingObject {
package Shape;

import Graph.readFile;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;
import javafx.scene.transform.Translate;

public class MovingObject {
    private readFile fileObject;
    private float[] texCoords;
    private float[] centroid;
    private TriangleMesh mesh;
    private MeshView triangleView;
    private Translate translate;
    private boolean dirtyTranslate;

    MovingObject() {
        texCoords = new float[2];
        mesh = new TriangleMesh();
        translate = new Translate();
        centroid = new float[3];
        dirtyTranslate = true;
    }

    public MovingObject(String[] parameters) {
        this();
        fileObject = new readFile(parameters);
        fileObject.makeVertexHash(fileObject.getPath());
        fileObject.makeMatrix();
        mesh.getPoints().setAll(fileObject.getVertices());
        mesh.getFaces().setAll(fileObject.getFaces());
        triangleView = new MeshView(mesh);
        setTempDefault();
        setCentroid();
    }

    public MovingObject(String filePath) {
        this();
        fileObject = new readFile(filePath);
        fileObject.makeMatrix();
        mesh.getPoints().setAll(fileObject.getVertices());
        mesh.getFaces().setAll(fileObject.getFaces());
        triangleView = new MeshView(mesh);
        setTempDefault();
        setCentroid();
    }

    public void setTexCoords(float t1, float t2) {
        texCoords[0] = t1;
        texCoords[1] = t2;
        mesh.getTexCoords().setAll(texCoords);
        triangleView.setMesh(mesh);
    }

    public void setTranslate(double x, double y, double z) {
        translate.setX(x);
        translate.setY(y);
        translate.setZ(z);
        triangleView.getTransforms().add(translate);
    }

    public void updateTranslate(double x, double y, double z) {
        translate.setX(x);
        translate.setY(y);
        translate.setZ(z);
    }

    public Translate getTranslate() {
        return translate;
    }

    public void setNewFilePath(String filePath) {
        fileObject = new readFile(filePath);
        fileObject.makeMatrix();
        mesh.getPoints().setAll(fileObject.getVertices());
        mesh.getFaces().setAll(fileObject.getFaces());
        setTexCoords(0,0);
        triangleView = new MeshView(mesh);
        setTempDefault();
        dirtyTranslate = true;
        setCentroid();
    }

    public void setTempDefault() {
        triangleView.setCullFace(CullFace.NONE);
        triangleView.setMaterial(new PhongMaterial(Color.BLUE));
        setTexCoords(0,0);
    }

    public MeshView getMesh() {
        return triangleView;
    }

    public void setCentroid() {
        if (dirtyTranslate) {
            float[] vertices = fileObject.getVertices();
            int count = 0;
            float x = 0;
            float y = 0;
            float z = 0;
            for(float ii : vertices) {
                int index = count % 3;
                if (index == 0) {
                    x += ii;
                } else if (index == 1) {
                    y += ii;
                } else {
                    z += ii;
                }
                count++;
            }
            centroid[0] = x/count;
            centroid[1] = y/count;
            centroid[2] = z/count;
        }
    }

    public float[] getCentroid() {
        return centroid;
    }

}
+24 −57
Original line number Diff line number Diff line
package org.example.newmat;
import Graph.readFile;
import Shape.MovingObject;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
@@ -29,7 +30,7 @@ import java.io.IOException;
 * it includes a real-time FPS counter overlaid on the scene to monitor performance.
 */
public class GraphicsAndWindowsTest extends Application {
    public static String[] parameters;
    //public static String[] parameters;

    /**
     * Starts the JavaFX application.
@@ -50,49 +51,15 @@ public class GraphicsAndWindowsTest extends Application {
        translate1.setZ(150);

        cube.getTransforms().add(translate1);
        TriangleMesh mesh = new TriangleMesh();

        /*
         * Texture coordinates used to add image to a mesh.
         * Initialized at 0, 0 since there is no need for an image.
         *
         */
        float[] texCoords = {
                0, 0
        };

        parameters = getParameters().getRaw().toArray(new String[0]);
        readFile processInput = new readFile(parameters);
        processInput.makeVertexHash(processInput.getPath());
        //processInput.makeMatrix();

        //Gives the vertices (points) to the mesh in the form of a float [] array
        mesh.getPoints().setAll(processInput.getVertices());

        //Gives faces to the mesh in the form of an int [] array
        mesh.getFaces().setAll(processInput.getFaces());

        //gives texture coordinate array
        mesh.getTexCoords().setAll(texCoords);

        // This is where the mesh is set to the MeshView.
        MeshView triangleView = new MeshView(mesh);
        // Removes front and back differential
        // removes need to keep track of front and back
        triangleView.setCullFace(CullFace.NONE);
        triangleView.setMaterial(new PhongMaterial(Color.BLUE));
        MovingObject newObject = new MovingObject(getParameters().getRaw().toArray(new String[0]));

        // Create a Translate transform
        Translate translate = new Translate();
        // Set the translation values
        translate.setX(600); // Translate 600 units along the X axis
        translate.setY(300); // Translate 300 units along the Y axis
        translate.setZ(0);   // Translate 0 units along the Z axis

        triangleView.getTransforms().add(translate);
        newObject.setTranslate(600,300,0);

        Group group3D = new Group(); // Group of our 3D elements
        group3D.getChildren().add(triangleView);
        group3D.getChildren().add(newObject.getMesh());
        group3D.getChildren().add(cube);
        group3D.getChildren().add(new AmbientLight(Color.WHITE));

@@ -142,7 +109,6 @@ public class GraphicsAndWindowsTest extends Application {
        subScene3D.setCamera(camera);



        /*
         * Text label to display FPS
         */
@@ -167,7 +133,7 @@ public class GraphicsAndWindowsTest extends Application {
            private long lastUpdate = 0; //Movement timer
            private long fpsTimer = 0; //Frame timer
            private long frameRate = 0; //Frames per second
            private double directionY = 5.0; // Direction of movement
            private double directionY = 8.0; // Direction of movement
            private double directionX = 4.0; // Direction of movement
            private double directionZ = 10.0; // Direction of movement

@@ -191,7 +157,7 @@ public class GraphicsAndWindowsTest extends Application {
            @Override
            public void handle(long now) {
                //Built in class that helps calculate the bounds of the mesh for collision
                Bounds meshBounds = triangleView.getBoundsInParent();
                Bounds meshBounds = newObject.getMesh().getBoundsInParent();

                double minX = meshBounds.getMinX() + directionX;
                double maxX = meshBounds.getMaxX() + directionX;
@@ -207,8 +173,8 @@ public class GraphicsAndWindowsTest extends Application {
                    if (minX < cubeMin+300 || maxX > cubeMax+300) {

                        // load the vertices of the mesh
                        float[] points = mesh.getPoints().toArray(null);

                        //float[] points = mesh.getPoints().toArray(null);
                        /*
                        // find the vertices which come close to the bounding box threshold and squish them into the mesh
                        for (int j = 0; j < points.length; j += 3) {
                            if (Math.abs(points[j] + translate.getX() + directionX - minX) < 10 || Math.abs(points[j] + translate.getX() + directionX - maxX) < 10) {
@@ -216,50 +182,51 @@ public class GraphicsAndWindowsTest extends Application {
                            }
                        }

                         */

                        //update the direction of the mesh and set the new coordinates
                        directionX *= -1;
                        mesh.getPoints().setAll(points);
                        //mesh.getPoints().setAll(points);
                    }
                    if (minY < cubeMin || maxY > cubeMax) {

                        // load the vertices of the mesh
                        float[] points = mesh.getPoints().toArray(null);

                        //float[] points = mesh.getPoints().toArray(null);
                        /*
                        // find the vertices which come close to the bounding box threshold and squish them into the mesh
                        for (int j = 1; j < points.length; j += 3) {
                            if (Math.abs(points[j] + translate.getY() + directionY - minY) < 10 || Math.abs(points[j] + translate.getY() + directionY - maxY) < 10) {
                                points[j] += (float) (-directionY*5);
                            }
                        }

                        */
                        //update the direction of the mesh and set the new coordinates
                        directionY *= -1;
                        mesh.getPoints().setAll(points);
                        //mesh.getPoints().setAll(points);
                    }
                    if (minZ < cubeMin-300 || maxZ > cubeMax-300) {

                        // load the vertices of the mesh
                        float[] points = mesh.getPoints().toArray(null);

                        //float[] points = mesh.getPoints().toArray(null);
                        /*
                        // find the vertices which come close to the bounding box threshold and squish them into the mesh
                        for (int j = 2; j < points.length; j += 3) {
                            if (Math.abs(points[j] + translate.getZ() + directionZ - minZ) < 10 || Math.abs(points[j] + translate.getZ() + directionZ - maxZ) < 10) {
                                points[j] += (float) (-directionZ*5);
                            }
                        }
                        */

                        //update the direction of the mesh and set the new coordinates
                        directionZ *= -1;
                        mesh.getPoints().setAll(points);
                        //mesh.getPoints().setAll(points);

                    }

                    double newY = translate.getY() + directionY;
                    translate.setY(newY);
                    double newX = translate.getX() + directionX;
                    translate.setX(newX);
                    double newZ = translate.getZ() + directionZ;
                    translate.setZ(newZ);
                    double newY = newObject.getTranslate().getY() + directionY;
                    double newX = newObject.getTranslate().getX() + directionX;
                    double newZ = newObject.getTranslate().getZ() + directionZ;
                    newObject.updateTranslate(newX,newY,newZ);

                    lastUpdate = now;
                }