Jump to content
  • Advertisement
Sign in to follow this  
tmccolgan88

Problem displaying correct portion of a scene

This topic is 2038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a 2D, top down, twin stick shooter.  When the player moves I want the player position to change based on the background size, but to remain in the middle of the screen.  Right now when the player moves they move on the actual screen position and I cant get the background to display correctly. The Update() function updates the player and then passes the player position to the background class. Wondering if any one can help.

 

The player class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame1.sprites
{
    class PlayerSprite:BaseSprite
    {
        const int PLAYER_SPEED_X = 10;
        const int PLAYER_SPEED_Y = 10;
        const int SLIDE_RATE = 50; 
        const int MAIN_GUN_RATE  = 75;

        SpriteManager manager;

        int timeSinceLastSlide = 0;
        int timeSinceLastFire = 0;
        bool firing = false;

        float playerAngle;
        float turretAngle;
        float scale = .25f;

        int xFrameSize = 0;
        int yFrameSize = 0;

        int playerCurrentSpeedX = 0;
        int playerCurrentSpeedY = 0;


        Vector2 sticksLeft  = Vector2.Zero;
        Vector2 sticksRight = Vector2.Zero;
        

        Vector2 turretLocation = Vector2.Zero;

        point turretPos;
  
        Texture2D turretImage, bulletImage;

        List<BaseSprite> bullets = new List<BaseSprite>();

        public PlayerSprite(int xP, int yP, int xFS, int yFS, int sheetSizeX, int sheetSizeY, int animTime, Texture2D image, Texture2D turret, Texture2D bullet, SpriteManager manager)
            :base(xP, yP,xFS, yFS, sheetSizeX, sheetSizeY, animTime, 0, image)
        {
            this.turretImage = turret;

            turretPos.x = xP;
            turretPos.y = yP;

            this.xFrameSize = xFS;
            this.yFrameSize = yFS;

           

            this.bulletImage = bullet;
            this.manager = manager;
        }

        public void Update(GameTime gameTime, Rectangle backGroundSize)
        {
            sticksLeft = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.Circular).ThumbSticks.Left;
            sticksRight = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.Circular).ThumbSticks.Right;


            if ((sticksLeft.X) != 0)
            {
                playerCurrentSpeedX = (int)(PLAYER_SPEED_X * sticksLeft.X);
                playerAngle = (float)Math.Atan2(sticksLeft.X, sticksLeft.Y);
            }
            this.position.x += playerCurrentSpeedX;

            if ((sticksLeft.Y) != 0)
            {
                playerCurrentSpeedY = (int)(PLAYER_SPEED_Y * sticksLeft.Y);
                playerAngle = (float)Math.Atan2(sticksLeft.X, sticksLeft.Y);
            }
            this.position.y -= playerCurrentSpeedY;


            //the following code is used to make the player slide after releasing left stick
            if ((sticksLeft.Y) == 0 && (sticksLeft.X) == 0 && (playerCurrentSpeedX != 0 || playerCurrentSpeedY != 0))
            {
                timeSinceLastSlide += gameTime.ElapsedGameTime.Milliseconds;          
                if (timeSinceLastSlide >= SLIDE_RATE)
                {
                    timeSinceLastSlide = 0;
                    if (playerCurrentSpeedY > 0)
                    {
                        playerCurrentSpeedY--;
                    }
                    else if (playerCurrentSpeedY < 0)
                    {
                        playerCurrentSpeedY++;
                    }
                    if (playerCurrentSpeedX > 0)
                    {
                        playerCurrentSpeedX--;
                    }
                    else if (playerCurrentSpeedX < 0)
                    {
                        playerCurrentSpeedX++;
                    }
                }
            }
           
            
            if (this.position.x <= 0 + (frameSizes.x / 2))
            {
                //this.position.x = (frameSizes.x / 2);
            }
            if (this.position.y <= 0 + (frameSizes.y / 2))
            {
                //this.position.y = (frameSizes.y / 2);
            }
            //////////////////////////////////////////////////////
            if (this.position.x >= 1300 - frameSizes.x / 2)
            {
                //this.position.x =  1300 - frameSizes.x / 2;
            }
            if (this.position.y >= 700 - frameSizes.y / 2)
            {
                //this.position.y = 700 - frameSizes.y / 2;
            }

            System.Diagnostics.Debug.Print(position.x + " " + position.y);
            
            turretPos.x = this.position.x;
            turretPos.y = this.position.y;

            float bulletVelocityX = (sticksRight.X * 10);
            float bulletVelocityY = (sticksRight.Y * 10);

            if (Math.Abs(sticksRight.X) >= .5f || Math.Abs(sticksRight.Y) >= .5f)
            {
                firing = true;
                turretAngle = (float)Math.Atan2(sticksRight.X, sticksRight.Y);
            }

            timeSinceLastFire += gameTime.ElapsedGameTime.Milliseconds;          
            if (timeSinceLastFire >= MAIN_GUN_RATE && firing)
            {
                firing = false;
                timeSinceLastFire = 0;

                manager.addBullet(new BaseSprite(position.x - 25, position.y - 25, (int)bulletVelocityX, (int)bulletVelocityY, 20, 40, 0, 0, 0, (float)Math.Atan2(sticksRight.X,
                     sticksRight.Y), bulletImage));
            }

            foreach (BaseSprite s in bullets)
            {
                s.Update(gameTime);
            }

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch b)
        {


            b.Draw(image, new Vector2(position.x, position.y), new Rectangle(rowFrameIndex * sheetSize.x, colFrameIndex * sheetSize.y, frameSizes.x, frameSizes.y),
                Color.White, playerAngle, new Vector2(xFrameSize/2, yFrameSize/2), .25f, SpriteEffects.None, 0);

            

            b.Draw(turretImage, new Vector2(position.x - 25, position.y - 25), new Rectangle(1, 1, 100, 100),
               Color.White, turretAngle, new Vector2(25, 25), .25f, SpriteEffects.None, 0);
        }

        public bool isFiring()
        {
            firing = false;

            return firing; 
        }

        public Vector2 getPosition()
        {
            return new Vector2(this.position.x, this.position.y);
        }
    }
}
 

 

 

The background class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WindowsGame1.sprites
{
    class BackgroundSprite:BaseSprite
    {
        const int SCREEN_HEIGHT = 800;
        const int SCREEN_WIDTH  = 1400;

        const int BACKGROUNDHEIGHT = 1400;
        const int BACKGROUNDWIDTH = 2600;

        Vector2 pPosition = Vector2.Zero;

        Vector2 currentPosition = new Vector2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
        Vector2 backgroundorigin = new Vector2(BACKGROUNDWIDTH / 2, BACKGROUNDHEIGHT / 2);
        Texture2D backgroundImage = null;

        public BackgroundSprite(int xP, int yP, int xFS, int yFS, int sheetSizeX, int sheetSizeY, int animTime, Texture2D image)
            :base(xP, yP,xFS, yFS, sheetSizeX, sheetSizeY, animTime, 0, image)
        {
            backgroundImage = image;
        }

        public void Update(GameTime gameTime, Vector2 playerPosition)
        {
            pPosition = new Vector2(-playerPosition.X, -playerPosition.Y);
        }

        public override void Draw(SpriteBatch batch)
        {
            batch.Draw(backgroundImage, pPosition, null,
             Color.White, 0, backgroundorigin, 1, SpriteEffects.None, 0f);
        }
    }
}

 

 

Share this post


Link to post
Share on other sites
Advertisement

Hi tmccolgan88  smile.png

 

What you need is a "camera". And I put "camera" in quotation marks, because it is merely a way of thinking about the concept.

Whenever you are drawing the background, you are using the absolute coordinates, which makes them stay where they are, and your player move, as you point out.

What you are looking for is a way to "translate the coordinates of the world into the screen, based on the player's position".

This can be done by storing a Vector2 (yes - you may call it "_camera", to keep up the illusion that there *is* a real camera ;)), and updating the value of that vector based on the player's movements. Whenever you want to draw the background (world), you subtract the position of the camera from the position of the tiles/map-pieces/whatever, to make it the new origin for coordinates (it will be the top-left corner of the screen).

 

Does that make sense?  Otherwise Google "2D camera" or ask again smile.png

 

Kind regards - Jakob

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!