Mathematical functions in gdscript- how to do?

Godot Version

4.2.1

Question

I’m not really sure how to make 2d math in gdscript.
I’m very used to using programs like desmos, maple, geogebra and the like but I’m not sure how to translate that to gdscript

For example if you want to make to make a top down racing game. And wanted the car to jump off a ramp. How could you simulate the curve a car driving off a ramp would have?

the real world formula would look like a throwing parabola but I’m not sure how to simulate the x in f(x)

Any and help is appreciated :slight_smile:

I think godot has some math docs Math — Godot Engine (stable) documentation in English
And also most of the time the maths are done in the physics engine.

3 Likes

Hi,

Translating functions like in Desmos in gdscript is just a straightforward translation.
Consider this Desmos curve:
image

It would result in a function that look like this:

func quad_curve(x: float) -> float:
	return x * x

Another way you can use curves is by exporting curves in the Inspector, like this:

@export var curve: Curve

And sample at x using curve.sample(x)
Exporting a curve allows you to edit it like this:


Let me know if that helps.
And, as @Frozen_Fried, make sure to first check what’s already provided by the engine itself :slight_smile:

3 Likes