Skip to content

physics::collider

Paweł Waligóra edited this page Apr 25, 2024 · 9 revisions

This class serves as a base class for:

You should not use physics::collider but rather one of the above instead.

Functionality

physics::collider has 3 subscription lists:

  • on_collision_enter
  • on_collision_stay
  • on_collision_exit

Usage

Declare collider in a script scope.

Construct it's object with right parameters.

Subscribe to it's collision events lists or use it as a collider for rigidbody.

Example

example below shows how to use collider in a script as an event trigger as well as a collider for a rigidbody

#pragma once
#include <physics.h>
#include <scripts_system.h>

namespace game {
	class player : public scripts_system::script {
	public:
		player();
		// ...

	private:
		void jump();
		void land(physics::collision_info ci);

		physics::rigidbody rb; // if you want to use collider just for triggering events - skip the rigidbody
		physics::colliders::sphere col;

		float jump_force = 5.5f; // actually velocity [m/s]
		bool ready_to_jump = false;

		// ...
	};
}

game::player::player() : rb(), col(&rb, 1.5f) { // pass rigidbody to collider as constructor parameter

	rb.force = physics::gravity * rb.mass;

	// subscribe for collision event
	col.on_collision_enter.subscribe(std::bind(&game::player::land, this, std::placeholders::_1));
}

void game::player::jump()
{
	if (ready_to_jump) {
		ready_to_jump = false;
		rb.velocity.y += jump_force;
	}
}

void game::player::land(physics::collision_info ci) { // this will be called when collider enters a collision with any other collider
	if (!ready_to_jump) {
		ready_to_jump = true;
	}
 }

Home

Git Workflow

Issues

Coding Rules

Skrypty-Tutorial

Useful resources

Game Structure

Code Structure

Clone this wiki locally