Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  smoother x/y-axis increments  (Read 3522 times)
0 Members and 1 Guest are viewing this topic.
Offline zyzz

Senior Newbie





« Posted 2012-03-18 02:45:40 »

Is there a way to increment the x/y coordinates of a render smoothly by merely just incrementing it by 1 'faster'?

I've come across situations in which I am changing the speed/rate of travel of an object by just adding a larger value to the x/y coordinates of said object

so, something like

x += 5;

would make it appear to go faster but instead result in choppy rendering (since you're adding a large value like 5 instead of progressively adding values of 1)

in essence:

is there a way to 'speed up' incrementing by 1 (ie. ++x) in order to reduce choppiness when changing the rate of travel of an object?
Offline GabrielBailey74
« Reply #1 - Posted 2012-03-18 02:56:00 »

Yes you can I believe like this example:
(Ints)
1  
2  
3  
4  
5  
   public void move() {
      if (player.movingRight()) {
         player.absX++;
      }
   }


Or if you're using a 'double':
1  
2  
3  
4  
5  
   public void move() {
      if (player.movingRight()) {
         player.absX = player.absX += 0.1;
      }
   }


If that's not what you're looking for let me know.

Offline gbeebe
« Reply #2 - Posted 2012-03-18 03:03:19 »

If you're using any sort of timer that updates (or allows updating) of the sprite position when time's up, then you can just use the speed of the sprite to calculate the new timer time.  Each sprite would need it's own timer, but that's normal.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zyzz

Senior Newbie





« Reply #3 - Posted 2012-03-18 03:05:47 »

...
Or if you're using a 'double':
1  
2  
3  
4  
5  
   public void move() {
      if (player.movingRight()) {
         player.absX = player.absX += 0.1;
      }
   }


If that's not what you're looking for let me know.

I am referring to faster increments of the x/y-axis.

If you're using any sort of timer that updates (or allows updating) of the sprite position when time's up, then you can just use the speed of the sprite to calculate the new timer time.  Each sprite would need it's own timer, but that's normal.

not sure how that would play out, maybe if I were to increase the time depending on how long the key is held? I'm mainly having trouble increasing the value by 1 fast enough so that I can move at a fast pace but still prevent choppiness

it's like basically trying to scroll over 200 pixels when you could just increment by like 50 pixels and get there extremely quickly whereas you'd increment by 1 pixel very quickly and get there as quick (except not producing a choppy, 'bursty' travel)
Offline GabrielBailey74
« Reply #4 - Posted 2012-03-18 04:16:15 »

Slowly increasing?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   public int elapsed = 0; // milleseconds.
   public int desiredLimit = 12; // max xSpeed
   public int desiredFix = 30; // somewhere between 0 and 80.
   public int xSpeed = 2; // start out slow.
   
   public void slowlyIncrease() {
      elapsed++;
      if (elapsed > 0 && elapsed < 80) {
         xSpeed = 4;
      } else if (elapsed > 80 && elapsed < 180) {
         xSpeed = 8;
      } else if (elapsed > 180 && elapsed < 280) {
         xSpeed = 8;
      } else if (elapsed > 280 && elapsed < 380) {
         xSpeed =10;
      } else if (xSpeed > desiredLimit) {
         xSpeed = desiredLimit;
         elapsed = desiredFix;
      }
   }


Adjusted to your liking's of course.

Offline ra4king

JGO Kernel


Medals: 352
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-03-18 04:51:59 »

Or, you know, do it the normal way and have an acceleration value Grin

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private double x, vX = 50, aX = 2;
private double y, vY = 50, aY = 2;

public void update() {
    vX += aX;
    x += vX;

    vY = aY;
    y = vY;
}


1  
2  
3  
4  
5  
   public void move() {
      if (player.movingRight()) {
         player.absX = player.absX += 0.1;
      }
   }

What's with the double assignment?
1  
player.absX += 0.1;

That is all you need.

Offline GabrielBailey74
« Reply #6 - Posted 2012-03-18 04:58:13 »

I was messing around in a class.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
   private static void randomMovement() {
      absX = absX + x1; // hint
      absY = absY + y1; // hint, hint
      if (absX < 40) {
         absX = 40;
      }
      if (absY+20 > 520) {
         absY = 500;
      }
      if (absX+20 > 760) {
         absX = 760-20;
      }
      if (absY < 40) {
         absY = 40;
      }
      if (absX < Player.getAbsX()) {
         x1++;
      } else if (absX > Player.getAbsX()) {
         x1--;
      }
      if (absY < Player.getAbsY()) {
         y1++;
      } else if (absY > Player.getAbsY()) {
         y1--;
      }
   }

Offline DrZoidberg

Senior Duke


Medals: 15



« Reply #7 - Posted 2012-03-18 06:00:34 »

Smoothness depends on your framerate. If you have enough fps and you update the position of all objects every frame it should be perfectly smooth.

Look at this code.
It's an example of a simple game loop that tries to achieve a given target fps.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
class Sprite {
    double  posX, posY, speedX, speedY;
   
    public void update(double time) {
        posX += time*speedX;
        posY += time*speedY;
    }
}

class Game extends JPanel {
    double targetFPS = 60; // try to achieve 60 fps
    double timePerFrame = 1.0/targetFPS;
    ArrayList<Sprite> sprites = new ArrayList<Sprite>();
    //for double buffering
    Image image;
   
    public void start() {
        image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        int sleep=(int)(timePerFrame*1000);
        double timeOfLastFrame = System.nanoTime()/1e9;
        double startTime = timeOfLastFrame;
        int frames = 0;
        while(true) {
            double time = System.nanoTime()/1e9;
            double timePassed = time - timeOfLastFrame;
            timeOfLastFrame = time;
            for(s: sprites) {
                s.update(timePassed);
            }
            Graphics g = image.getGraphics();
            g.setColor(backgroundColor);
            g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
            for(s: sprites) {
                s.paint(g);
            }
            g.dispose();
            g = getGraphics();
            g.drawImage(image, 0, 0 ,null);
            g.dispose();
            try {
                if(timePassed>timePerFrame && sleep > 0) sleep--;
                else if(timePassed<timePerFrame) sleep++;
                Thread.sleep(sleep);
            } catch (Exception e) {
            }
            frames++;
            time=timeOfLastFrame-startTime;
            if(time >= 1) {
                double fps=frames/time;
                frames=0;
                System.out.println("FPS: "+fps);
                startTime = timeOfLastFrame;
            }
        }
    }
    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }
}
Offline ra4king

JGO Kernel


Medals: 352
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-03-18 06:03:40 »

You want to divide timePassed by 1e9 before multiplaying by speed Wink

Offline DrZoidberg

Senior Duke


Medals: 15



« Reply #9 - Posted 2012-03-18 06:12:23 »

Why? timePassed already is in seconds. No need to divide it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 352
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2012-03-18 06:42:28 »

Oh! I didn't see that division by 1e9 on the actual timeOfLastFrame. However I do advise against that since it is quite inaccurate. You should be only dividing the final raw timePassed value.

Offline DrZoidberg

Senior Duke


Medals: 15



« Reply #11 - Posted 2012-03-18 07:00:04 »

Yes, I guess you are right. Although the inaccuracy is actually very small - less then a ┬Ás. If I was using a float instead of a double however it could be a problem.
Offline zyzz

Senior Newbie





« Reply #12 - Posted 2012-03-18 13:11:57 »

Thanks, all. I'll try out some of your solutions and report back.

edit: I found a solution by using the delta*moveSpeed method.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (53 views)
2014-10-17 03:59:02

Norakomi (47 views)
2014-10-16 15:22:06

Norakomi (35 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (46 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!