1

Currently writing a small framework that I have to use for a school project, more precisely, a game of Pong. I'm using NetBeans to code. I'm trying to make the ball movement speed frame-independent which if I'm not wrong is given by the formula

New position = Last position + Speed * Time since the last frame

I chose to represent the ball's position using a 2D vector class I wrote (all it has is an x and y property and their respective getters/setters) so the method used to update the ball's position looks like so

public void update(double deltaTime) {
    this.position.setX(this.position.getX() + (this.speed.getX() * deltaTime));
    this.position.setY(this.position.getY() + (this.speed.getY() * deltaTime));

    this.texture.update(this.position);
}

When I run this the ball disappears off screen. I've only checked the X coordinate but this is what I got.

newX = 300 + 5 * 0.025

newX = -7.2444433823598585E9

I have no idea why this is and I'm completely lost. I've tried using the BigDecimal class but the result is the same.

EDIT: Vector class screenshot

EDIT2: Texture2D.update() and Test before and after setX()

EDIT3: Solved, the passed deltaTime was wrong in the deltaTime = currentTime - lastFrameTime because the currentTime was not initialized for the first iteration.

7
  • 1
    I think we need to see setX, setY, getX, and getY, are you sure you're not setting or retrieving the wrong field?
    – Bathsheba
    Nov 30, 2015 at 13:14
  • Imgur is blocked at school, here's a screenshot of the vector class, it's pretty straightforward though
    – 5unkEn
    Nov 30, 2015 at 13:21
  • Where you are checking the value of newX ?? Can you check before and after this code this.texture.update(this.position); so that we will know if this is causing some issue .. Are we need to know implementation of texture.update(); Nov 30, 2015 at 13:25
  • Test before and after operation - Seems to be working with values of 50, which remains weird... and the texture update function calls Swing methods
    – 5unkEn
    Nov 30, 2015 at 13:31
  • 1
    Please create a small, running example showing your issue. Right now, it's impossible to say why it doesn't work.
    – Manu
    Nov 30, 2015 at 13:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.