Skip to main content
Notice removed Draw attention by CommunityBot
Bounty Ended with Blau's answer chosen by CommunityBot
Added new code and additional information.
Source Link
Homer_Simpson
  • 615
  • 1
  • 7
  • 19
Vector2 vp, gameWorldSize = new Vector2(800, 480);
Matrix Camera;
float ScaleX, ScaleY, Scale;

vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
ScaleX = vp.X / gameWorldSize.X;
ScaleY = vp.Y / gameWorldSize.Y;
Scale = Math.Min(ScaleX, ScaleY);
Vector2 PlayerPosition;
CameraVector2 =Viewport;
float Scale;

public Matrix GetMatrix(float scrollingspeed)
{
  return Matrix.CreateTranslation(new Vector3(-Player.PlayerpositionPlayerPosition.X, -Player.PlayerpositionPlayerPosition.Y, 0) * scrollingspeed)
   * Matrix.CreateScale(Scale, Scale, 1)
   * Matrix.CreateTranslation(vpViewport.X / 2, vpViewport.Y / 2, 0);
}
//Drawing

I draw the sprites like this:

spriteBatch.Begin(SpriteSortMode.BackToFront, theBlendState.AlphaBlend, spritesnull, null, null, null, camera.GetMatrix(0.8f));  
  BackgroundLayer1.Render(spriteBatch);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Cameracamera.GetMatrix(1.0f));
  Player.Render(spriteBatch);
spriteBatch.End();

//in the Player class:
batch.Draw(Playertex, new Rectangle(400, 240, Playertex.Width, Playertex.Height), null, Color.White, 0, new Vector2(Playertex.Width / 2, Playertex.Height / 2), SpriteEffects.None, 0f);
//in the BackgroundLayer1 class:
batch.Draw(Background1tex, new Rectangle(400, 240, Background1tex.Width, Background1tex.Height), null, Color.White, 0, new Vector2(Background1tex.Width / 2, Background1tex.Height / 2), SpriteEffects.None, 1.0f);

UPDATEUPDATE1: In addition, the background sprites shouldn't repeat in a loop, because I have ten 800x480 pixel sprites that are drawn one after another. For example, the first sprite is drawn at (0,0), the second at (800,0), the third at (1600,0), … The last sprite(number 10) is the end of the level. The player can move back but not further. I will create more than one layer of sprites, but every layer with a different speed factor to create the Parallax Scrolling.

How can I add the speed factor so that the background sprites move slower than the player sprite?

UPDATE2: If I draw "BackgroundLayer1" with a scrolling speed of 1.0f, the sprite is drawn on the correct position, but if I change the scrolling speed to another value(for example 0.8f), then the sprites of "BackgroundLayer1" aren't drawn on the correct position.

Why are the sprites of "BackgroundLayer1" not drawn on the same position if I change the scrolling speed? What is wrong? The sprites should be drawn on the same positions, even if I change the scrolling speed.

In these two pictures, you see the differences if I change the scrolling speed of BackgroundLayer1:

scrollingspeed = 1.0f

http://s1.directupload.net/images/140505/qdi6cqwz.jpg

scrollingspeed = 0.8f

http://s1.directupload.net/images/140505/56ferzuv.jpg

Vector2 vp, gameWorldSize = new Vector2(800, 480);
Matrix Camera;
float ScaleX, ScaleY, Scale;

vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
ScaleX = vp.X / gameWorldSize.X;
ScaleY = vp.Y / gameWorldSize.Y;
Scale = Math.Min(ScaleX, ScaleY);

Camera = Matrix.CreateTranslation(-Player.Playerposition.X, -Player.Playerposition.Y,0)
   * Matrix.CreateScale(Scale, Scale, 1)
   * Matrix.CreateTranslation(vp.X / 2, vp.Y / 2, 0);

//Drawing the sprites
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Camera);
 ...

UPDATE: In addition, the background sprites shouldn't repeat in a loop, because I have ten 800x480 pixel sprites that are drawn one after another. For example, the first sprite is drawn at (0,0), the second at (800,0), the third at (1600,0), … The last sprite(number 10) is the end of the level. The player can move back but not further. I will create more than one layer of sprites, but every layer with a different speed factor to create the Parallax Scrolling.

How can I add the speed factor so that the background sprites move slower than the player sprite?

Vector2 vp, gameWorldSize = new Vector2(800, 480);
vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
ScaleX = vp.X / gameWorldSize.X;
ScaleY = vp.Y / gameWorldSize.Y;
Scale = Math.Min(ScaleX, ScaleY);
Vector2 PlayerPosition;
Vector2 Viewport;
float Scale;

public Matrix GetMatrix(float scrollingspeed)
{
  return Matrix.CreateTranslation(new Vector3(-PlayerPosition.X, -PlayerPosition.Y, 0) * scrollingspeed)
   * Matrix.CreateScale(Scale, Scale, 1)
   * Matrix.CreateTranslation(Viewport.X / 2, Viewport.Y / 2, 0);
}

I draw the sprites like this:

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.GetMatrix(0.8f));  
  BackgroundLayer1.Render(spriteBatch);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.GetMatrix(1.0f));
  Player.Render(spriteBatch);
spriteBatch.End();

//in the Player class:
batch.Draw(Playertex, new Rectangle(400, 240, Playertex.Width, Playertex.Height), null, Color.White, 0, new Vector2(Playertex.Width / 2, Playertex.Height / 2), SpriteEffects.None, 0f);
//in the BackgroundLayer1 class:
batch.Draw(Background1tex, new Rectangle(400, 240, Background1tex.Width, Background1tex.Height), null, Color.White, 0, new Vector2(Background1tex.Width / 2, Background1tex.Height / 2), SpriteEffects.None, 1.0f);

UPDATE1: In addition, the background sprites shouldn't repeat in a loop, because I have ten 800x480 pixel sprites that are drawn one after another. For example, the first sprite is drawn at (0,0), the second at (800,0), the third at (1600,0), … The last sprite(number 10) is the end of the level. The player can move back but not further. I will create more than one layer of sprites, but every layer with a different speed factor to create the Parallax Scrolling.

How can I add the speed factor so that the background sprites move slower than the player sprite?

UPDATE2: If I draw "BackgroundLayer1" with a scrolling speed of 1.0f, the sprite is drawn on the correct position, but if I change the scrolling speed to another value(for example 0.8f), then the sprites of "BackgroundLayer1" aren't drawn on the correct position.

Why are the sprites of "BackgroundLayer1" not drawn on the same position if I change the scrolling speed? What is wrong? The sprites should be drawn on the same positions, even if I change the scrolling speed.

In these two pictures, you see the differences if I change the scrolling speed of BackgroundLayer1:

scrollingspeed = 1.0f

http://s1.directupload.net/images/140505/qdi6cqwz.jpg

scrollingspeed = 0.8f

http://s1.directupload.net/images/140505/56ferzuv.jpg

Added additional information.
Source Link
Homer_Simpson
  • 615
  • 1
  • 7
  • 19

I want to create a Windows Phone jump-and-run game with parallax scrolling. The character should always be in centered in the viewport.

I use the following camera so that the viewport looks on every resolution the same:

Vector2 vp, gameWorldSize = new Vector2(800, 480);
Matrix Camera;
float ScaleX, ScaleY, Scale;

vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
ScaleX = vp.X / gameWorldSize.X;
ScaleY = vp.Y / gameWorldSize.Y;
Scale = Math.Min(ScaleX, ScaleY);

Camera = Matrix.CreateTranslation(-Player.Playerposition.X, -Player.Playerposition.Y,0)
   * Matrix.CreateScale(Scale, Scale, 1)
   * Matrix.CreateTranslation(vp.X / 2, vp.Y / 2, 0);

//Drawing the sprites
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Camera);
 ...

The camera is working fine and the player is always in the center of the viewport, but I don't know how to implement Parallax Scrolling. What is the best way to implement Parallax Scrolling?

How can I create a resizeable 2D viewport that follows the player and supports parallax scrolling?

UPDATE: In addition, the background sprites shouldn't repeat in a loop, because I have ten 800x480 pixel sprites that are drawn one after another. For example, the first sprite is drawn at (0,0), the second at (800,0), the third at (1600,0), … The last sprite(number 10) is the end of the level. The player can move back but not further. I will create more than one layer of sprites, but every layer with a different speed factor to create the Parallax Scrolling.

How can I add the speed factor so that the background sprites move slower than the player sprite?

I want to create a Windows Phone jump-and-run game with parallax scrolling. The character should always be in centered in the viewport.

I use the following camera so that the viewport looks on every resolution the same:

Vector2 vp, gameWorldSize = new Vector2(800, 480);
Matrix Camera;
float ScaleX, ScaleY, Scale;

vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
ScaleX = vp.X / gameWorldSize.X;
ScaleY = vp.Y / gameWorldSize.Y;
Scale = Math.Min(ScaleX, ScaleY);

Camera = Matrix.CreateTranslation(-Player.Playerposition.X, -Player.Playerposition.Y,0)
   * Matrix.CreateScale(Scale, Scale, 1)
   * Matrix.CreateTranslation(vp.X / 2, vp.Y / 2, 0);

//Drawing the sprites
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Camera);
 ...

The camera is working fine and the player is always in the center of the viewport, but I don't know how to implement Parallax Scrolling. What is the best way to implement Parallax Scrolling?

How can I create a resizeable 2D viewport that follows the player and supports parallax scrolling?

I want to create a Windows Phone jump-and-run game with parallax scrolling. The character should always be in centered in the viewport.

I use the following camera so that the viewport looks on every resolution the same:

Vector2 vp, gameWorldSize = new Vector2(800, 480);
Matrix Camera;
float ScaleX, ScaleY, Scale;

vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
ScaleX = vp.X / gameWorldSize.X;
ScaleY = vp.Y / gameWorldSize.Y;
Scale = Math.Min(ScaleX, ScaleY);

Camera = Matrix.CreateTranslation(-Player.Playerposition.X, -Player.Playerposition.Y,0)
   * Matrix.CreateScale(Scale, Scale, 1)
   * Matrix.CreateTranslation(vp.X / 2, vp.Y / 2, 0);

//Drawing the sprites
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Camera);
 ...

The camera is working fine and the player is always in the center of the viewport, but I don't know how to implement Parallax Scrolling. What is the best way to implement Parallax Scrolling?

How can I create a resizeable 2D viewport that follows the player and supports parallax scrolling?

UPDATE: In addition, the background sprites shouldn't repeat in a loop, because I have ten 800x480 pixel sprites that are drawn one after another. For example, the first sprite is drawn at (0,0), the second at (800,0), the third at (1600,0), … The last sprite(number 10) is the end of the level. The player can move back but not further. I will create more than one layer of sprites, but every layer with a different speed factor to create the Parallax Scrolling.

How can I add the speed factor so that the background sprites move slower than the player sprite?

Notice added Draw attention by Homer_Simpson
Bounty Started worth 100 reputation by Homer_Simpson
Added new code and additional information.
Source Link
Homer_Simpson
  • 615
  • 1
  • 7
  • 19

I want to create a Windows Phone jump-and-run game with parallax scrolling. The character should always be in centered in the viewport.

Parallax scrolling is easy to implement, but currently scaling the viewport to any resolution higher than 800×480 de-centers the character. The character moves out ofI use the viewport after a while. I'm scalingfollowing camera so that the viewport to make sure it looks the same on every device.

I use this code to scale the viewport. I calculateresolution the character's position like thissame:

NewcamerapositionVector2 vp, gameWorldSize = new Vector2(player800, 480);
Matrix Camera;
float ScaleX, ScaleY, Scale;

vp = new Vector2(GraphicsDevice.PlayerpositionViewport.Width, GraphicsDevice.Viewport.Height);
ScaleX = vp.X -/ VirtualScreenWidthgameWorldSize.X;
ScaleY = vp.Y / 2gameWorldSize.Y;
Scale = Math.Min(ScaleX, playerScaleY);

Camera = Matrix.CreateTranslation(-Player.Playerposition.YX, -Player.Playerposition.Y,0)
 VirtualScreenHeight  * Matrix.CreateScale(Scale, Scale, 1)
   * Matrix.CreateTranslation(vp.X / 2, vp.Y / 2, 0);

//Drawing the sprites
cameraspriteBatch.UpdateBegin(gameTimeSpriteSortMode.BackToFront, NewcamerapositionBlendState.AlphaBlend, null, null, null, null, Camera); 
 ...

The camera is working fine and the player is always in the center of the viewport, but I don't know how to implement Parallax Scrolling. What is the best way to implement Parallax Scrolling?

How can I create a resizeable 2D viewport that follows the player and supports parallax scrolling?

I want to create a Windows Phone jump-and-run game with parallax scrolling. The character should always be in centered in the viewport.

Parallax scrolling is easy to implement, but currently scaling the viewport to any resolution higher than 800×480 de-centers the character. The character moves out of the viewport after a while. I'm scaling the viewport to make sure it looks the same on every device.

I use this code to scale the viewport. I calculate the character's position like this:

Newcameraposition = new Vector2(player.Playerposition.X - VirtualScreenWidth / 2, player.Playerposition.Y - VirtualScreenHeight / 2); 
camera.Update(gameTime, Newcameraposition); 

How can I create a resizeable 2D viewport that follows the player and supports parallax scrolling?

I want to create a Windows Phone jump-and-run game with parallax scrolling. The character should always be in centered in the viewport.

I use the following camera so that the viewport looks on every resolution the same:

Vector2 vp, gameWorldSize = new Vector2(800, 480);
Matrix Camera;
float ScaleX, ScaleY, Scale;

vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
ScaleX = vp.X / gameWorldSize.X;
ScaleY = vp.Y / gameWorldSize.Y;
Scale = Math.Min(ScaleX, ScaleY);

Camera = Matrix.CreateTranslation(-Player.Playerposition.X, -Player.Playerposition.Y,0)
   * Matrix.CreateScale(Scale, Scale, 1)
   * Matrix.CreateTranslation(vp.X / 2, vp.Y / 2, 0);

//Drawing the sprites
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Camera); 
 ...

The camera is working fine and the player is always in the center of the viewport, but I don't know how to implement Parallax Scrolling. What is the best way to implement Parallax Scrolling?

How can I create a resizeable 2D viewport that follows the player and supports parallax scrolling?

Tweeted twitter.com/#!/StackGameDev/status/461926890673303553
Inlined links. Condensed. Improved grammar and word order.
Source Link
Anko
  • 13.5k
  • 10
  • 56
  • 82
Loading
Source Link
Homer_Simpson
  • 615
  • 1
  • 7
  • 19
Loading