Skip to content

Commit

Permalink
wip: started on TAR physics v2
Browse files Browse the repository at this point in the history
  • Loading branch information
zoogies committed Dec 4, 2024
1 parent 599dae4 commit 514951f
Show file tree
Hide file tree
Showing 28 changed files with 728 additions and 599 deletions.
7 changes: 7 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 152,10 @@ components have aspect ratio lock sensibly (button)

- give default admin commands
- go deep with it, let us modify the engine runtime state

## TAR PHYSICS

- collision detection
- solver
- LUA API's for colliders and rigidbodies
- triple check accounting for rotation in all the other systems... should button, audiosource, be effected? should relative spin around the center of the transform??
51 changes: 31 additions & 20 deletions engine/dist/include/yoyoengine/ecs/collider.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 25,19 @@
#include <yoyoengine/ecs/ecs.h>
#include <yoyoengine/utils.h>

enum ye_collider_type {
YE_COLLIDER_RECT = 0,
YE_COLLIDER_CIRCLE = 1
};

// v2, meh
// enum ye_collider_flags {
// YE_COLLIDER_TRIGGER = 1 << 0,
// YE_COLLIDER_STATIC = 1 << 1,
// YE_COLLIDER_RECT = 1 << 2,
// YE_COLLIDER_CIRCLE = 1 << 3
// };

/**
* @brief A structure representing a collider component.
*
Expand All @@ -35,32 48,30 @@ struct ye_component_collider {
bool active; /**< Controls whether system will act upon this component. */

bool relative; /**< Specifies whether this component is relative to a parent transform. */

struct ye_rectf rect; /**< The collider rectangle. */

bool is_trigger; /**< Specifies whether this collider is a trigger. If false, it is a static collider. */

/*
Meta tracking trigger states
*/
// bool _trigger_entered;
enum ye_collider_type type; /**< The type of collider. */
float x, y;
// TODO: not sure how i feel about anonymous structs in the union...
union {
// YE_COLLIDER_RECT
struct { float width, height; };

// YE_COLLIDER_CIRCLE
struct { float radius; };
};
};

/**
* @brief Adds a static collider to an entity.
*
* @param entity The entity to which the collider is to be added.
* @param rect The rectangle defining the collider.
*/
YE_API void ye_add_static_collider_component(struct ye_entity *entity, struct ye_rectf rect);
///////////////////////////////////////////////////

/**
* @brief Adds a trigger collider to an entity.
*
* @param entity The entity to which the collider is to be added.
* @param rect The rectangle defining the collider.
*/
YE_API void ye_add_trigger_collider_component(struct ye_entity *entity, struct ye_rectf rect);
YE_API void ye_add_static_rect_collider_component(struct ye_entity *entity, float x, float y, float w, float h);
YE_API void ye_add_trigger_rect_collider_component(struct ye_entity *entity, float x, float y, float w, float h);

YE_API void ye_add_static_circle_collider_component(struct ye_entity *entity, float x, float y, float radius);
YE_API void ye_add_trigger_circle_collider_component(struct ye_entity *entity, float x, float y, float radius);

///////////////////////////////////////////////////

/**
* @brief Removes an entity's collider component.
Expand Down
14 changes: 4 additions & 10 deletions engine/dist/include/yoyoengine/ecs/ecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 17,8 @@

#include <stdbool.h>

#include <yoyoengine/types.h>

/*
=============================================================
ENTITY LISTS
Expand All @@ -28,12 30,12 @@ YE_API extern struct ye_entity_node *entity_list_head;
YE_API extern struct ye_entity_node *transform_list_head;
YE_API extern struct ye_entity_node *renderer_list_head;
YE_API extern struct ye_entity_node *camera_list_head;
YE_API extern struct ye_entity_node *physics_list_head;
YE_API extern struct ye_entity_node *tag_list_head;
YE_API extern struct ye_entity_node *collider_list_head;
YE_API extern struct ye_entity_node *lua_script_list_head;
YE_API extern struct ye_entity_node *audiosource_list_head;
YE_API extern struct ye_entity_node *button_list_head;
YE_API extern struct ye_entity_node *rigidbody_list_head;

/**
* @brief Linked list structure for storing entities
Expand Down Expand Up @@ -95,18 97,10 @@ struct ye_entity {
struct ye_component_lua_script *lua_script; // lua script component
struct ye_component_button *button; // button component
struct ye_component_camera *camera; // camera component
struct ye_component_physics *physics; // physics component
struct ye_component_collider *collider; // collider component
struct ye_component_tag *tag; // tag component
struct ye_component_audiosource *audiosource; // audiosource component
};

/**
* @brief 2D vector structure
*/
struct ye_vec2f {
float x;
float y;
struct ye_component_rigidbody *rigidbody; // rigidbody component
};

/*
Expand Down
63 changes: 0 additions & 63 deletions engine/dist/include/yoyoengine/ecs/physics.h

This file was deleted.

2 changes: 2 additions & 0 deletions engine/dist/include/yoyoengine/ecs/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,8 @@

#include <yoyoengine/utils.h>

#include <yoyoengine/types.h>

/**
* @enum ye_component_renderer_type
* @brief Enum to store different unique types of renderers.
Expand Down
3 changes: 3 additions & 0 deletions engine/dist/include/yoyoengine/ecs/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 27,9 @@ struct ye_component_transform {

float x; // the transform x position
float y; // the transform y position

// physics2
float rotation; // clockwise rotation in degrees
};

/**
Expand Down
4 changes: 2 additions & 2 deletions engine/dist/include/yoyoengine/lua_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 50,8 @@ YE_API void ye_lua_renderer_register(lua_State *L);
// Button
YE_API void ye_lua_button_register(lua_State *L);

// Physics
YE_API void ye_lua_physics_register(lua_State *L);
// Rigidbody
YE_API void ye_lua_rigidbody_register(lua_State *L);

// Collider
YE_API void ye_lua_collider_register(lua_State *L);
Expand Down
40 changes: 40 additions & 0 deletions engine/dist/include/yoyoengine/tar_physics/rigidbody.h
Original file line number Diff line number Diff line change
@@ -0,0 1,40 @@
/*
This file is a part of yoyoengine. (https://github.com/yoyoengine)
Copyright (C) 2023-2024 Ryan Zmuda
Licensed under the MIT license. See LICENSE file in the project root for details.
*/

#ifndef YE_RIGIDBODY_H
#define YE_RIGIDBODY_H

#include <stdbool.h>

#include <yoyoengine/export.h>

#include <yoyoengine/ecs/ecs.h>

// TODO: gravity as a vec2 for flexibility (in physics main, not here)
// note: will be applying impulses directly to velocity

struct ye_component_rigidbody {
bool active; /**< Controls whether system will act upon this component. */

// TODO: inverse mass for performance
float mass; /**< The mass of the rigidbody. */

float restitution; /**< The "bounciness" of the rigidbody. */

float kinematic_friction; /**< The friction of the rigidbody. */
float rotational_kinematic_friction; /**< The rotational friction of the rigidbody. */

struct ye_vec2f velocity; /**< Velocity of entity */
float rotational_velocity; /**< Rotational velocity of entity */
};

// TODO: freeze axes?

YE_API void ye_add_rigidbody_component(struct ye_entity *ent, float mass, float restitution, float kinematic_friction, float rotational_kinematic_friction);
YE_API void ye_remove_rigidbody_component(struct ye_entity *ent);

#endif // YE_RIGIDBODY_H
24 changes: 24 additions & 0 deletions engine/dist/include/yoyoengine/tar_physics/solver.h
Original file line number Diff line number Diff line change
@@ -0,0 1,24 @@
/*
This file is a part of yoyoengine. (https://github.com/yoyoengine)
Copyright (C) 2023-2024 Ryan Zmuda
Licensed under the MIT license. See LICENSE file in the project root for details.
*/

#ifndef YE_SOLVER_H
#define YE_SOLVER_H

#include <stdbool.h>

#include <yoyoengine/export.h>

#include <yoyoengine/ecs/ecs.h>


YE_API bool ye_detect_circle_circle_collision(struct ye_vec2f pos1, float radius1, struct ye_vec2f pos2, float radius2);

YE_API bool ye_detect_circle_rect_collision(struct ye_vec2f circle_pos, float circle_radius, struct ye_rectf rect, float rotation);

YE_API bool ye_detect_rect_rect_collision(struct ye_rectf rect1, float rotation1, struct ye_rectf rect2, float rotation2);

#endif // YE_SOLVER_H
24 changes: 24 additions & 0 deletions engine/dist/include/yoyoengine/tar_physics/tar.h
Original file line number Diff line number Diff line change
@@ -0,0 1,24 @@
/*
This file is a part of yoyoengine. (https://github.com/yoyoengine)
Copyright (C) 2023-2024 Ryan Zmuda
Licensed under the MIT license. See LICENSE file in the project root for details.
*/

/*
PHYSICS SYSTEM V2:
A simple velocity (immediate impulse application) physics system.
FUTURE WISHLIST:
- physics thread
- advanced paramters (drag, static friction, etc)
*/

#ifndef YE_TAR_H
#define YE_TAR_H

#include <yoyoengine/export.h>

YE_API void ye_physics_tick(float dt);

#endif // YE_TAR_H
25 changes: 25 additions & 0 deletions engine/dist/include/yoyoengine/types.h
Original file line number Diff line number Diff line change
@@ -0,0 1,25 @@
/*
This file is a part of yoyoengine. (https://github.com/yoyoengine)
Copyright (C) 2023-2024 Ryan Zmuda
Licensed under the MIT license. See LICENSE file in the project root for details.
*/

#ifndef YE_TYPES_H
#define YE_TYPES_H

/**
* @brief A rectangle made up of floats.
*/
struct ye_rectf {
float x, y, w, h;
};

/**
* @brief A 2D vector made up of floats.
*/
struct ye_vec2f {
float x, y;
};

#endif // YE_TYPES_H
11 changes: 2 additions & 9 deletions engine/dist/include/yoyoengine/utils.h
Original file line number Diff line number Diff line change
@@ -1,5 1,5 @@
/*
This file is a part of yoyoengine. (https://github.com/yoyoengine/yoyoengine)
This file is a part of yoyoengine. (https://github.com/yoyoengine)
Copyright (C) 2023-2024 Ryan Zmuda
Licensed under the MIT license. See LICENSE file in the project root for details.
Expand Down Expand Up @@ -62,13 62,6 @@ YE_API void ye_auto_fit_bounds(struct ye_rectf* bounds_f, struct ye_rectf* obj_f
*/
YE_API SDL_Rect ye_get_real_texture_size_rect(SDL_Texture *pTexture);

/**
* @brief A rectangle made up of floats.
*/
struct ye_rectf {
float x, y, w, h;
};

/**
* @brief Convert a floating rectangle to an integer rectangle.
*
Expand All @@ -91,7 84,7 @@ YE_API struct ye_rectf ye_convert_rect_rectf(SDL_Rect rect);
enum ye_component_type {
YE_COMPONENT_TRANSFORM,
YE_COMPONENT_RENDERER,
YE_COMPONENT_PHYSICS,
YE_COMPONENT_RIGIDBODY,
YE_COMPONENT_COLLIDER,
YE_COMPONENT_LUA_SCRIPT,
YE_COMPONENT_AUDIOSOURCE,
Expand Down
10 changes: 8 additions & 2 deletions engine/dist/include/yoyoengine/yoyoengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 39,23 @@
#include "uthash/uthash.h"
#include "cache.h"
#include "ui.h"

// ecs //
#include "ecs/ecs.h"
#include "ecs/button.h"
#include "ecs/audiosource.h"
#include "ecs/camera.h"
#include "ecs/ecs.h"
#include "ecs/physics.h"
#include "ecs/renderer.h"
#include "ecs/transform.h"
#include "ecs/collider.h"
#include "ecs/tag.h"
#include "ecs/lua_script.h"

// physics //
#include "tar_physics/tar.h"
#include "tar_physics/solver.h"
#include "tar_physics/rigidbody.h"

#include "utils.h"
#include "timer.h"
#include "audio.h"
Expand Down
Loading

0 comments on commit 514951f

Please sign in to comment.