Raycaster 2D to 3D

For many days I've been trying to make a raycasting engine for Arduino in which 3 steps are essential -making a 3d environment (done👍) -making the player move (pending) -hand the player a assault rifle (pending) So,here I have problem with the movement of player which I have no I idea with ,I have tried dozens of ways to make the player move but it just glued to one place.If anyone can help then it will be just amazing.I have made this project on wokwi btw.If anyone knows about wokwi then you can also help me there :point_down: Raycast engine - Wokwi ESP32, STM32, Arduino Simulator. AND This is gonna run on a esp32 ,display the game in tft display and inputs by 5 push buttons ......Here's the code

#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>

#define TFT_CS     15
#define TFT_RST    -1
#define TFT_DC     33

#define BUTTON_UP    4
#define BUTTON_DOWN  36
#define BUTTON_LEFT  34
#define BUTTON_RIGHT 35

#define TFT_WIDTH   320
#define TFT_HEIGHT  240
#define MAP_WIDTH   15
#define MAP_HEIGHT  15

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

int worldMap[MAP_WIDTH][MAP_HEIGHT] = {
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
  {1,0,0,1,0,0,0,0,0,0,1,1,1,1,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};

double posX = 13.0, posY = 7.0, posZ = 0;
double dirX = -1.0, dirY = 0.0, planeX = 0.0, planeY = 0.66;
double speedVar = 0.50, rotSpeedVar = 0.1;

void setup() {
  tft.begin();
  tft.setRotation(1);
  Serial.begin(115200);

  pinMode(BUTTON_UP, INPUT_PULLUP);
  pinMode(BUTTON_DOWN, INPUT_PULLUP);
  pinMode(BUTTON_LEFT, INPUT_PULLUP);
  pinMode(BUTTON_RIGHT, INPUT_PULLUP);
}

void loop() {
  handleInput();
  movePlayer(1); 
  renderWalls();


}
// Button states
bool upPressed = false, downPressed = false, leftPressed = false, rightPressed = false;

void handleInput() {
  // Read button states
  upPressed = !digitalRead(BUTTON_UP);
  downPressed = !digitalRead(BUTTON_DOWN);
  leftPressed = !digitalRead(BUTTON_LEFT);
  rightPressed = !digitalRead(BUTTON_RIGHT);

  // Update player position based on button presses
  if (upPressed) {
    posY -= speedVar;
    dirY = cos(rotSpeedVar) * dirY - sin(rotSpeedVar) * dirX;
    dirX = sin(rotSpeedVar) * dirY + cos(rotSpeedVar) * dirX;
  }
  if (downPressed) {
    posY += speedVar;
    dirY = cos(-rotSpeedVar) * dirY - sin(-rotSpeedVar) * dirX;
    dirX = sin(-rotSpeedVar) * dirY + cos(-rotSpeedVar) * dirX;
  }
  if (leftPressed) {
    posX -= speedVar;
    dirX = cos(rotSpeedVar) * dirX - sin(rotSpeedVar) * dirY;
    dirY = sin(rotSpeedVar) * dirX + cos(rotSpeedVar) * dirY;
  }
  if (rightPressed) {
    posX += speedVar;
    dirX = cos(-rotSpeedVar) * dirX - sin(-rotSpeedVar) * dirY;
    dirY = sin(-rotSpeedVar) * dirX + cos(-rotSpeedVar) * dirY;
  }
}

  


   void movePlayer(double deltaTime) {
   //Ensure the player stays within the map boundaries
    posX = constrain(posX, 0.0, double(MAP_WIDTH - 1));
    posY = constrain(posY, 0.0, double(MAP_HEIGHT - 1));
    }

void renderWalls() {
  tft.fillScreen(ILI9341_BLACK);

  for (int x = 0; x < TFT_WIDTH; ++x) {
    double cameraX = 2 * x / double(TFT_WIDTH) - 1;
    double rayDirX = dirX + planeX * cameraX;
    double rayDirY = dirY + planeY * cameraX;

    int mapX = int(posX);
    int mapY = int(posY);

    double deltaDistX = abs(1 / rayDirX);
    double deltaDistY = abs(1 / rayDirY);

    int stepX, stepY;
    double sideDistX, sideDistY;

    if (rayDirX < 0) {
      stepX = -1;
      sideDistX = (posX - mapX) * deltaDistX;
    } else {
      stepX = 1;
      sideDistX = (mapX + 1.0 - posX) * deltaDistX;
    }

    if (rayDirY < 0) {
      stepY = -1;
      sideDistY = (posY - mapY) * deltaDistY;
    } else {
      stepY = 1;
      sideDistY = (mapY + 1.0 - posY) * deltaDistY;
    }

    bool hit = false;
    int side;
    while (!hit) {
      if (sideDistX < sideDistY) {
        sideDistX += deltaDistX;
        mapX += stepX;
        side = 0;
      } else {
        sideDistY += deltaDistY;
        mapY += stepY;
        side = 1;
      }
      if (worldMap[mapX][mapY] > 0) hit = true;
    }

    // Calculate perpendicular distance to wall
    double perpWallDist;
    if (side == 0) {
      perpWallDist = fabs((mapX - posX + (1 - stepX) / 2) / rayDirX);
    } else {
      perpWallDist = fabs((mapY - posY + (1 - stepY) / 2) / rayDirY);
    }

    // Calculate wall height
    int lineHeight = int(TFT_HEIGHT / perpWallDist);

    // Calculate draw start and draw end
    int drawStart = max(0, -lineHeight / 2 + TFT_HEIGHT / 2);
    int drawEnd = min(TFT_HEIGHT - 1, lineHeight / 2 + TFT_HEIGHT / 2);

    // Draw the wall slice
    int color = (worldMap[mapX][mapY] == 1) ? ILI9341_RED : ILI9341_WHITE;
    tft.drawFastVLine(x, drawStart, drawEnd - drawStart + 1, color);
  }
}

Welcome to the forum

Not the cause of your problem, but why are you wasting memory using int variables for your world map when the values are only 0 or 1 ?

Pack them into the bits of int variable instead

Can you help me with that but my main problem is movement of the player

Because i thought it would be easy

Whereabouts in the code do you draw the player on the screen ?

No the player is not drawn ,the sight of player is drawn

Now can you help me with the movement

I don't really understand what you are trying to do, but the player has an x and y coordinate and from what you say you are trying to draw the field of view of the player

If that is correct, then it should be easy to determine that a movement button has become pressed, adjust the player x or y coordinate and redraw the field of view

Where are you stuck ?

I dont know what code to write to make it move can you make a example code and I'll upgrade it

Sorry of code demand
But if you can provide then it will be awesome

You have this code in your sketch

    // Read button states
    upPressed = !digitalRead(BUTTON_UP);
    downPressed = !digitalRead(BUTTON_DOWN);
    leftPressed = !digitalRead(BUTTON_LEFT);
    rightPressed = !digitalRead(BUTTON_RIGHT);

    // Update player position based on button presses
    if (upPressed)
    {
        posY -= speedVar;
        dirY = cos(rotSpeedVar) * dirY - sin(rotSpeedVar) * dirX;
        dirX = sin(rotSpeedVar) * dirY + cos(rotSpeedVar) * dirX;
    }

So, pressing the up button changes some variables, including the posY variable. This is presumably the y position of something, possibly the player

You have a function named movePlayer() but all it does is to constrain the values of two variables

The code is quite complicated. Where did you get it from ?

I got it from chatgpt but i upgraded it a bit because that version doesn't complile
Feel free to customise the code

That says it all

What does your chatty friend have to say about your problem ?

I dont even like him ,it just gives trash

As you have found out

I am sorry to say that I have no idea what you expect the sketch to do, what should be displayed on the screen or what the effect of moving the played should be

Perhaps you could explain what the display should show, starting with where the player is positioned

The display should render the walls and if any button is pressed like for example
up then go forward and re-render the walls from that position

And i cant understand what do you mean by "effects of moving"??

If the display should render the walls, presumably from the perspective of the player, then why does the display in the Wokwi simulation change when the player is not moving ?

Presumably, when the player moves the view on the screen should change. That is the effect that I was referring to

It refreshes every moment because imagine if i add enemies then they can only move when a input is taken otherwise they will just freeze

Do you have any idea how can i implement moving . Feel free to edit the code