-3

I'm making a simple game that will have a space ship being gravitationally attracted to a star at the center of the screen. The code I have so far moved the rectangle (soon to be a rocket) in a way that is not smooth at all. The rectangle also will not stop when it reaches the star but rather keeps moving well beyond the star before turning back.

Here's my code:

public void move() {
    // Deals with gravity towards the center star

    if (x > game.getWidth() / 2) {
        xVel -= xa;
    }
    if (x < game.getWidth() / 2) {
        xVel += xa;
    }
    if (y > game.getHeight() / 2) {
        yVel -= ya;
    }
    if (y < game.getHeight() / 2) {
        yVel += ya;
    }

    x += xVel;
    y += yVel;
}

I start by calculating where the rocket is located on the screen relative to the center, and then change its acceleration values, xa and ya, as needed. I then add the acceleration values to the Velocity variables, xVel and yVel.

TKDefender
  • 41
  • 6
  • 3
    Thanks for the information...and your question is? – pczeus Apr 24 '16 at 04:03
  • Unless you add *collision detection*, any object attracted by gravity will keep moving past the gravity center, and will then slow down again from the pull of the gravity that is now behind the object. That is how gravity works. – Andreas Apr 24 '16 at 04:46

3 Answers3

1

Obviously you change the speed by

yVel += ya;

etc. and thats an acceptable thing to you as it seems from your wording. But that will create a constantly increasing speed (i.e. flying by at the end).

If you want to keep the speed constant you do

if (x > game.getWidth() / 2) {
    xVel = -xa;
}

if (x < game.getWidth() / 2) {
    xVel = xa;
}

and equivalent. That will have the rectangle oscilate probably around the center. Please update and come up with new questions.

gpasch
  • 2,672
  • 3
  • 10
  • 12
1

Your spaceship's movement doesn't look smooth, because it is moving with no respect to time. What happens if you open up a CPU intensive application in the background, and your Java process gets less computing time? Now you are not calling move() as often, so your spaceship suddenly slows down.

You need to keep track of how much time has elapsed since the last call to move() - I'm sure you remember the high-school physics formula, distance = velocity * time. There are multiple ways to get the time - see here for a discussion specifically for games.

This will smooth out the movement a bit, but if you want the movement to look like gravity, then you can't just use velocity on its own - you also need to consider acceleration. Your space ship is accelerated towards the star due to its' gravity. The closer your spaceship gets to the star, the stronger the gravitational acceleration becomes. This is another high-school physics formula, which you should be able to find on the web.

Community
  • 1
  • 1
Andrew Williamson
  • 8,299
  • 3
  • 34
  • 62
  • Time * velocity will not work well for accelerating objects, especially if there is a second integral involved. You can't just multiply velocity by time, when velocity changes over time due to acceleration, and because acceleration is also changing over time the only option is to do all the math (a lot of math). For this type of sim you use verlet integration,or run at discrete time steps and accept the minimized error. Using frame time will give indeterminate and unmanageable error. Though I very much doubt frame rate is the problem. – Blindman67 Apr 28 '16 at 02:51
0

You have not supplied all the code but to me it sounds like you are using integers where you should be using floats or doubles.

If you decalre the variables like the following

int x;
int velX;

You will not get the precision needed for smooth animation (there is nothing after the full stop). With integers 5 / 2 == 2 and not 2.5 as integers can only store whole numbers. This is why you are getting jerky motion.

You need to use floating point variables by decalring them as such.

float x = 0.0f; 
float vecX;

or

double x = 0.0;
double vecX;

That will give you the smooth motion you want.

Blindman67
  • 51,134
  • 11
  • 73
  • 136