Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - R4nd0mP3rSon

Pages: [1]
1
General / Raycast failed
« on: April 12, 2024, 02:36:41 pm »
How do I fixed this https://pastebin.com/8qTAdy95 the code worked fine if I  put      float shape_height = round(1000.f* projection  /rad);        but when I put           float shape_height = round(1000.f* projection  /(rad * dist));    like from this video   it worked  for horizontal  but  not for vertical for some reason




basically the height is a it too tall

2
General / Re: Ray go through wall
« on: April 10, 2024, 03:52:15 pm »
nvm I fixed it the triangle fan fov[1] was wrong

3
General / Ray go through wall
« on: April 07, 2024, 11:50:31 am »
How do I make the ray not go through the wall.
  https://pastebin.com/0DK8MMSm

4
   But how do I make this better so that the ray doesn't go through the block like this

https://pastebin.com/0DK8MMSm

###It fixed now

5
Yeah kinda

6
General / Re: Raycasting Ending point
« on: April 06, 2024, 01:45:37 pm »
it fixed I just need to replaced all the b with a

7
General / Raycasting Ending point
« on: April 06, 2024, 10:16:07 am »
How do I correctly get the ending point of the ray and not get this? because it keep working then stop working



#include "SFML/Graphics.hpp"
#include "iostream"
#include "math.h"


constexpr float PI = 3.14159265358979323846264338327952f;


int main() {
    float gridF = 64.f;
    sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML Application");
    window.setFramerateLimit(60);
    sf::RectangleShape shape(sf::Vector2f(10.f, 10.f));
    shape.setPosition(300.f, 300.f);
    shape.setFillColor(sf::Color::Cyan);
    sf::RectangleShape line(sf::Vector2f (70.f,3.f));
    line.setFillColor(sf::Color::Cyan);
    float timer;
    float timerMax;
    float px, py, pdx, pdy;
    float pa = 0;
    sf::CircleShape c;
    c.setRadius(3.f);
    c.setPointCount(100.f);
    float yx;
    float xy;

    sf::Text text;
    sf::Font font;
    int map[][8] = {
            {1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 0, 0, 0, 1, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 1, 1, 0, 0, 1},
            {1, 0, 1, 1, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 1, 0, 0, 0, 1},
            {1, 1, 1, 1, 1, 1, 1, 1}
    };




    int mapX = 8;
    int mapY = 8;



    px = 300.f;
    py = 300.f;

    timerMax = 1.5f;
    timer = timerMax;

    pdx = cos(pa) * 5;
    pdy = sin(pa) * 5;

    sf::Clock clock;
    sf::VertexArray cast(sf::LineStrip,2.f);




    sf::RectangleShape tile[mapY][mapX];
    for (int y = 0; y < mapY; y++) {
        for (int x = 0; x < mapX; x++) {
            if (map[y][x] == 1) {
                tile[y][x].setSize(sf::Vector2f(gridF, gridF));
                tile[y][x].setFillColor(sf::Color(55, 55, 55, 255));
                tile[y][x].setOutlineThickness(1.f);
                tile[y][x].setOutlineColor(sf::Color::White);
                tile[y][x].setPosition(x * gridF , y * gridF);
            } else {
                tile[y][x].setSize(sf::Vector2f(gridF, gridF));
                tile[y][x].setFillColor(sf::Color::Transparent);
                tile[y][x].setOutlineThickness(1.f);
                tile[y][x].setOutlineColor(sf::Color::White);
                tile[y][x].setPosition(x * gridF , y * gridF );
            }

        }
    }
    float a;
    float b;
    pa = PI * 2;
    while (window.isOpen()) {


        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }


        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)&& timer >= timerMax ) {
            pa -= 0.1f;
            pdx = cos(pa) * 5;
            pdy = sin(pa) * 5;


        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)&& timer >= timerMax ) {
            pa += 0.1f;
            pdx = cos(pa) * 5;
            pdy = sin(pa) * 5;


        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
          px += pdx;
          py += pdy;

        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {

            px -= pdx;
            py -= pdy;
        }
        if (timer >= timerMax) {
            timer = 0;
        }
        else {
            timer += 1.f;
        }
        shape.setPosition(px,py);
        xy = px + (shape.getSize().x/2);
        yx = py + (shape.getSize().y/2);
        cast[0].position = sf::Vector2f(xy,yx);
        cast[1].position = sf::Vector2f(xy + (pdx * 180/PI),yx + (pdy * 180/PI));
        float dir_y = sin(pa) ;
        float dir_x = cos(pa) ;

        int mapx = floor(px/64);
        int mapy = floor(py/64);


        float sideDistX;
        float sideDistY;

        int step_x;
        int step_y;
        int hit = 0;

        float delta_x = sqrt(1.f + (pow(dir_y,2)/pow(dir_x,2))) * gridF;
        float delta_y = sqrt(1.f + (pow(dir_x,2)/pow(dir_y,2))) * gridF;

        if (dir_x < 0)
        {

            step_x = -1;
            sideDistX = ((px/64) - float(mapx)) * delta_x;

        }
        else
        {
            step_x = 1;
            sideDistX = ((float(mapx) + 1.f) - (px/64)) * delta_x;

        }

        if (dir_y < 0)
        {
            step_y = -1;
            sideDistY = ( (py/64) - float(mapy)) * delta_y;

        }
        else {
            step_y = 1;
            sideDistY = ((float(mapy) + 1.f) - (py/64)) * delta_y;

        }

        while (hit == 0)
        {

            if (sideDistX < sideDistY)
            {

                a = sideDistX;
                mapx += step_x;
                sideDistX += delta_x;

            }
            else
            {

                b = sideDistY;
                mapy += step_y;
                sideDistY += delta_y;

            }
            //Check if ray has hit a wall
            if ( mapy < 8 && mapx < 8 && map[mapy][mapx] > 0) {

                hit = 1;}
        }
        std::cout << mapy << "   " << mapx << "\n";
        c.setPosition(   px + dir_x * a , py + dir_y * b) ;

        window.clear();

        for (int y = 0; y < mapY; y++) {
            for (int x = 0; x < mapX; x++) {
                window.draw(tile[y][x]);
            }
        }
        window.draw(c);
        window.draw(shape);
        window.draw(cast);


        window.display();
    }

 

8
I tried doing  the same calculation  as this tutorial https://lodev.org/cgtutor/raycasting.html
but it didn't work and now it like this ,Does anyone know what wrong with it?

#include "SFML/Graphics.hpp"
#include "sstream"
#include "math.h"


constexpr float PI = 3.14159265358979323846264338327952f;

#define SCREEN_WIDTH 1000
#define SCREEN_HEIGHT 1000
void draw3DRays(float pa,sf::RenderTarget *target,float px,float py) {
    int maper[][8] = {
            {1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 0, 0, 0, 1, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 1, 1, 0, 0, 1},
            {1, 0, 1, 1, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 1, 0, 0, 0, 1},
            {1, 1, 1, 1, 1, 1, 1, 1}
    };
    std::stringstream ss;
    sf::Text text;
    sf::CircleShape c;
    c.setRadius(10.f);
    c.setPointCount(100.f);

    float dir_y = sin(pa) * 5.f;
    float dir_x = cos(pa) * 5.f;
    int mapX = floor(px/64);
    int mapY = floor(py/64);
    float sideDistX;
    float sideDistY;
    int step_x;
    int step_y;
    int hit = 0;

    float delta_x = sqrt(1 + pow(dir_y,2)/pow(dir_x,2));
    float delta_y = sqrt(1 + pow(dir_x,2)/pow(dir_y,2));


    if (dir_x < 0)
    {
        step_x = -1;
        sideDistX = (px/64 - mapX) * delta_x;
    }
    else if (dir_x > 0)
    {
        step_x = 1;
        sideDistX = (mapX + 1 - px/64) * delta_x;
    }
    else{
        sideDistX = 0;
    }
    if (dir_y < 0)
    {
        step_y = -1;
        sideDistY = (py/64 - mapY) * delta_y;
    }
    else if (dir_y > 0){
        step_y = 1;
        sideDistY = (mapY + 1 - py/64) * delta_y;
    }
    else
    {
      sideDistY = 0;
    }

    while (hit == 0)
    {
     
        if (sideDistX < sideDistY)
        {
            sideDistX += delta_x;
            mapX += step_x;

        }
        else
        {
            sideDistY += delta_y;
            mapY += step_y;

        }
        //Check if ray has hit a wall
        if ( mapY < 8 && mapX < 8 && maper[mapY][mapX] > 0) hit = 1;
    }








    sf::VertexArray line(sf::Lines,2);
    line[0] = sf::Vector2f(px, py);
    line[1]= sf::Vector2f(sideDistX,sideDistY);

    target->draw(line);
}
int main() {
    float gridF = 64.f;
    sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML Application");
    window.setFramerateLimit(60);
    sf::RectangleShape shape(sf::Vector2f(50.f, 50.f));
    shape.setPosition(144.f, 144.f);
    shape.setFillColor(sf::Color::Cyan);
    sf::RectangleShape line(sf::Vector2f (70.f,3.f));
    line.setFillColor(sf::Color::Cyan);
    float timer;
    float timerMax;
    float px, py, pdx, pdy;
    float pa;
    sf::CircleShape c;
    c.setRadius(10.f);
    c.setPointCount(100.f);
    float yx;
    float xy;
   
    sf::Text text;
    sf::Font font;
    int map[][8] = {
            {1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 0, 0, 0, 1, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 1, 1, 0, 0, 1},
            {1, 0, 1, 1, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 1, 0, 0, 0, 1},
            {1, 1, 1, 1, 1, 1, 1, 1}
    };


   

    int mapX = 8;
    int mapY = 8;
 


    px = 300.f;
    py = 300.f;

    timerMax = 1.5f;
    timer = timerMax;

    pdx = cos(pa) * 5;
    pdy = sin(pa) * 5;


   
    sf::RectangleShape ray(sf::Vector2f(100.f,2.f));
    ray.setFillColor(sf::Color::Green);

    sf::Clock clock;
    sf::VertexArray cast(sf::LineStrip,2.f);


    font.loadFromFile("MadimiOne-Regular.ttf");


    sf::RectangleShape tile[mapY][mapX];
    for (int y = 0; y < mapY; y++) {
        for (int x = 0; x < mapX; x++) {
            if (map[y][x] == 1) {
                tile[y][x].setSize(sf::Vector2f(gridF, gridF));
                tile[y][x].setFillColor(sf::Color(55, 55, 55, 255));
                tile[y][x].setOutlineThickness(1.f);
                tile[y][x].setOutlineColor(sf::Color::White);
                tile[y][x].setPosition(x * gridF , y * gridF);
            } else {
                tile[y][x].setSize(sf::Vector2f(gridF, gridF));
                tile[y][x].setFillColor(sf::Color::Transparent);
                tile[y][x].setOutlineThickness(1.f);
                tile[y][x].setOutlineColor(sf::Color::White);
                tile[y][x].setPosition(x * gridF , y * gridF );
            }

        }
    }
    pa = PI * 2;
    while (window.isOpen()) {

       
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }


        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)&& timer >= timerMax ) {
            pa -= 0.1f;
            pdx = cos(pa) * 5;
            pdy = sin(pa) * 5;


        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)&& timer >= timerMax ) {
            pa += 0.1f;
            pdx = cos(pa) * 5;
            pdy = sin(pa) * 5;


        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
          px += pdx;
          py += pdy;

        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
            float radians = pa * PI/180.f;
            px -= pdx;
            py -= pdy;
        }
        if (timer >= timerMax) {
            timer = 0;
        }
        else {
            timer += 1.f;
        }
        shape.setPosition(px,py);
        xy = px + (shape.getSize().x/2);
        yx = py + (shape.getSize().y/2);
        cast[0].position = sf::Vector2f(xy,yx);
        cast[0].position = sf::Vector2f(xy + (pdx * 180/PI),yx + (pdy * 180/PI));









        window.clear();

        for (int y = 0; y < mapY; y++) {
            for (int x = 0; x < mapX; x++) {
                window.draw(tile[y][x]);
            }
        }

        window.draw(shape);
        window.draw(cast);
        draw3DRays(pa,&window,px,py);
        window.display();
    }

 

9
is there a better way to do the line rotation and player directions because it doesn't seem to be able to point to all directions


#include "SFML/Graphics.hpp"
#include "iostream"
#include "math.h"


#define PI 3.14159265358979323846264338327952

int main()
{
    sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML Application");
    window.setFramerateLimit(60);
    sf::RectangleShape shape(sf::Vector2f(50.f,50.f));
    shape.setPosition(100.f, 100.f);
    shape.setFillColor(sf::Color::Cyan);
    sf::RectangleShape line(sf::Vector2f (70.f,3.f));
    line.setFillColor(sf::Color::Cyan);
    //float stepx,step_y;
    float px,py,pdx,pdy;
    float pa;
    int map[][8] ={
            {1,1,1,1,1,1,1,1},
            {1,0,0,0,0,1,0,1},
            {1,0,0,0,0,0,0,1},
            {1,0,0,1,1,0,0,1},
            {1,0,1,1,0,0,0,1},
            {1,0,0,0,0,0,0,1},
            {1,0,0,1,0,0,0,1},
            {1,1,1,1,1,1,1,1}
    };

    sf::View view;
    //float viewSpeed= 200.f;
    float dtMultiplier = 60.f;
    float dt;
    float gridF= 100.f;
    int mapX = 8;
    int mapY = 8;
    px = 300.f;
    py = 300.f;


    pdx =cos(pa) * 5;
    pdy = sin(pa) * 5;

    int mapS = 16;
    sf::Clock clock;
    sf::RectangleShape tile[mapY][mapX];
    for(int y =0;y < mapY;y++){
        for(int x =0;x < mapX;x++){
            if(map[y][x] == 1){
                tile[y][x].setSize(sf::Vector2f(gridF,gridF));
                tile[y][x].setFillColor(sf::Color(55,55,55,255));
                tile[y][x].setOutlineThickness(1.f);
                tile[y][x].setOutlineColor(sf::Color::White);
                tile[y][x].setPosition(x * gridF + 50.f,y* gridF + 50.f);
            }
            else{
                tile[y][x].setSize(sf::Vector2f(gridF,gridF));
                tile[y][x].setFillColor(sf::Color::Transparent);
                tile[y][x].setOutlineThickness(1.f);
                tile[y][x].setOutlineColor(sf::Color::White);
                tile[y][x].setPosition(x * gridF + 50.f,y* gridF + 50.f);
            }

        }
    }

    while (window.isOpen())
    {

        dt = clock.restart().asSeconds();
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){

            pa -= 0.1f ;
            if(pa < 0){
                pa += 2 * PI;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;

            std::cout << 0.1 * 360 /6.28 << "\n";
            line.setRotation(pa * 360/(PI *2));


        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
            pa += 0.1f ;
            if(pa > PI * 2){
                pa -= 2 * PI ;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;
            std::cout << (pa* 180/PI) + 180 << "\n";
            line.setRotation(pa * 360/(PI*2));

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
           px += pdx ;
           py += pdy ;

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
            px -= pdx ;
            py -= pdy ;
        }

        shape.setPosition(px,py);
        line.setPosition(shape.getPosition().x + (shape.getSize().x/2),shape.getPosition().y+ shape.getSize().y /2 );


        window.clear();
        window.draw(line);
        window.draw(shape);
        for(int y =0;y < mapY;y++) {
            for (int x = 0; x < mapX; x++) {
                window.draw(tile[y][x]);
            }
        }

        window.display();
    }
}

10
General / Re: How do I make a rectangle move at the direction of a line
« on: January 23, 2024, 08:02:03 am »
How do I rotate the line the direction is decided like this


float px =300.f;
float py = 300.f;
float pa,pdx,pdy;

    while (window.isOpen())
    {


        dt = clock.restart().asSeconds();
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){

            pa -= 0.1 * dt * dtMultiplier;
            if(pa < 0){
                pa += 2* PI;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
            pa += 0.1 * dt * dtMultiplier;
            if(pa >PI * 2){
                pa -= 2 * PI;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;




        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
           px -= pdx * dt * dtMultiplier;
           py -= pdy * dt * dtMultiplier;

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
            px += pdx * dt * dtMultiplier;
            py += pdy * dt * dtMultiplier;
        }

        shape.setPosition(px,py);
        line.setPosition(shape.getPosition().x - (shape.getSize().x),shape.getPosition().y + (shape.getSize().y/2));
       

        window.clear();
        window.draw(line);
        window.draw(shape);
        for(int y =0;y < mapY;y++) {
            for (int x = 0; x < mapX; x++) {
                window.draw(tile[y][x]);
            }
        }

        window.display();
    }
}

11
General / Re: How do I make a rectangle move at the direction of a line
« on: January 22, 2024, 01:12:22 pm »
i dont know if im dumb or something but I still cant figure it out here my code I want make the line rotate 360 degree around the player but when I do it not very accurate



[8] ={
            {1,1,1,1,1,1,1,1},
            {1,0,0,0,0,1,0,1},
            {1,0,0,0,0,0,0,1},
            {1,0,0,1,1,0,0,1},
            {1,0,1,1,0,0,0,1},
            {1,0,0,0,0,0,0,1},
            {1,0,0,1,0,0,0,1},
            {1,1,1,1,1,1,1,1}
    };

    sf::View view;
    //float viewSpeed= 200.f;
    float dtMultiplier = 60.f;
    float dt;
    float gridF= 100.f;
    int mapX = 8;
    int mapY = 8;
    px = 300.f;
    py = 300.f;
 
    pdx =cos(pa) * 5;
    pdy = sin(pa) * 5;

    int mapS = 16;
    sf::Clock clock;
    sf::RectangleShape tile[mapY][mapX];
    for(int y =0;y < mapY;y++){
        for(int x =0;x < mapX;x++){
            if(map[y][x] == 1){
                tile[y][x].setSize(sf::Vector2f(gridF,gridF));
                tile[y][x].setFillColor(sf::Color(55,55,55,255));
                tile[y][x].setOutlineThickness(1.f);
                tile[y][x].setOutlineColor(sf::Color::White);
                tile[y][x].setPosition(x * gridF + 50.f,y* gridF + 50.f);
            }
            else{
                tile[y][x].setSize(sf::Vector2f(gridF,gridF));
                tile[y][x].setFillColor(sf::Color::Transparent);
                tile[y][x].setOutlineThickness(1.f);
                tile[y][x].setOutlineColor(sf::Color::White);
                tile[y][x].setPosition(x * gridF + 50.f,y* gridF + 50.f);
            }

        }
    }

    while (window.isOpen())
    {


        dt = clock.restart().asSeconds();
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){

            pa -= 0.1 * dt * dtMultiplier;
            if(pa < 0){
                pa += 2* PI;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
            pa += 0.1 * dt * dtMultiplier;
            if(pa >PI * 2){
                pa -= 2 * PI;
            }
            pdx =cos(pa) * 5;
            pdy = sin(pa) * 5;




        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
           px -= pdx * dt * dtMultiplier;
           py -= pdy * dt * dtMultiplier;

        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
            px += pdx * dt * dtMultiplier;
            py += pdy * dt * dtMultiplier;
        }

        shape.setPosition(px,py);
        line.setPosition(shape.getPosition().x - (shape.getSize().x),shape.getPosition().y + (shape.getSize().y/2));
       

        window.clear();
        window.draw(line);
        window.draw(shape);
        for(int y =0;y < mapY;y++) {
            for (int x = 0; x < mapX; x++) {
                window.draw(tile[y][x]);
            }
        }

        window.display();
    }
}

12
General / Re: How do I make a rectangle move at the direction of a line
« on: January 22, 2024, 06:42:31 am »
Basically I want to have an arrow which can turn 360 degree around the player and it is  turn by pressing  "a" or "d"  then it would point to a direction on the map where the  player can move by pressing "w" to the direction where the arrow is pointing at but I don't how to rotate it

13
General / How do I make a rectangle move at the direction of a line
« on: January 21, 2024, 05:16:18 pm »
How do I make the line change it direction around the player when  I pressed "A" or "D" and when I pressed w it wile move here the line is pointing at

14
Thanks it worked now. but do you know if there's
a way to detect the collision only once ?

15
void ball::update( sf::RenderTarget *target,ball *Ball,int i,int k)
{

    this->updateAccel();

    this->dx = (this->balls.getRadius() + this->balls.getPosition().x) -
         (Ball->getRadius() + Ball->getBounds().x);
    this->dy = (this->balls.getRadius() + this->balls.getPosition().y) -
         (Ball->getRadius() + Ball->getBounds().y);
    this->distance = sqrt((this->dx * this->dx) + (this->dy * this->dy));

    if(i != k) {
        if (this->distance <= this->balls.getRadius() + Ball->getRadius()) {
            this->velocity *= -1.f;

            std::cout << "Collision!" << "\n";
            return;
        }
        return;
    }


    this->updateCollision(target);


}


for(int i = 0; i < this->balls.size();i++) {
        for (int k = 0; k < this->balls.size(); k++) {
                this->balls[i].update(this->window, &this->balls[k],i,k);
        }
    }

Pages: [1]
anything