Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
vince authored and vince committed Jul 20, 2024
1 parent 887f193 commit 25e9b4f
Show file tree
Hide file tree
Showing 10 changed files with 472 additions and 0 deletions.
31 changes: 31 additions & 0 deletions raylib physics.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35013.160
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "raylib physics", "raylib physics\raylib physics.vcxproj", "{6F66A6A3-7E2D-4AB2-AA54-EAAA1AE26E1A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6F66A6A3-7E2D-4AB2-AA54-EAAA1AE26E1A}.Debug|x64.ActiveCfg = Debug|x64
{6F66A6A3-7E2D-4AB2-AA54-EAAA1AE26E1A}.Debug|x64.Build.0 = Debug|x64
{6F66A6A3-7E2D-4AB2-AA54-EAAA1AE26E1A}.Debug|x86.ActiveCfg = Debug|Win32
{6F66A6A3-7E2D-4AB2-AA54-EAAA1AE26E1A}.Debug|x86.Build.0 = Debug|Win32
{6F66A6A3-7E2D-4AB2-AA54-EAAA1AE26E1A}.Release|x64.ActiveCfg = Release|x64
{6F66A6A3-7E2D-4AB2-AA54-EAAA1AE26E1A}.Release|x64.Build.0 = Release|x64
{6F66A6A3-7E2D-4AB2-AA54-EAAA1AE26E1A}.Release|x86.ActiveCfg = Release|Win32
{6F66A6A3-7E2D-4AB2-AA54-EAAA1AE26E1A}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E6BFD028-9EE8-4594-813C-4E6A49FBA94A}
EndGlobalSection
EndGlobal
44 changes: 44 additions & 0 deletions raylib physics/BoxEntity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <list>
#include <iostream>

#include "BoxEntity.h"
#include "Conversions.h"
#include "EnvVariables.h"

void CreateBoxEntity(b2BodyType type, b2Vec2 position, b2Vec2 size, float rotation, float friction, Texture2D* texture, b2World* world, std::list<BoxEntity>* objectsList)
{
BoxEntity boxEntity = {};
boxEntity.size = size;
boxEntity.texture = texture;

b2BodyDef bodyDef;
bodyDef.type = type;
bodyDef.position.Set(position.x, position.y);
bodyDef.angle = rotation / 57.29578;

boxEntity.physicsBody = world->CreateBody(&bodyDef);

b2PolygonShape bodyShape;
bodyShape.SetAsBox(size.x / 2, size.y / 2);

b2FixtureDef bodyFixture;
bodyFixture.density = 1;
bodyFixture.friction = friction;
bodyFixture.shape = &bodyShape;

boxEntity.physicsBody->CreateFixture(&bodyFixture);

objectsList->push_back(boxEntity);
}

void DrawBoxEntity(BoxEntity* boxEntity)
{
b2Vec2 position = boxEntity->physicsBody->GetPosition();
float rotation = boxEntity->physicsBody->GetAngle() * 57.29578;

Rectangle source = { 0, 0, boxEntity->texture->width, boxEntity->texture->height };
Rectangle dest = { position.x, position.y, boxEntity->size.x, boxEntity->size.y };
Vector2 origin = { boxEntity->size.x / 2, boxEntity->size.y / 2 };

DrawTexturePro(*boxEntity->texture, source, dest, origin, rotation, WHITE);
}
12 changes: 12 additions & 0 deletions raylib physics/BoxEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "box2d/box2d.h"
#include "raylib.h"

struct BoxEntity
{
b2Vec2 size;
Texture2D* texture;
b2Body* physicsBody;
};

void CreateBoxEntity(b2BodyType type, b2Vec2 position, b2Vec2 size, float rotation, float friction, Texture2D* texture, b2World* world, std::list<BoxEntity>* objectsList);
void DrawBoxEntity(BoxEntity* boxEntity);
9 changes: 9 additions & 0 deletions raylib physics/Conversions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "raylib.h"
#include "box2d/box2d.h"
#include "EnvVariables.h"

b2Vec2 ConvertScreenToWorld(Vector2 input, Camera2D* camera)
{
b2Vec2 output = {(input.x - camera->offset.x + (camera->target.x * camera->zoom)) / camera->zoom, (input.y - camera->offset.y + (camera->target.y * camera->zoom)) / camera->zoom};
return output;
}
4 changes: 4 additions & 0 deletions raylib physics/Conversions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "raylib.h"
#include "box2d/box2d.h"

b2Vec2 ConvertScreenToWorld(Vector2 input, Camera2D* camera);
23 changes: 23 additions & 0 deletions raylib physics/EnvVariables.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "raylib.h"

#define SCREEN_WIDTH 1000
#define SCREEN_HEIGHT 800
#define TARGET_FPS 240.0f

#define PHYSICS_TIME_STEP 1.0f / TARGET_FPS
#define PHYSICS_VELOSITY_ITERATIONS 6
#define PHYSICS_POSITION_ITERATIONS 2

#define CAMERA_SPEED 10.0f
#define CAMERA_ZOOM_SPEED 10.0f

#define CREATE_NOTHING_MODE 0
#define CREATE_STATIC_OBJECT_MODE 1
#define CREATE_DYNAMIC_OBJECT_MODE 2

static float deltaTime;

static Camera2D camera;

static b2Vec2 createObjectCorner1;
static b2Vec2 createObjectCorner2;
154 changes: 154 additions & 0 deletions raylib physics/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include <iostream>
#include <list>
#include <string>

#include "raylib.h"
#include "box2d/box2d.h"
#include "BoxEntity.h"
#include "Conversions.h"

#include "EnvVariables.h"

int main()
{
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "physics test");
SetTargetFPS((int)TARGET_FPS);

camera.offset = { SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 };
camera.rotation = 0;
camera.zoom = 10;

Texture2D cubeTexture = LoadTexture("resources/textures/physics_cube.png");

b2World world = b2World(b2Vec2{ 0.0f, 10.0f });
std::list<BoxEntity> objects;

int createObjectMode = CREATE_NOTHING_MODE;

b2Vec2 createObjectCorner1;
b2Vec2 createObjectCorner2;

while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(BLACK);

deltaTime = GetFrameTime();

world.Step(PHYSICS_TIME_STEP, PHYSICS_VELOSITY_ITERATIONS, PHYSICS_POSITION_ITERATIONS);

//camera controls
if (IsKeyDown(KEY_RIGHT))
{
camera.target.x += CAMERA_SPEED * deltaTime;
}
if (IsKeyDown(KEY_LEFT))
{
camera.target.x -= CAMERA_SPEED * deltaTime;
}
if (IsKeyDown(KEY_DOWN))
{
camera.target.y += CAMERA_SPEED * deltaTime;
}
if (IsKeyDown(KEY_UP))
{
camera.target.y -= CAMERA_SPEED * deltaTime;
}
if (IsKeyDown(KEY_I))
{
camera.zoom += CAMERA_ZOOM_SPEED * deltaTime;
}
if (IsKeyDown(KEY_O))
{
camera.zoom -= CAMERA_ZOOM_SPEED * deltaTime;
}

//create objects controls
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && createObjectMode != CREATE_NOTHING_MODE)
{
createObjectCorner1 = ConvertScreenToWorld(GetMousePosition(), &camera);
}

if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && createObjectMode != CREATE_NOTHING_MODE)
{
createObjectCorner2 = ConvertScreenToWorld(GetMousePosition(), &camera);

b2Vec2 size = {};
b2Vec2 position = {};
b2BodyType type;

if (createObjectCorner2.x > createObjectCorner1.x)
{
size.Set(createObjectCorner2.x - createObjectCorner1.x, 0.0f);
position.Set(createObjectCorner1.x + size.x / 2, 0.0f);
}
else
{
size.Set(createObjectCorner1.x - createObjectCorner2.x, 0.0f);
position.Set(createObjectCorner2.x + size.x / 2, 0.0f);
}

if (createObjectCorner2.y > createObjectCorner1.y)
{
size.Set(size.x, createObjectCorner2.y - createObjectCorner1.y);
position.Set(position.x, createObjectCorner1.y + size.y / 2);
}
else
{
size.Set(size.x, createObjectCorner1.y - createObjectCorner2.y);
position.Set(position.x, createObjectCorner2.y + size.y / 2);
}

if (createObjectMode == CREATE_STATIC_OBJECT_MODE) type = b2_staticBody;
else type = b2_dynamicBody;

CreateBoxEntity(type, position, size, 0.0f, 0.3f, &cubeTexture, &world, &objects);
}

if (IsKeyPressed(KEY_U))
{
createObjectMode++;
if (createObjectMode > CREATE_DYNAMIC_OBJECT_MODE) createObjectMode = CREATE_NOTHING_MODE;
}


BeginMode2D(camera);

for (auto e : objects)
{
DrawBoxEntity(&e);
}

EndMode2D();

int fps = GetFPS();
std::string fpsText = "fps: ";
fpsText.append(std::to_string(fps));
DrawText(fpsText.c_str(), 0, 0, 20, LIGHTGRAY);
DrawText("current create mode: ", 0, SCREEN_HEIGHT - 20, 20, LIGHTGRAY);

std::string createObjectModeText;
Color createObjectModeTextColor;

if (createObjectMode == CREATE_NOTHING_MODE)
{
createObjectModeText = "DISABLED";
createObjectModeTextColor = RED;
}
if (createObjectMode == CREATE_STATIC_OBJECT_MODE)
{
createObjectModeText = "STATIC";
createObjectModeTextColor = BLUE;
}
if(createObjectMode == CREATE_DYNAMIC_OBJECT_MODE)
{
createObjectModeText = "DYNAMIC";
createObjectModeTextColor = GREEN;
}
DrawText(createObjectModeText.c_str(), 230, SCREEN_HEIGHT - 20, 20, createObjectModeTextColor);

EndDrawing();
}

return 0;
}
Loading

0 comments on commit 25e9b4f

Please sign in to comment.