#How to move a ball in a square path

18 messages · Page 1 of 1 (latest)

plush dirge
#

i want to move a ball in a square path. Right now it moves in a triangular like wave. I want it to behave like the image. What can i do to make this happen? thanks! I have tried many things so far (moving only one position and so on). I didn't want to split the screen but do it dynamic, if possible.

My code at the moment:

public void move(int widthScreen, int heightScreen) {

    posX += velocityX;

    posY += velocityY;

    // Check for collisions with screen walls

    if (posX <= 0 || posX >= widthScreen - size) {

        velocityX *= -1; // Reverse horizontal velocity

    }

    if (posY <= 0 || posY >= heightScreen - size) {

        velocityY *= -1; // Reverse vertical velocity

    }
}```
edgy wolfBOT
#

This post has been reserved for your question.

Hey @plush dirge! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

edgy wolfBOT
#

<@&765578700724371486>

Requested by saara#6096
cinder fossil
# plush dirge i want to move a ball in a square path. Right now it moves in a triangular like ...

You could define points on the screen at which point you would want the ball to turn or use random points:

private int nextTurn = -1;
public void move(int widthScreen, int heightScreen) {
    posX += velocityX;
    posY += velocityY;
      
    if(posX >= nextTurn || nextTurn < 0) {
        nextTurn += (int) (Math.random()*widthScreen/6);
        if(velocityX == 0) {
            velocityY = 0;
            velocityX = //Whatever you want
        } else {
           velocityX = 0;
           velocityY = //Whatever you want
        }
    }
    // Check for collisions with screen walls
    if (posX <= 0 || posX >= widthScreen - size) {
        velocityX *= -1; // Reverse horizontal velocity
    }
    if (posY <= 0 || posY >= heightScreen - size) {
        velocityY *= -1; // Reverse vertical velocity
    }
}

(Not tested)

edgy wolfBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

plush dirge
edgy wolfBOT
cinder fossil
plush dirge
#

No, it didn't work i dont think since it just goes in a straight line. This is the complete code i have:

cinder fossil
#

this should work although readability could be improved:

    private int nextXPos = -1;
    private int nextYPos = -1;
    private int tempYvelocity = 0;
    private int tempXvelocity = 0;
    public void move(int widthScreen, int heightScreen) {
        if(nextXPos < 0) {
            nextXPos += (int) (Math.random() * widthScreen / 6);
            tempXvelocity = (int) velocityX;
            System.out.println(nextXPos);
        }
        // Check for collisions with screen walls
        if (posX <= 0 || posX >= widthScreen - size) {
            tempXvelocity *= -1; // Reverse horizontal velocity
        }
        if (posY <= 0 || posY >= heightScreen - size) {
            tempYvelocity *= -1; // Reverse vertical velocity
        }
        posX += tempXvelocity;
        posY += tempYvelocity;


        if(Math.abs(nextYPos - posY) <= velocityY && tempYvelocity != 0) {
            nextXPos += (int) (Math.random() * widthScreen / 6)+100;
            nextYPos = tempYvelocity > 0 ? -1:1;
            tempYvelocity = 0;
            tempXvelocity = (int) velocityX;
        } else if(posX >= nextXPos && tempXvelocity != 0) {
            nextYPos *= (int) (Math.random() * heightScreen / 3);
            nextYPos += heightScreen/2;
            tempYvelocity = (int) velocityY;
            tempYvelocity *= nextYPos < posY ? -1:1;
            tempXvelocity = 0;
        }
    }
plush dirge
#

thank you so much !! when it bounces against one of the "walls" goes again to be move in a straight line but its a big improvement from what i had no doubts

edgy wolfBOT
cinder fossil
cinder fossil
# plush dirge thank you so much !! when it bounces against one of the "walls" goes again to be...
    private int nextXPos = Integer.MIN_VALUE;
    private int nextYPos = 0;
    private int tempYvelocity = 0;
    private int tempXvelocity = 0;
    public void move(int widthScreen, int heightScreen) {
        if(nextXPos == Integer.MIN_VALUE) {
            nextXPos = (int) (Math.random() * widthScreen / 6);
            tempXvelocity = (int) velocityX;
        }
        // Check for collisions with screen walls
        if ((posX <= 0 || posX >= widthScreen - size)&&tempXvelocity != 0) {
            tempXvelocity *= -1; // Reverse horizontal velocity
            velocityX *= -1;
            if(nextXPos > widthScreen){
                nextXPos = widthScreen - (nextXPos-widthScreen);
            } else if(nextXPos < 0) {
                nextXPos = 40;
            }
        }
        if (posY <= 0 || posY >= heightScreen - size) {
            tempYvelocity *= -1; // Reverse vertical velocity
            velocityY *= -1;
        }
        posX += tempXvelocity;
        posY += tempYvelocity;


        if(Math.abs(nextYPos - posY) <= velocityY && tempYvelocity != 0) {
            nextXPos += ((int) (Math.random() * widthScreen / 6)+100)*(velocityX < 0 ? -1:1);
            nextYPos = tempYvelocity > 0 ? -1:1;
            tempYvelocity = 0;
            tempXvelocity = (int) velocityX;
        } else if(posX >= nextXPos && tempXvelocity > 0 || posX <= nextXPos && tempXvelocity < 0) {
            nextYPos *= (int) (Math.random() * heightScreen / 3);
            nextYPos += heightScreen/2;
            tempYvelocity = (int) velocityY;
            tempYvelocity *= nextYPos < posY ? -1:1;
            tempXvelocity = 0;
        }
    }

It should work now.

edgy wolfBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

plush dirge
#

it worked perfectly !!! thank you so much for all the help

edgy wolfBOT