Apr 1, 2011 at 8:46 PM
Edited Apr 1, 2011 at 9:49 PM
I'm in deep deep trouble with parallax. Although it seems easy to do, i still cannot get it.
I took the origin in a literal meaning, so i set it in the upper-left corner.
| the screen
Now scrollSpeed 1:1 works perfectly, but not the other layers: even without scrolling enabled, they are drawn on wrong coordinates, a matter of float points but that's:
(this is how i understood to build the level):
BUT in game...
OH Noes !! Look at the columns....
layers that are back to 1:1 are drawn shifted to left, layers that are in front of 1:1 are drawn shifted to right.
So i really can't get what's going on... And obviously, when i scroll the level, parallax works really bad, showing textures boundaries. Speeds are Ok, layers are scrolling respecting their speeds, but with that drawing issue the result is a mess.
Now this is the code for moving the MAIN camera:
const float ViewMargin = 0.45f;
const float ViewMarginX = 0.5f;
const float ViewMarginY = 0.35f;
// Calculate the edges of the screen.
float marginWidth = viewport.Width*ViewMarginX;
float marginLeft = cameraPosition.X + marginWidth;
float marginRight = cameraPosition.X + viewport.Width - marginWidth;
float marginHeight = viewport.Height* ViewMarginY;
float marginTop = cameraPosition.Y + marginHeight;
float marginBottom = cameraPosition.Y + viewport.Height - marginHeight;
// Calculate how far to scroll when the player is near the edges of the screen.
float cameraMovementX = 0.0f;
float cameraMovementY = 0.0f;
// Update the camera position, but prevent scrolling off the ends of the level.
//tenere conto della posizione di inizio nella mappa del livello per calcolare i bordi
float maxCameraPositionX = 22000; //viewport.Width*6;
float maxCameraPositionY = viewport.Height * 2;
if (kandar.Position.X < marginLeft)
cameraMovementX = kandar.Position.X - marginLeft;
else if (kandar.Position.X > marginRight)
cameraMovementX = kandar.Position.X - marginRight;
else if (kandar.Position.Y < marginTop)
cameraMovementY = kandar.Position.Y - marginTop;
else if (kandar.Position.Y > marginBottom)
cameraMovementY = kandar.Position.Y - marginBottom;
cameraPosition.X = MathHelper.Clamp(cameraPosition.X + cameraMovementX, 0, maxCameraPositionX);
cameraPosition.Y = MathHelper.Clamp(cameraPosition.Y + cameraMovementY, -2000f, maxCameraPositionY);
And it's pratically took from the platformer kit example... "kandar" is the player character, of course...
In the level class, i use in the partial Layer class Update method
//don't need to rotate or scale by now...
Transform = Matrix.Identity
* Matrix.CreateTranslation(-cameraPosition.X * this.ScrollSpeed.X, -cameraPosition.Y * this.ScrollSpeed.Y, 0);
And in Draw method:
sb.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, null, DepthStencilState.None, null, null, Transform);
(foreach item ....)
spriteBatch.Draw(texture, Position, source, TintColor, Rotation, Origin, Scale, effects, 0);
So... what's going on? Perhaps i missed some translation on the camera origin? But WHAT'S the camera origin? I still mean the upper left corner and i don't think that's the middlepoint of width and height....
Can u help me please? :( I'm so worried about that ... ^__^ Thank you!