create rObj class

This commit is contained in:
2026-04-27 20:53:20 +10:00
parent f753ca1bca
commit 48fa24f3fc
7 changed files with 288 additions and 196 deletions

View File

@@ -6,11 +6,27 @@
#include "LinearMath/btDefaultMotionState.h"
#include "LinearMath/btVector3.h"
#include <btBulletDynamicsCommon.h>
#include <chrono>
#include <random>
#include <raylib.h>
#include <stdio.h>
#include <vector>
// todo: basic player movement script
// todo: untitled fighting game fr
// - load player model
// - code player movement left and right
int randomInteger(int lbound, int ubound) {
std::random_device rd;
std::mt19937_64 rng(rd());
std::uniform_real_distribution<> dist(lbound, ubound);
return dist(rng);
}
const std::vector<Color> colors = {MAROON, PINK, VIOLET,
DARKBLUE, DARKPURPLE, SKYBLUE};
btDefaultCollisionConfiguration *collision_configuration;
btCollisionDispatcher *dispatcher;
@@ -45,7 +61,7 @@ public:
model = LoadModelFromMesh(GenMeshCube(size.x, size.y, size.z));
} else if (shape == SPHERE) {
col_shape = new btSphereShape(btScalar(size.x));
model = LoadModelFromMesh(GenMeshSphere(size.x, 8, 16));
model = LoadModelFromMesh(GenMeshSphere(size.x, 16, 32));
}
collision_shapes.push_back(col_shape);
@@ -91,10 +107,58 @@ public:
DrawModelEx(model, pos, axis, angle, {1, 1, 1}, color);
}
void unload() { UnloadModel(model); }
};
enum object_types
{
BOX,
BALL
};
class rObj {
public:
Vector3 posXYZ;
Vector3 rotAxisXYZ;
Vector3 scaleXYZ;
float rotAngle;
Color objColor;
rObj(Vector3 position, Vector3 size, Vector3 scale, Vector3 rotAxis, float angle, Color color, object_types objType)
{
this->posXYZ = position;
this->rotAxisXYZ = rotAxis;
this->scaleXYZ = scale;
this->rotAngle = angle;
this->objColor = color;
switch(objType)
{
case BOX:
this->mesh = GenMeshCube(size.x,size.y,size.z);
this->model = LoadModelFromMesh(mesh);
break;
case BALL:
this->mesh = GenMeshSphere(size.x, 8,16);
this->model = LoadModelFromMesh(mesh);
break;
}
};
void render()
{
DrawModelEx(model, this->posXYZ, rotAxisXYZ, rotAngle, scaleXYZ, objColor);
}
private:
Mesh mesh;
Model model;
};
int main() {
InitWindow(800, 600, "raylib and bullet integration");
Camera3D cam = {0};
@@ -124,7 +188,7 @@ int main() {
DARKBLUE, DARKPURPLE, SKYBLUE};
physics_objects.push_back(
PhysObj({(float)GetRandomValue(-5, 5), (float)GetRandomValue(10, 15),
PhysObj({(float)randomInteger(-5, 5), (float)GetRandomValue(10, 15),
(float)GetRandomValue(-5, 5)},
{(float)GetRandomValue(-3, 3), (float)GetRandomValue(-3, 3),
(float)GetRandomValue(-3, 3)},
@@ -132,8 +196,11 @@ int main() {
colors[GetRandomValue(0, colors.size() - 1)]));
}
EnableCursor();
while (!WindowShouldClose()) {
UpdateCamera(&cam, CAMERA_ORBITAL);
UpdateCamera(&cam, CAMERA_PERSPECTIVE);
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
if (!collision.hit) {
@@ -142,8 +209,10 @@ int main() {
ray = GetScreenToWorldRay(GetMousePosition(), cam);
collision = GetRayCollisionBox(
ray, {{-8 / 2, -0.5 / 2, -8 / 2}, {8 / 2, 0.5 / 2, 8 / 2}});
physics_objects.push_back(PhysObj({collision.point}, {0, 0, 0},
{1, 1, 1}, CUBE, DYNAMIC, 1, PINK));
physics_objects.push_back(PhysObj(
{collision.point.x, collision.point.y + 0.5f, collision.point.z},
{0, 0, 0}, {1, 1, 1}, CUBE, DYNAMIC, 1,
colors[randomInteger(0, 5)]));
} else
collision.hit = false;
}
@@ -151,10 +220,13 @@ int main() {
dynamics_world->stepSimulation(1.0 / float(60), 10);
if (IsKeyPressed(KEY_SPACE)) {
physics_objects.push_back(
PhysObj({0, 10, 0}, {0, 0, 0}, {1, 1, 1}, SPHERE, DYNAMIC, 1, PINK));
physics_objects.push_back(PhysObj({0, 10, 0}, {0, 0, 0}, {0.5, 0.5, 0.5},
SPHERE, DYNAMIC, 1, colors[randomInteger(0, 5)]));
}
// regular game logic and stuff
BeginDrawing();
ClearBackground(BLACK);
BeginMode3D(cam);
@@ -164,6 +236,7 @@ int main() {
DrawGrid(10, 1.0);
EndMode3D();
DrawText("Left Click on the platform to create a cube", 16, 64, 20, GREEN);
DrawText("Press Space to create a sphere", 16, 86, 20, GREEN);
DrawFPS(16, 16);
EndDrawing();
}