Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9066853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:48:38+00:00 2026-06-16T16:48:38+00:00

Okay so I have two problems. First off I have made a square that

  • 0

Okay so I have two problems. First off I have made a square that is locked to a grid the same size as my tiles. This is what will be used for changing tiles. It works fine except for when I start scrolling. I know why it is. It’s because the mouse position is relative to the window, not the map. I was wondering if there was a way I could code the squares to follow my mouse even when I scroll.

Current code:

if (Event.type == sf::Event::MouseMoved)
        {

            rect.setFillColor(sf::Color(255, 0, 255));
            rect.setSize(sf::Vector2f(BLOCKSIZE, BLOCKSIZE));
            int x_offset = (Window.getView().getCenter().x - Window.getSize().x /2);
            int y_offset = (Window.getView().getCenter().y - Window.getSize().y /2);

            rect.setPosition(((sf::Mouse::getPosition(Window).x/32 *32) + (x_offset/32 *32)), ((sf::Mouse::getPosition(Window).y/32 * 32) + (y_offset/32 * 32)));
             std::cout << "Mouse position: x:" << ((sf::Mouse::getPosition(Window).x/32 *32) + (x_offset/32 *32)) << " y:" << ((sf::Mouse::getPosition(Window).y/32 * 32) + (y_offset/32 * 32)) << ")\n\n";
        }

Next problem is loading collision.

Code:

for(int i = 0; i < CollisionVector.size(); i++)
    {
        //Loop through the height of the MapVector
        for(int j = 0; j < CollisionVector[i].size(); j++)
        {
            sf::RectangleShape rect;
            //If the stored number is 1
            if(CollisionVector[i][j] == 1)
            {
                rect.setFillColor(sf::Color(255, 0, 255));
                rect.setSize(sf::Vector2f(BLOCKSIZE, BLOCKSIZE));
                //Set the position of the rectangle
                rect.setPosition(j * BLOCKSIZE, i * BLOCKSIZE);
            }
            //Draw the rectangle
            Window.draw(rect);
        }
    }

I get a blank screen if I add that in. I wanted the rectangles to be transparent but I changed it to pink just in case that was the problem (Which it wasn’t)

Screen scrolling code:

void Camera::Update(float x, float y)
{
    cameraX = x - (ScreenWidth / 2);
    cameraY = y - (ScreenHeight / 2);

    if (cameraX < 0)
        cameraX = 0.0;
    if (cameraY < 0)
        cameraY = 0.0;

    CameraPosition.reset(sf::FloatRect(cameraX, cameraY, ScreenWidth, ScreenHeight));
    CameraPosition.setViewport(sf::FloatRect(0,0,1,1));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:48:39+00:00Added an answer on June 16, 2026 at 4:48 pm

    For the first one: since you are scrolling by changing the sf::View, calculate the view offset and add it to your coordinates:

    [...]
    float x_offset = Window.getView().getCenter().x - Window.getSize().x * .5f;
    float y_offset = Window.getView().getCenter().y - Window.getSize().y * .5f;
    
    rect.setPosition(sf::Mouse::getPosition(Window).x/32 * 32 + x_offset, 
                sf::Mouse::getPosition(Window).y/32 * 32 + y_offset;
    

    For the second one… uhhh I’ve got nothing right now. I’ve checked SFML and a sf::RectangleShape is default-initialized to have a size of (0, 0) so that’s not the issue. Maybe the problem is in surrounding code?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two methods that do the same thing. The first one makes performance
Okay, I have two OperationContracts and MessageContracts, like this: [OperationContract] OperationResult OperationOnSingleItem(Input input) [OperationContract]
Okay, here is my problem: I have a form that requires to have two
Okay i have two models: posts and comments. as you can think comments has
okay i have been trying to understand this for hours i am learning VB
Okay I have a large CRUD app that uses tabs with Forms embedded in
I have a situation in which we have two production databases that synchronize with
This is in continuation of two ongoing problems I'm facing: Problems trying to attach
Okay so here's my problem. I have a two global NSString variables. globalVariable1 //stores
So I have this datagridview that is linked to a Binding source that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.