Skip to content

physics::rigidbody

Paweł Waligóra edited this page May 4, 2024 · 12 revisions

signature: physics::rigidbody

files: rigidbody.h, rigidbody.cpp

Declaration

namespace physics {
	class rigidbody {
	public:
		bool dynamic = true;
		glm::vec3 position = glm::vec3(0.0f);
		glm::quat rotation = glm::quat(glm::vec3(0, 0, 0));
		float mass = 10.0f; // [kg]
		float moment_of_inertia = 10.0f; // [kg*m^2]
		glm::vec3 velocity = glm::vec3(0.0f); // [m/s]
		glm::quat angular_speed = glm::quat(glm::vec3(0, 0, 0));
		glm::vec3 force = glm::vec3(0.0f); // [N]
		glm::vec3 torque = glm::vec3(0.0f);

		void update();
	};
}

Methods

physics::rigidbody::update()

Called exactly once per frame in every frame by physics system.

Usage

Just declare member of type phyics::rigidbody in your script. You can read and write to it's position, rotation, velocity, angular velocity, force and torque by accessing rigidbody members directly. Rigidbodies update automatically - no additional work needed.

In order for rigidbody to collide with other rigidbodies you will need to add a physics::collider as well.

Example

#include "rigidbody.h"
#include "physics.h" // physics::gravity
// (btw. physics.h includes rigidbody.h, so first line not necessary)

namespace game {
	class my_script {
	private:
		physics::rigidbody rb;
	public:
		void start() override;
		void update() override;
	};
}

game::my_script::start() {
	rb.mass = 20.0f; // kg
	rb.force = physics::gravity * rb.mass;
}

game::my_script::update() {
	printf("%f, %f, %f\n", rb.position.x, rb.position.y, rb.position.z);
}

Home

Git Workflow

Issues

Coding Rules

Skrypty-Tutorial

Useful resources

Game Structure

Code Structure

Clone this wiki locally