From f5311cc940fd94aa70808f19a38581c80b764615 Mon Sep 17 00:00:00 2001 From: realtradam Date: Sun, 12 May 2024 15:35:37 -0400 Subject: working movement system --- src/main.c | 5 +++-- src/player.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++------------- src/render.c | 42 +++++++++++++++++++++++++++++++++++------- 3 files changed, 84 insertions(+), 22 deletions(-) diff --git a/src/main.c b/src/main.c index 3f5e94d..38f6576 100644 --- a/src/main.c +++ b/src/main.c @@ -116,7 +116,6 @@ int main(void) //----------------------------------------------------- //controller_read(&controllers); - movePlayers(); camera = lookThroughPlayer(camera, world.players[0]); // Update @@ -148,13 +147,14 @@ int main(void) //DrawCubeTextureRec(texture, (Rectangle){ 0, texture.height/2, texture.width/2, texture.height/2 }, // (Vector3){ 2.0f, 1.0f, 0.0f }, 2.0f, 2.0f, 2.0f, WHITE); + movePlayers(); updateController(); char text[50]; sprintf(text, "Joystick %d,%d", inputs_p1.stick_x, inputs_p1.stick_y); DrawText(text, 10, 30, 12, GREEN); + /* DrawText("Position", 150, 30, 12, GREEN); - sprintf(text, "x: %f", camera.position.x); DrawText(text, 150, 45, 12, GREEN); sprintf(text, "y: %f", camera.position.y); DrawText(text, 150, 60, 12, GREEN); @@ -167,6 +167,7 @@ int main(void) DrawText(text, 220, 60, 12, GREEN); sprintf(text, "%f", camera.target.z); DrawText(text, 220, 75, 12, GREEN); + */ DrawFPS(10, 10); EndDrawing(); diff --git a/src/player.c b/src/player.c index 64f12c9..3b30b05 100644 --- a/src/player.c +++ b/src/player.c @@ -1,6 +1,7 @@ #include "player.h" #include "world.h" #include +#include "raymath.h" void drawPlayer(Player *player) @@ -33,26 +34,58 @@ lookThroughPlayer(Camera camera, Player player) void movePlayers(void) { - float x = inputs_p1.stick_x / 126.0f; - float y = -inputs_p1.stick_y / 126.0f; + float xinput = inputs_p1.stick_x / 126.0f; + float yinput = -inputs_p1.stick_y / 126.0f; + + //float distance = sqrtf(xinput * xinput + yinput * yinput); + //if(distance == 0) + //{ + // distance = 1; + //} + + Vector2 direction = { + world.players[0].direction.x, + world.players[0].direction.y + }; + + char text[50]; + + sprintf(text, "x: %f", direction.x); + DrawText(text, 160, 45, 12, GREEN); + sprintf(text, "y: %f", direction.y); + DrawText(text, 160, 60, 12, GREEN); + sprintf(text, "z: %f", world.players[0].direction.z); + DrawText(text, 160, 75, 12, GREEN); + + direction = Vector2Rotate(direction, 0.01745329 * -xinput * 7); + + sprintf(text, "x: %f", direction.x); + DrawText(text, 160, 175, 12, GREEN); + sprintf(text, "y: %f", direction.y); + DrawText(text, 160, 190, 12, GREEN); + sprintf(text, "z: %f", world.players[0].direction.z); + DrawText(text, 160, 205, 12, GREEN); + + world.players[0].direction.z = yinput; + world.players[0].direction.x = direction.x; + world.players[0].direction.y = direction.y; //float x = world.players[0].direction.x; //float y = world.players[0].direction.z; - float distance = sqrtf(x * x + y * y); - if(distance != 0) - { - world.players[0].direction.x = x / distance; - world.players[0].direction.y = y / distance; - } + //if(distance != 0) + //{ + // world.players[0].direction.x = x / distance; + // world.players[0].direction.y = y / distance; + //} - world.players[1].direction.y = inputs_p2.stick_x / 126.0f; - world.players[1].direction.z = -inputs_p2.stick_y / 126.0f; + //world.players[1].direction.y = inputs_p2.stick_x / 126.0f; + //world.players[1].direction.z = -inputs_p2.stick_y / 126.0f; - world.players[0].position.x += world.players[0].direction.x * world.players[0].speed; - world.players[0].position.y += world.players[0].direction.y * world.players[0].speed; - world.players[0].position.z += world.players[0].direction.z * world.players[0].speed; + world.players[0].position.x += world.players[0].direction.x * 0.1f; //* world.players[0].speed; + world.players[0].position.y += world.players[0].direction.y * 0.1f; //* world.players[0].speed; + world.players[0].position.z += world.players[0].direction.z * 0.1f; //* world.players[0].speed; world.players[1].position.x += world.players[1].direction.x * world.players[1].speed; world.players[1].position.y += world.players[1].direction.y * world.players[1].speed; world.players[1].position.z += world.players[1].direction.z * world.players[1].speed; diff --git a/src/render.c b/src/render.c index d4734c6..2206f13 100644 --- a/src/render.c +++ b/src/render.c @@ -64,8 +64,10 @@ drawLine(Vector3 start, Vector3 end, float width, int up, Color color) void drawGrid(Vector3 position, int lines, int size, Color color) { - position.x -= lines * size / 2; - position.y -= lines * size / 2; + position.x += lines * size / 2; + position.y -= (lines * size / 2); + rlPushMatrix(); + //rlTranslatef(-(lines * size / 2), -(lines * size / 2), 0); for(int i = 0; i < lines + 1; ++i) { float mx = position.x; @@ -84,7 +86,7 @@ drawGrid(Vector3 position, int lines, int size, Color color) }, 4.0f, 2, - BLACK); + color); } for(int i = 0; i < lines + 1; ++i) { @@ -104,8 +106,9 @@ drawGrid(Vector3 position, int lines, int size, Color color) }, 4.0f, -1, - BLACK); + color); } + rlPopMatrix(); } void @@ -129,8 +132,8 @@ renderWorld(World* world, Camera camera) //drawGrid((Vector3){0}, 10, 1); for(int i = -3; i <= 3; ++i){ - DrawCube((Vector3){i,3,3}, 0.5f, 0.5f, 0.5f, YELLOW); - DrawCube((Vector3){i,4,3}, 0.5f, 0.5f, 0.5f, DARKPURPLE); + //DrawCube((Vector3){i,3,3}, 0.5f, 0.5f, 0.5f, YELLOW); + //DrawCube((Vector3){i,4,3}, 0.5f, 0.5f, 0.5f, DARKPURPLE); //DrawCube((Vector3){i,5,3}, 0.5f, 0.5f, 0.5f, YELLOW); //DrawCube((Vector3){i,3,4}, 0.5f, 0.5f, 0.5f, MAGENTA); //DrawCube((Vector3){i,4,4}, 0.5f, 0.5f, 0.5f, GREEN); @@ -140,7 +143,32 @@ renderWorld(World* world, Camera camera) } //drawLine((Vector3){0}, (Vector3){10,0,0}, 2.0f, 2, BLACK); - drawGrid((Vector3){0}, 10, 1, BLACK); + int space = 3; + + rlPushMatrix(); + rlTranslatef(0,0,-space); + drawGrid((Vector3){0}, 5, 1, BLACK); + rlPopMatrix(); + + rlPushMatrix(); + rlTranslatef(0,0,space); + rlRotatef(180,0,1,0); + drawGrid((Vector3){0}, 5, 1, GREEN); + rlPopMatrix(); + + rlPushMatrix(); + rlTranslatef(0,space,0); + rlRotatef(90,1,0,0); + drawGrid((Vector3){0}, 5, 1, YELLOW); + rlPopMatrix(); + + rlPushMatrix(); + rlRotatef(90,-1,0,0); + rlTranslatef(0,0,-space); + drawGrid((Vector3){0}, 5, 1, ORANGE); + rlPopMatrix(); + + //rlPopMatrix(); EndMode3D(); -- cgit v1.2.3