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.


Topics - Driftmaniak

Pages: [1]
1
Graphics / Issue with rendering sprites
« on: October 13, 2023, 07:10:01 am »
Hi to all!)
I started learning c++ and to make it more interesting I make a small game pseudo3d racing.
And i have a strange problem, maybe with sprites or maybe with my c++ skills)
so, i have struct that contains information about traffic cars
struct trafficCar
{
    sf::Sprite sprite;
    int index;    
    float xOffset;
    float speed;
    float zPosition;
    float percent;
};
also i have struct that describes info about 1 road segment, here is part of code
struct segment
{
        //variables

    std::vector<billboardSprite> sideRoadSprites;

    std::vector<trafficCar*> segmentTrafficCars;


    void renderSprite()
    {          
        for (int i = 0; i < sideRoadSprites.size(); i++)
        {
            auto& roadSprite = sideRoadSprites[i];
            float spriteScale = point1.screenScale*1300.f;
            float spriteX = point1.screenPosition.x + (point1.screenScale * roadSprite.xOffset * roadWidth * wWidth /2.f);
            float spriteY = point1.screenPosition.y;

            const float destX = 215.f / 215.f * spriteScale;
            const float destY = 540.f / 540.f * spriteScale;
           
            roadSprite.sprite.setPosition(sf::Vector2f(spriteX, spriteY));
            roadSprite.sprite.setScale(destX, destY);
        }

        for (int i = 0; i < segmentTrafficCars.size(); i++)
        {
            auto& car = segmentTrafficCars[i];
            float spriteScale = interpolate(point1.screenScale,point2.screenScale,car->percent) *1300.f;
            float spriteX = interpolate(point1.screenPosition.x,point2.screenPosition.x,car->percent) + (spriteScale * car->xOffset * roadWidth * wWidth/2.f);
            float spriteY = interpolate(point1.screenPosition.y,point2.screenPosition.y,car->percent);

            const float destX = 80.f / 80.f * spriteScale;
            const float destY = 57.f / 57.f * spriteScale;
           
            car->sprite.setPosition(sf::Vector2f(spriteX, spriteY));
            car->sprite.setScale(destX, destY);
        }
    }
       
        //calculation functions
};

in segment i have vector array of pointers to trafficCar struct
and also i have vector array that contains all cars for track
here is how i create all this
void resetTrafficCars()
{
    std::random_device rd; // a seed source for the random number engine
    std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> distrib(0, 1);
    std::uniform_int_distribution<> distrib1(0, roadSegments.size());
   
    for(int i=0;i<100;i++)
    {
        trafficCar car;
        car.index = i;
        car.speed = maxSpeed/4 + distrib(gen) * maxSpeed/4.f;
        car.xOffset = 0;
        car.percent = 0;
        car.zPosition = distrib1(gen)*segmentLength;
        car.sprite.setTexture(testBotCarTexture);
        car.sprite.setTextureRect(sf::IntRect(sf::Vector2<int>(1383,894),sf::Vector2<int>(80,57)));
        car.sprite.setOrigin(80.f/2,57);
        auto& segment = roadSegments[(static_cast<int>(car.zPosition) / static_cast<int>(segmentLength)) %
        roadSegments.size()];
        segment.segmentTrafficCars.push_back(&car);
        allTrafficCars.push_back(car);        
    }
}
and here is function that update car state, in general it calculates car position and swap car between segment pointers array, i store them in segment to properly render later
void updateTrafficCars(float dt, segment* playerSegment,float playerW)
{
    for(int i=0;i<allTrafficCars.size();i++)
    {
        auto& car = allTrafficCars[i];
        auto& oldSegment = roadSegments[(static_cast<int>(car.zPosition) / static_cast<int>(segmentLength)) %
        roadSegments.size()];
        car.zPosition = increase(car.zPosition,dt * car.speed,trackLength);
        car.percent = percentRemaining(car.zPosition,segmentLength);
        auto& newSegment = roadSegments[(static_cast<int>(car.zPosition) / static_cast<int>(segmentLength)) %
        roadSegments.size()];

        if(oldSegment.index != newSegment.index)
        {
            int index = 0;
            for(int i=0;i<oldSegment.segmentTrafficCars.size();i++)
            {
                const auto& carPointer = oldSegment.segmentTrafficCars[i];
                if(carPointer->index == car.index)
                {
                    index = i;
                    break;
                }                
            }
            //switch car arrays
            oldSegment.segmentTrafficCars.erase(oldSegment.segmentTrafficCars.begin() + index);
            newSegment.segmentTrafficCars.push_back(&car);
        }
    }
}
and here is part of render code
for (auto& sideRoadSprite : roadSegments[currentIndex].sideRoadSprites)
                window.draw(sideRoadSprite.sprite);
           
            for (auto& trafficCar : roadSegments[currentIndex].segmentTrafficCars)
            {
                window.draw(trafficCar->sprite);
            }
so my problem is - a lot of pointers in segmentTrafficCars contains weird corrupted data, and it crush on trying to render it, even if i try to filter this pointers so it contain proper data, still i have black square instead of sprite,
i tested this sprite separatly and it loads and render's properly
also i tried for test create vector array for all sideRoadSprites and store pointers there, it also fails to render via pointer, help me pls with this)

Pages: [1]