4 Replies - 458 Views - Last Post: 02 August 2015 - 05:58 AM Rate Topic: -----

#1 JAVA NOOB   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 01-August 15

Making the squareYLocation accelerate with time (how to add gravity)?

Posted 01 August 2015 - 12:46 PM

I put my question next to the part of the code which I would like to modify. I present two ways in which I think it will work, but the easiest option is probably near the bottom with Thread.sleep.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.math.*;
import java.lang.Object.*;

public class App extends JPanel {
    //changing these values will change the size of the game, while still remaining functional
    //within the size limit specified.
    static int windowWidth = 1300;
    static int windowHeight =1020;
    int randNumb = 650;
    int squareWidth = 5;
    int squareHeight = 5;
    int squareYLocation = (squareHeight);
    int squareXLocation = 650;
    boolean numberCreated = false;
    static boolean gameRunning = false;
    


    //generates a random Y value inside the window for the square to spawn at
    public void generateRandomNumber() {
        Random rand = new Random();
        randNumb = rand.nextInt(windowWidth - squareWidth);
        numberCreated = true;
    }

    //paints a black screen, then paints a rectangle on top of the black screen
    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0, 0, windowWidth, windowHeight);
        g.setColor(Color.PINK);
        g.fillRect(randNumb, squareYLocation, squareWidth, squareHeight);
    }

    public void update() {
	
        //moves the squares y coordinate towards the bottom of the screen and stops once it hits the bottom
        if (squareYLocation <= windowHeight)
        { squareYLocation++; // this is where I need to add the equation (9.81 * time^2) to make the square accelerate but I can't seem to get a timer to track the time, how would I do this?

            //resets the x and y location to a new position
        } else {
            numberCreated = false;
            squareYLocation = squareHeight;
	    squareXLocation= 650;
        }
    }

    //sets the while loop to true to start the game
    public void start() {
        gameRunning = true;
    }

    public static void main(String[] args) throws InterruptedException {

        App game = new App();
        JFrame frame = new JFrame();
        frame.add(game);
        frame.setVisible(true);
        frame.setSize(windowWidth, windowHeight);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setTitle("Raining Squares");
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);

        game.start();

        //updates square position, repaints square, and slows down update and paint speed.
        while (gameRunning) {
            game.update();
            game.repaint();
	    Thread.sleep(4);// another option I was looking into was making the thread.sleep progress from 0 to another number so the square would get faster and faster. Is this even possible being that Thread.sleep() is a static variable?
        }
    }
}

This post has been edited by andrewsw: 01 August 2015 - 02:13 PM
Reason for edit:: Added [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Making the squareYLocation accelerate with time (how to add gravity)?

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Making the squareYLocation accelerate with time (how to add gravity)?

Posted 01 August 2015 - 12:54 PM

How to use code tags:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 Inchidi   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 21-July 15

Re: Making the squareYLocation accelerate with time (how to add gravity)?

Posted 01 August 2015 - 06:45 PM

hai JAVA NOOB i am still java noob too but i'll try to help you here~
first, doing something like this is not a good habit

View PostJAVA NOOB, on 02 August 2015 - 02:46 AM, said:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.math.*;
import java.lang.Object.*;


this is why

and about your solution i think thats good but its not a great choice
imagine if you make a game and reduce FPS on all motion like this
then you can use something like timer to tell your code when to be slower or faster
then how to describe slower and faster?
as you know speed formula is:

Quote

speed(velocity) = distance(displacement) / time

then your time is your timer and your length is difference between x/y now and x/y next
finally, you can set your velocity/speed variable to get acceleration you want

thats one of many method. i'll give you another example too~
Spoiler

This post has been edited by macosxnerd101: 01 August 2015 - 07:34 PM
Reason for edit:: Removed Indonesian comments per Inchidi's request

Was This Post Helpful? 1
  • +
  • -

#4 JAVA NOOB   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 01-August 15

Re: Making the squareYLocation accelerate with time (how to add gravity)?

Posted 01 August 2015 - 08:45 PM

Thank you Inchidi, your solutions are very helpful! I'm almost there, and I will post my final code when I'm done.
yeah, I know that stopping and starting the code is very bad form, especially when dealing with very long coding projects, eventually I should move on from that.
I like the example you give because it shows how to properly implement the timer. this method is definitely the best, thanks.
Was This Post Helpful? 1
  • +
  • -

#5 Inchidi   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 21-July 15

Re: Making the squareYLocation accelerate with time (how to add gravity)?

Posted 02 August 2015 - 05:58 AM

glad you enjoy it
btw welcome to DIC~
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1