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

  • SEARCH
  • Home
  • 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 8842465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:57:55+00:00 2026-06-14T10:57:55+00:00

I am making a game and I am looking for some help with an

  • 0

I am making a game and I am looking for some help with an elementary camera-following algorithm.

  • Say we have a window which has the size of 320 by 480.
  • We also have
    a player with coordinates x,y.
  • The user can move the player with the
    arrow keys.
  • The camera is following the player coordinates. [The camera coordinates are where the screen is]. So camera.x=player.x and camera.y=player.y

Now the problem that I am getting is : when the player reaches the end of the room,
I can see the ‘outside’ of the room. So ideally the camera should stop moving when it is approaching the end of the room.

I am not sure how to tackle this problem efficiently and this does seem like a common feature in games. Any help would be great.

My code, it doesnt work.. really at all, the view goes wacky randomly*:

#bi_currentwindow.layout.x = -(top left x corner of window)
#bi_currentwindow.layout.y = -(top left y corner of window)
#bi_bg_size= total room (or world) size

            diff=(object.x-Window.size[0]/2)
            if diff<0:
                bi_currentwindow.layout.x = object.x+diff
            else:
                diff=(object.x+Window.size[0]/2)-bi_bg_size[0]
                if diff<0:
                    bi_currentwindow.layout.x = -object.x+Window.size[0]/2.0
            diff=(object.y-Window.size[1]/2)

            if diff<0:
                bi_currentwindow.layout.y = diff+Window.size[1]/2
            else:
                bi_bg_size=centralVarTransfer.bi_bg_size
                diff=bi_bg_size[1]-(object.y+Window.size[1]/2)

                if diff>0:
                    bi_currentwindow.layout.y = -object.y+Window.size[1]/2.0
  • 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-14T10:57:56+00:00Added an answer on June 14, 2026 at 10:57 am

    I prefer to make the player center screen and move the background, whenever I do a 2D game. This allows you to do: If my rooms side is > or < my screen size, then move my screen so its side is equal to my screen size.

    But if you have a camera and prefer to use that method:
    The math should be pretty easy.
    Something like:

    //assuming origin is the center of the object
    if ( camera.xPosition - (camera.width / 2) < room.x - (room.width / 2))
         camera.xPosition  = room.x - (room.width / 2) + (camera.width / 2)
    

    my code might be a bit off (and only for one side of the room), but conceptually you are checking if your camera position modified by an offset (based on camera width or height) is greater or less than the room position modified by an offset (based on room width or height).

    I find having 2 index cards and moving one around like the camera helps conceptualize the boundary conditions you are trying to code.

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

Sidebar

Related Questions

I'm making some game, and I have made it pretty much all, but not
Im looking for an algorithm to be used in a racing game Im making.
I am looking into making a text based game that I wrote in Haskell
Started making a game. Here's some of my code. package games.tribe.screens; import games.tribe.model.World; import
I'm making a game for the Android platform and I need to access some
I am making a game, and have a Game model and a User model.
I am making a game that has its own level editor. The editor simply
I am making a game engine using HTML5 canvas, and I have decided to
I'm looking for some advice on dynamic variables. Particularly if I'm making the best
I'm developing an Android game that has to download some assets to the SD

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.