How to create a simple ego perspective camera?

Hello there, total beginner trying to get my feet wet. I think I have some “old-school” or “different” expectations on how to create a player with a first person perspective camera and attach input to it.

I want to create a simple car game where every player sits inside a car and steers it with some input buttons. The cars will have custom behavior / physics eventually. I thought of creating each player in an object hierarchy like this:

  • Collision body
    • Visible mesh (hidden for current player or replaced with cockpit / HUD mesh)
    • Inside Camera (active only for local player)

That way, moving and coding the behavior of the collision body would move the visible mesh and camera in sync.

I tried the existing cameras in Babylon.js which did not really work out:

  • Camera: Thought this simple base class would suffice, but the view does not change when I move the mesh I set as its parent, nor when I directly set the camera’s position.
  • FreeCamera/FlyCamera: These ones were the only ones with collision properties like ellipsoid, applyGravity or checkCollisions. Those surprised me in being specific to a camera. I’m not sure how I would allow opponent players participate in physics then, as they would not have a camera at all.
  • FollowCamera: Followed my mesh at least, but had too many “extra features” like interpolation / smoothing, and even trying to disable all those by changing its properties like heightOffset or radius to 0 still caused it behave weird eventually. This is the only attempt I kept: Babylon.js Playground

Am I on the wrong path here? Do I have to write my own Camera class from scratch to attach it to an object like that? Do I need to use a physics library for physics on other players?

Hi RayKoopa,

First of all, welcome to Babylon!

Cool idea! The relationship between Babylon’s default cameras and collision systems can be a bit confusing, partly because vanilla Babylon doesn’t have a fully-featured physics engine built in (Ammo, Cannon, and others are available as plugins). The hierarchy you described makes a lot of sense, and I think it’s actually pretty close to what we did in…

This repo contains a kart racer the Babylon team made for fun last year. In order to have complete control so that we could get the feel of the karts right, we used a FreeCamera but ignored all its default capabilities and implemented custom collision and physics ourselves. It might work a little differently if you’re going for a less arcade-y feel (and of course you’ll want the camera to be inside the kart, not above and behind it), but this implementation might serve as a good reference to get you started. I don’t think anybody’s tried it in a few months; but last time we checked, it still worked. :smiley: Good luck!

4 Likes

Thanks for that. That’s a nice sample project to study first which should answer a lot of my questions :slight_smile:
I’m not sure if I can get FreeCamera to work for an ego perspective as it still tries to “follow” something and turns around suddenly or wobbles (as seen my playground above); I think I’ll try “copying” the FreeCamera class from the engine source and steam it down to what I need (basically just statically following the parent orientation).