Hello Guest

Author Topic: Camera  (Read 48798 times)

0 Members and 1 Guest are viewing this topic.

poole.shane

  • Member
  • Posts: 12
    • View Profile
Camera
« on: June 18, 2014, 04:56:00 AM »
I'm trying to get the camera to follow the player entity in the scene. It works, but somehow it's rather jittery when I move the character and the screen/camera moves along with it. It just isn't smooth at all. I did the following:

Code: [Select]
public override void Update()
        {
            base.Update();

            CameraFollow();
        }

        public void CameraFollow()
        {
            CameraX = TargetX;
            CameraY = TargetY;
        }

        private float TargetX
        {
            get
            {
                return player.X - Game.Instance.HalfWidth;
            }
        }

        private float TargetY
        {
            get
            {
                return player.Y - Game.Instance.HalfHeight;
            }
        }

My game's resolution is 320X180.

I tested this in Flashpunk in AS3 and it was buttery smooth. I wonder what it is I'm doing wrong?

I apologise if I'm asking a silly question. I'm a novice programmer and only started using Otter and C# about just over a week ago. My apologies if I'm being redundant, but any help would be much appreciated!
I'll just leave this here: https://twitter.com/S_L_Poole

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Camera
« Reply #1 on: June 18, 2014, 12:21:03 PM »
Try doing Math.Floor or Math.Round on the TargetX and TargetY when assigning the CameraX and CameraY values.

poole.shane

  • Member
  • Posts: 12
    • View Profile
Re: Camera
« Reply #2 on: June 19, 2014, 02:34:31 AM »
Something like this?:

Code: [Select]
public void CameraFollow()
        {
            CameraX = (float)Math.Round(TargetX);
            CameraY = (float)Math.Round(TargetY);
        }

I tried both Math.Round and Math.Floor. The strange thing is, when my character is moving at certain speeds, it's super smooth, but when I slow down or speed up even just a little bit, it becomes jittery again...
I'll just leave this here: https://twitter.com/S_L_Poole

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Camera
« Reply #3 on: June 19, 2014, 05:32:24 PM »
If you change Round to Floor, does it still jitter?

poole.shane

  • Member
  • Posts: 12
    • View Profile
Re: Camera
« Reply #4 on: June 20, 2014, 04:54:03 AM »
Yep, I tried both Math.Floor and Math.Round and it still jitters. The strange thing is that depending on the speed at which the character is moving, the intensity of the jitters differ.

I made a new class called Moveable.cs that extends Entity.cs, and I'm extending that with a class dedicated to the character. Moveable.cs is basically a copy of PlatformingMovement.cs with a couple of small differences (most obvious difference is that it's not used as a component). So the SpeedScale value is set to 1000 and the jitters vary depending on the character's xSpeedMax when pushing the analogue stick all the way. I also tested this by using PlatformingMovement.cs as a component and found that the problem persists.

With the xSpeedMax set to 197, the character moves quite smoothly with very little jitter. Deviate from that value just a little bit and the jitters immediately begin to appear again...
I'll just leave this here: https://twitter.com/S_L_Poole

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Camera
« Reply #5 on: June 20, 2014, 08:01:51 PM »
Hmm okay, I'll look into this!

poole.shane

  • Member
  • Posts: 12
    • View Profile
Re: Camera
« Reply #6 on: June 20, 2014, 09:55:35 PM »
Thanks! Let me know if you have any more questions!
I'll just leave this here: https://twitter.com/S_L_Poole

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Camera
« Reply #7 on: June 29, 2014, 06:58:30 PM »
I'm starting to look into this a little bit now.  I put together a quick example on how I would do camera centering on a player (or any object) and can't get it to jitter.  I haven't tried to reproduce it with your method yet though.  Here's the full code for my example:

Code: [Select]
using Otter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CameraTestThing {
    class Program {
        static void Main(string[] args) {

            var game = new Game("Camera Test", 320, 180);
            game.SetWindowScale(4);
            game.Start(new TestScene());
        }
    }

    class TestScene : Scene {

        public TestScene() {
            var grid = new Grid(1000, 1000, 20, 20, Color.Grey);
            AddGraphic(grid);
            Add(new Player());
        }
    }

    class Player : Entity {

        Vector2 speed = new Vector2();
        Vector2 targetSpeed = new Vector2();
        Vector2 maxSpeed = new Vector2(2, 2);
        float accel = 0.01f;

        Axis movementAxis = Axis.CreateArrowKeys();

        public Player() {
            SetGraphic(Image.CreateRectangle(10, Color.Red));
            AddComponent(movementAxis);
        }

        public override void Update() {
            base.Update();

            targetSpeed.X = movementAxis.X * maxSpeed.X;
            targetSpeed.Y = movementAxis.Y * maxSpeed.Y;

            speed.X = Util.Approach(speed.X, targetSpeed.X, accel);
            speed.Y = Util.Approach(speed.Y, targetSpeed.Y, accel);

            X += speed.X;
            Y += speed.Y;

            Scene.CenterCamera(X, Y);
        }
    }
}

Note that I'm using some new Otter stuff that is available in the latest Dev branch, so make sure you're using that before trying to build this.  I'm going to look more closely at the Movement and PlatformingMovement stuff next.
« Last Edit: June 29, 2014, 07:00:36 PM by Kyle »

poole.shane

  • Member
  • Posts: 12
    • View Profile
Re: Camera
« Reply #8 on: July 04, 2014, 05:59:11 AM »
Okay, sorry for my late reply. Things have been hectic recently so I haven't been checking the forums much.

So, I read your reply and played around for a bit. I found that putting this line "Scene.CenterCamera(X, Y);" in the player entity's class under the update method (obviously) completley removes the jitters. But putting the same line (CenterCamera(player.X, player.Y);) in the scene's class extension (which I'm currently calling "FirstScene.cs") causes jitters when moving the player around...

So yeah, the jitters are gone when the line is implemented in the entity's class but they appear when it's in the scene class. I prefer putting it in the scene class for when I want to edit camera behavior and such, but I'll keep it in the player class for now. I'm not using the dev branch yet (as I don't yet know how to quickly change the otter version I'm using), but I'll use it at a later date.

Anyway, thanks for your help!
I'll just leave this here: https://twitter.com/S_L_Poole

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Camera
« Reply #9 on: July 04, 2014, 05:45:36 PM »
Are you centering the camera in the scene before the player has moved?  If you take my example and do the same thing, there will be jittering, and this is most likely because you are centering the camera before the player has moved each update.

If you're looking to put the center camera code in your FirstScene.cs, then try this:

Code: [Select]
public override void UpdateLast() {
    base.UpdateLast();

    CenterCamera(player.X, player.Y);
}

poole.shane

  • Member
  • Posts: 12
    • View Profile
Re: Camera
« Reply #10 on: July 04, 2014, 07:48:39 PM »
*facepalm* I knew I was making some sort of n00b mistake. It works like a champ now.

I thought that it may be some kind of update problem, but it never occurred to me that I should try and use UpdateLast() and center the camera in there.

Sorry for the confusion. And thanks!
I'll just leave this here: https://twitter.com/S_L_Poole