0

In my homework I have to code a 2d parcour-game with javafx. How can I add sensors to my player(gameobject) so it can print out the distance to the next barrier?

To create the barriers/walls I used circles, my player is also an circle. Can anyone help me with this problem?

I expact when I am controlling the player, that the sensor detects the nearest object in front of him, and prints out the distance and location(x/y) of the object.

This is the main/control class where I build the walls and the player. I hope this helps for understanding my problem.This is how the program looks like for now. I am also happy to get some coding advice.

public class ParkourApp extends Application {

private List<Point2D> points = new ArrayList<>();
private List<Circle> circles = new ArrayList<>();
private Player p1;


public Parent createContent (){
    BorderPane root = new BorderPane();
    Pane draw = new Pane();
    Circle c1 = new Circle(100,0,5);
    Circle c2 = new Circle(200,0,5);
    Circle c3 = new Circle(300,0,5);
    Circle c4 = new Circle(400,0,5);

    Circle c5 = new Circle(0,100,5);
    Circle c6 = new Circle(0,200,5);
    Circle c7 = new Circle(0,300,5);
    Circle c8 = new Circle(0,400,5);


    addVerticalStraight(100,100,100,50,6);
    addHorizontalStraight(150,50,100,50,6);
    addVerticalStraight(140,250,50,50,6);
    addVerticalStraight(300,150,100,50,6);
    addStraight(100,90,140,50,9);
    addStraight(100,210,135,250,8);
    addStraight(158,205,190,240,8);
    addStraight(260,50,330,80,9);
    addStraight(340,85,350,140,8);
    addStraight(258,105,300,140,8);
    addAllCircles();

    AnimationTimer at = new AnimationTimer() {
        @Override
        public void handle(long now) {
            update();
        }
    };
    at.start();

    for(Circle c : circles){
        draw.getChildren().add(c);
    }

    p1 = new Player(new Rectangle(50,400,11,5));
    p1.setGeschwindigkeit(new Point2D(1,0));
    System.out.printf("xx:%f YY:%f\n",p1.getNode().getTranslateX(),p1.getNode().getTranslateY());

    draw.getChildren().addAll(c1,c2,c3,c4,c5,c6,c7,c8,p1.getNode());

    root.setCenter(draw);
    return root;
}

private void update(){
    for(Circle c : circles){
        if(p1.isColliding(c)){
            System.out.println("is Colliding..");
        }
    }
    p1.update();
}

private void addAllCircles(){
    for(Point2D po : points){
        Circle c = new Circle(po.getX(),po.getY(),3, Color.DIMGREY);
        circles.add(c);
    }
}

private ArrayList<Point2D> addStraight(double startX, double startY, double endX, double endY, double divisor){
    ArrayList<Point2D> list = new ArrayList<>();
    double length = 0;
    double xDist = 0;
    double m = 0;
    if(startX != endX) {
        if(endX < startX){
            double temp = startX;
            startX = endX;
            endX = temp;
            temp = startY;
            startY = endY;
            endY = temp;
        }
        //upgrade of the straight
        m = -(startY-endY)/(endX - startX);
        System.out.println(m);

        //Distance of the two points
        length = Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
        System.out.println(length);

        //xDist is the distance of every point of the straight on the x-axis
        xDist = endX - startX;
        xDist /= divisor;
        System.out.println(xDist);


        double x = startX;
        double y = startY;
        //this loop uses all the data that was calculated and creates a   
        //2D-Point which is stored in class-list of the circles
        for(int i=0; i<divisor+1; i++){
            list.add(new Point2D(x, y));
            points.add(new Point2D(x, y));
            y += (xDist * m);
            System.out.println(y);
            x += xDist;
        }
    }
    return list;
}

private ArrayList<Point2D> addHorizontalStraight(double x, double y, double length, double width, double dist){
    int amount = (int)length/(int)dist;
    ArrayList<Point2D> list = new ArrayList<>();
    for(int i=0; i<amount+1; i++){
        list.add(new Point2D(x,y));
        list.add(new Point2D(x,y+width));
        points.add(new Point2D(x,y));
        points.add(new Point2D(x,y+width));
        x += dist;
    }
    return list;
}

private ArrayList<Point2D> addVerticalStraight(double x, double y, double length, double width, double dist){
    int amount = (int)length/(int)dist;
    ArrayList<Point2D> list = new ArrayList<>();
    for(int i=0; i<amount+1; i++){
        list.add(new Point2D(x,y));
        list.add(new Point2D(x+width,y));
        points.add(new Point2D(x,y));
        points.add(new Point2D(x+width,y));
        y += dist;
    }
    return list;
}

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    primaryStage.setScene(new Scene(createContent(),600,600));
    primaryStage.getScene().setOnKeyReleased(e ->{
        switch (e.getCode()){
            case LEFT:
                p1.turnLeft();
                break;
            case RIGHT:
                p1.turnRight();
                break;
            case UP:
                p1.faster();
                break;
            case DOWN:
                p1.slower();
                break;
        }
    });


    primaryStage.show();
}

}

lukasQ
  • 1
  • 1
  • 2
  • 2
    it's _your_ homework ;) Consult your textbook, find out/learn what you should know, start designing/code, when you are stuck at a concrete problem, provide a [mcve] that demonstrates the problem. – kleopatra Dec 25 '18 at 12:30
  • You may get some insight from the example examined [here](https://stackoverflow.com/q/39185306/230513). – trashgod Dec 26 '18 at 09:26
  • I am looking for a solution that gives me the distance instantaneously. I don't think shooting a bullet in a driection and waiting for it to intersact with a wall will solve this problem, because the bullet needs time to arrive a wall. – lukasQ Dec 26 '18 at 09:49
  • You may still be able to leverage the `intersect()` method. – trashgod Dec 27 '18 at 21:11

0 Answers0