Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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  
  [LibGDX] Getting smooth camera movement  (Read 668 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771
« Posted 2014-02-06 14:25:06 »

I am in the process of creating a class that can handle camera positioning and effects behind the scenes and I am having an issue, I have this method here which you can use to have the camera follow the vector position of an object:

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  
public void follow(float delta, Vector2 position, float offsetX,
         float offsetY) {
      if (isFixed)
         throw new CameraSettingsException(
               "You can not follow a fixed camera, use the correct constructor for a camera that can follow a vector");

      System.out.println(delta / 4);

      if (shakeEnabled) {
         if (shaking) {
            processShake(delta);
         }
         if (super.position.x >= (position.x)) {
            super.position.x -= position.x * delta;
         }
         if (super.position.x <= (position.x)) {
            super.position.x += position.x * delta;
         }
         if (super.position.y > (position.y)) {
            super.position.x -= position.y * delta;
         }
         if (super.position.y < position.y) {
            super.position.x += position.y * delta;
         }

      } else {
         super.position.set(position.x, position.y, z1);
      }
   }


It is fairly simple and it works, to a degree.

It follows the player just fine, from left to right. As you can see I have a shake boolean in there, this simply causes the camera to spasm around a little then reset on the object smoothly, as if an explosion occurred nearby.

The problem being is,  jerky as hell, it is fine at the very left hand side but is super shaky at the right hand side. If I divide the delta time by 4 it works flawlessly but then the camera would not be able to keep up with whatever it is following if it moved quicker than the delta time. This seems due to the cameras x position increases as you move from left to right and is getting multiplied by the delta time, so it is obviously getting fired past the desired position and the next if statement in line is kicking in to knock it back, creating a back and forth between the 2 statements. If that makes sense.

I can easily use something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
if (super.position.x >= (position.x + 0.01f)) {
         super.position.x = position.x + offsetX;
      }
      if (super.position.x <= (position.x - 0.01f)) {
         super.position.x = position.x - offsetX;
      }
      if (super.position.y > (position.y + 0.01f)) {
         super.position.y = position.y + offsetY;
      }
      if (super.position.y < position.y - 0.01f) {
         super.position.y = position.y - offsetY;
      }



But if the camera was to shake and then recentre on the vector, it would snap into position, unlikes this other method which smooths it back into place :

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  
if (shakeEnabled) {
         if (shaking) {
            processShake(delta);
         } else {
            if (position.x > viewportWidth / 2) {
               position.set(position.x - (viewportWidth / 2) * delta
                     / 1.5f, position.y, 0);
            }
            if (position.x < viewportWidth - viewportWidth / 2) {
               position.set(position.x + (viewportWidth / 2) * delta
                     / 1.5f, position.y, 0);
            }
            if (position.y > viewportHeight / 2) {
               position.set(position.x, position.y - (viewportHeight / 2)
                     * delta / 1.5f, 0);
            }
            if (position.y < viewportHeight - viewportHeight / 2) {
               position.set(position.x, position.y + (viewportHeight / 2)
                     * delta / 1.5f, 0);
            }

         }

      } else {
         position.set(viewportWidth / 2, viewportHeight / 2, z1);
         System.out.println("Here");
      }


However the above code is just a basic camera position, never moves and in the middle of screen, so it's a much easier to do.

Any idea how I can get around this? I know for a fact it is going to be simple.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline kalkitus

Senior Newbie


Medals: 1
Exp: 6 years



« Reply #1 - Posted 2014-02-06 18:00:35 »

I am not sure if I understand your question correctly...but why don't you just lerp to the target position?

1  
2  
float smoothing = 0.5f; // lower the smoother 
super.position.lerp(position, smoothing);
Offline Gibbo3771
« Reply #2 - Posted 2014-02-06 18:12:08 »

I am not sure if I understand your question correctly...but why don't you just lerp to the target position?

1  
2  
float smoothing = 0.5f; // lower the smoother 
super.position.lerp(position, smoothing);


I had no idea that function existed...will try that

I shall try to clarify my question a little better.

I basically have my own camera class that has some built in functions, follow, centre, shake etc.
The follow method does not follow the given vector smoothly, it is all jittery. lol, as simple as I can put it.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gibbo3771
« Reply #3 - Posted 2014-02-06 18:21:32 »

Heh, fixed.

Sorry if you still don't understand my question but anyway, you saved me a whole bunch of code lol.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline kalkitus

Senior Newbie


Medals: 1
Exp: 6 years



« Reply #4 - Posted 2014-02-06 18:31:31 »

Glad to have at least taught you about the lerp function; it's very useful!
Offline Gibbo3771
« Reply #5 - Posted 2014-02-06 18:40:52 »

Glad to have at least taught you about the lerp function; it's very useful!

Yep! Now that I have found it, I can do some cool camera effects Cheesy Well kinda lol.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

pw (14 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!