Sign in to follow this  
dhartles

Need help with scrolling map

Recommended Posts

I set up a 2D tile map comprised of sprite objects for each tile. (This is the Managed Direct X Sprite which is a wrapper for the D3DX Sprite). My Mapsize defines the 2 dimensions [Row][Column] in a 2 Dimensional array of objects. My original scroll function for the map is as follows: (Pseudo) On KeyDown If W Get object from array at 0,0 If current Y position of object < 1/2 screenheight (player object always stays centered) for column = 0; column < [Column]; column++ for row == 0; row < [Row]; row++ GraphicSprite sprite = (GraphicsSprite)myArray.GetValue(row, column) sprite.Move(0,10) . . . So basically when the player presses the W key this will retrieve the first object in the array (0,0) and see if it's current Y position is less 1/2 screen height. If not, then it will iterate through every object in the array and change the Y position of all by 10 pixels. All objects are then re-drawn durintg the overridden onPain event for the form. While this works (currently runs at ~100fgp with a 40x40 map (thats 1600 objects), it just seems there has to be a better way to do this. I saw an example of using the Transform.World to 'scroll' the map, but when I tried to implement, the screen goes black after the function is called. Here's the couple of lines of code: defined in the class declartions; static Matrix scrollMatrix = Matrix.Identity; in the KeyDown Event; scrollMatrix = Matrix.Multiply(scrollMatrix, Matrix.Translation(0, 10, 0)); in the onPaint event; device.Transform.World = scrollMatrix This is all that the book has, but he's implements his sprite class diferently as well, so I'm not sure yet if I've implements something else differently that needs to be modified. Also, should I even bother trying to make the change. Is the World.Transform really any different than me looping through the array? Doesn't it basically have to do the same?(maybe it's implement using pointers though..). At 100fps does it really matter. I would like to get the transform.world to work though, I can't stand getting stuck on something and just letting it go. Thanks, Dave

Share this post


Link to post
Share on other sites
Use the matrix. What's happening is you're not undoing all of the transformations - they keep adding onto each other, and really quick everything is gone. It should look like this:
static Matrix scrollMatrix = Matrix.Identity;
scrollMatrix = Matrix.Multiply(scrollMatrix, Matrix.Translation(0, 10, 0));
device.Transform.World = scrollMatrix
*render
scrollMatrix = Matrix.Identity;
scrollMatrix = Matrix.Multiply(scrollMatrix, Matrix.Translation(0, -10, 0));
device.Transform.World = scrollMatrix

Not the changed part: 10+-10 = 0; therefore the changes will not accumulate as you keep going through.

Just be careful you undo it only when needed, though. I don't want to contradict your book or anything, but I find its easier to keep a D3DXVECTOR floating around with the world matrix translation coordinates, and just use it every pass. Matrix math really isn't all that much overhead, and it makes life a little easier to just be able to modify the contents of a D3DVECTOR rather than dealing with matrix functions.

EDIT: You know what I just realized? Did you set up all the stuff for the matrices? You need to set the FOV, etc and some other stuff you should be able to find in the SDK. That might be the real reason it isn't working; generally the matrices aren't set up for 2D stuff. Doh!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this