Quick Otter Camera Example
A question about the camera in Otter came up on the forums, and I whipped up a small example that could be useful to take a look at for getting started with Otter in general. The whole program is just this little chunk of code:
Compiling that with the latest dev version of Otter gives you a simple player object that can move around a scene. The background is a grid so that you can actually see scrolling happening. This quickly shows how to set up a movement Axis, how to apply speed to an object while getting input from the Axis, and how to center the camera on the object.
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);
}
}
}
Compiling that with the latest dev version of Otter gives you a simple player object that can move around a scene. The background is a grid so that you can actually see scrolling happening. This quickly shows how to set up a movement Axis, how to apply speed to an object while getting input from the Axis, and how to center the camera on the object.
2 Comments