Git Product home page Git Product logo

ergo's Introduction

Ergo

Fly a little ship around in a raylib project. The main purpose of this project was to figure out how to handle arbitrary rotations, since the built in rotation functions don't make this easy.

This ended up being another take on simple arcade space flight mechanics. Specifically, this is probably how I'd do a Rogue Squadron kind of game.

I'm really happy with the space dust and trails too. They look really neat, but I needed some help from the raylib Discord to get the transparencies working correctly. Sometimes you need to force a render batch with rlDrawRenderBatchActive() when messing with transparencies and the depth buffer.

The game can be controlled with either WASD/Arrow keys, or a gamepad.

Rotations

Check Actor.cpp and Ship.cpp for some silly looking and probably very ineffecient functions to rotate a quaternion representing the ship's rotation.

Getting a forward (or any direction) vector from some actor is something that needs to be very commonly done. I'm pretty sure there's a smarter way to do this, but I get it by multiplying the forward vector by the ship's Rotation:

Vector3 Actor::GetForward() const
{
	return Vector3RotateByQuaternion(
		Vector3{ 0, 0, 1 },
		Rotation);
}

Rotating an object in local space is another essential piece of code. In Unity this is a very simple Transform.Rotate(Vector3, Space.Self), but for raylib you have to write your own. This seemed to work.

void Actor::RotateLocalEuler(Vector3 axis, float degrees)
{
	auto radians = degrees * DEG2RAD;
	Rotation = QuaternionMultiply(
		Rotation,
		QuaternionFromAxisAngle(axis, radians));
}

While not rotation specific, transforming a point from local space to world space is another essential function. In Unity this is just Transform.TransformPoint(), but as far as I can tell raylib's Transform struct is very basic and used only by model rendering.

Vector3 Actor::TransformPoint(Vector3 point) const
{
	auto mPos = MatrixTranslate(Position.x, Position.y, Position.z);
	auto mRot = QuaternionToMatrix(Rotation);
	auto matrix = MatrixMultiply(mRot, mPos);
	return Vector3Transform(point, matrix);
}

Smoothing and Ship Flight

I've been asked about how this looks so smooth a lot, and it's SmoothDamps all the way down. I use the SmoothDamp function in everything I work on because it's an easy way to add smoothing in a way that isn't (usually) affected by framerate.

inline float SmoothDamp(float from, float to, float speed, float dt)
{
	return Lerp(from, to, 1 - expf(-speed * dt));
}

In this specific implementation, smoothing is applied to the inputs the player gives, and the ship's own velocity. For rotations, the smoothing is applied only to the input, not to any kind of angular velocity.

For example, here's the smoothing applied to the ship input.

// Give the ship some momentum when accelerating.
SmoothForward = SmoothDamp(SmoothForward, InputForward, ThrottleResponse, deltaTime);
SmoothLeft = SmoothDamp(SmoothLeft, InputLeft, ThrottleResponse, deltaTime);
SmoothUp = SmoothDamp(SmoothUp, InputUp, ThrottleResponse, deltaTime);

After the velocity the ship should have is figured out, smoothing is applied to move the current velocity to the target velocity.

Velocity = SmoothDamp(Velocity, targetVelocity, 2.5, deltaTime);
Position = Vector3Add(Position, Vector3Scale(Velocity, deltaTime));

Arbitrary Render Resolution

There's also some messy code in there for rendering the game at an arbitrary resolution and separate from the display resolution. This is something that always interests me because I have an unhealthy rose-tinted nostalgia for DOS games.

This rendering mode can be used by uncommenting #define RENDER_SMALL at the top of the Ergo.cpp.

ergo's People

Contributors

brihernandez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.