Drift for 2D Space Game

GUYS IM NOT DEAD. My computer just crashed and I managed to install ubuntu on it to start programming again. I went 2 months without programming D:! Anyways, remember those old top down racing games where you just guided that car along the track and get as much “drift” as possible? How would I implement that in my libgdx game? Just in case you need it, here’s the spaceship class:


package com.slyth.spaceGame.Models;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class Ship extends MovableEntity {

	public boolean left = false, right = false, up = false, down = false, isKeyDown = false;
	float rotateSpeed = 30, health = 100, SPEEDORIG;
	Rectangle pickupBounds;
	boolean isUpgradedSpeed = false;
	public int usT = 0, usP = 400;

	public Ship(Vector2 position, float width, float height, float rotation, float SPEED) {
		super(SPEED, rotation, width, height, position);
		this.SPEEDORIG = SPEED;
		pickupBounds = new Rectangle(position.x - ((width * 3) / 2), position.y - ((height * 3) / 2), width * 3, height * 3);
	}

	public void update() {
		if (isUpgradedSpeed) {
			if (usT >= usP) {
				isUpgradedSpeed = false;
				SPEED = SPEEDORIG;
				usP = 400;
				usT = 0;
			} else {
				SPEED = SPEEDORIG + 2;
				usT++;
			}
		}
		pickupBounds = new Rectangle(position.x - ((width * 10) / 2.5f), position.y - ((height * 10) / 2.5f), width * 10, height * 10);
		if (left) {
			if (rotation <= 360) {
				rotation += .1f * rotateSpeed - 0.03;
			} else {
				rotation = 0;
				rotation += .1f * rotateSpeed - 0.03;
			}

		} else if (right) {
			if (rotation >= 0) {
				rotation += -.1f * rotateSpeed - 0.03;
			} else {
				rotation = 360;
				rotation += -.1f * rotateSpeed - 0.03;
			}
		}
		if (up) {
			float yP = MathUtils.sinDeg(rotation + 90) * SPEED;
			float xP = MathUtils.cosDeg(rotation + 90) * SPEED;

			velocity.y = yP;
			velocity.x = xP;
		} else if (!up && !down) {
			velocity.x *= .95;
			velocity.y *= .95;
		} else if (down) {
			float yP = MathUtils.sinDeg(rotation + 90) * SPEED;
			float xP = MathUtils.cosDeg(rotation + 90) * SPEED;

			velocity.y = -yP;
			velocity.x = -xP;
		}
		if(!isKeyDown){

		}
		position.add(velocity.tmp().mul(Gdx.graphics.getDeltaTime() * SPEED));

		bounds.x = position.x;
		bounds.y = position.y;
	}

	public float getHealth() {
		return health;
	}

	public Rectangle getPickupBounds() {
		return pickupBounds;
	}

	public void setHealth(float am) {
		health = am;
	}

	public void loseHealth(float am) {
		health -= am;
	}

	public void heal(float am) {
		health += am;
	}

	public void upgradeSpeed() {
		if (!isUpgradedSpeed) {
			isUpgradedSpeed = true;
		} else {
			usP += 400;
		}
	}

	public boolean isSpeedUpgraded() {
		return isUpgradedSpeed;
	}
}

Thanks
-cMp

Drifting is caused when you change the direction you’re facing, without changing the direction of your motion (at least temporarily).

Think about that for a bit.

so I should do some sort of time step after I rotate to move in the same direction for a small bit? From what I am thinking that actually would make it like a racing game. I didn’t make it clear, I need the effect of 0 gravity so that it would drag like a racing game, but still move in the direction you are facing.

That would be fine. I recommend doing that.

But for a more realistic effect it would be a good idea to learn basic kinematics.

Will do, thanks. Also, if you know of an easier/better way to do time steps in libGDX instead of the standard


if(firstVar >= secVar){
var++:
firstVar=0;
}else{
firstVar++:
}

could you tell me?

What do you mean by time step?

the process where you increase or decrease a variable over time in the code. An example is up there in the ship class where if the ship is not being moved I do velocity.x *= .98f to make the ship slow down over time. However the type I meant was where you have a first variable (varOne), and a second (varTwo), and compare them to eachother to edit a variable over time. Hard to explain I guess :stuck_out_tongue:

So every X ticks, you increment a variable by one?

In that case:


firstVar--;
if(firstVar == 0)
{
    firstVar = secVar;
    var++;
}

Kind of does the same thing. Not sure if it’s more optimised. It’s just easier to code.

Ah ok, thanks. I have been searching the kinematics you suggested and I haven’t found anything related to drifting. Is there some simple math that you can write down in just a couple seconds if it doesn’t bother you? Thanks


// Vector2 facingVector
// Vector2 movingVector
// float inertia
// float acceleration

movingVector -= multiply(normalize(movingVector), inertia);
movingVector += multiply(normalize(facingVector), acceleration);

Requires some knowledge of vector math.
Can probably be optimised.

Gotcha, thanks!