Codetopia

A place to talk about things related to XNA, .NET, and enterprise application development.
Welcome to Codetopia Sign in | Join | Help
in
Home My Book Blogs Forums Photos Files Roller

Chapter 9 Exercise 3

Last post 03-17-2008, 12:01 AM by XNA42. 4 replies.
Sort Posts: Previous Next
  •  03-05-2008, 6:11 PM 424

    Chapter 9 Exercise 3

    Hello Everyone

    Hello Mr Hall

    First I want to say that you have wrote great book, its my 4th book that I try to read on XNA and it is have been pleasant experience so far unlike some others. I have some constructive critique I am going to share with you when I have completely done the book.

    To business: I am trying to do Chapter 9 Exercise 3. Little bit challenging for me. Here is what I understood I need to do (correct me if wrong).
    1.When scrolling with the mouse the whole grid with rectangles move up or down.
    2. If rectangle is half out of the screen draw in on the other end.
    3. Scrolling around should keep the state of the rectangles

    On paper sounds very logical but when I try to work out with your algorithm things get messy. So I started with making a method to control a variable

    // the amount of scroll
     private int yScroll = 0;



      private int lastScrollWheelValue;
            private void CheckScroll(MouseState mouse1)
            {
                if (mouse1.ScrollWheelValue > lastScrollWheelValue)
                    yScroll++;
                if (mouse1.ScrollWheelValue < lastScrollWheelValue)
                    yScroll--;
                lastScrollWheelValue = mouse1.ScrollWheelValue;
            }


    that I just add to the drawing position of the rectangle in the drawing loop. That made the thing scroll ok in drawing but not when the mouse need to detect on which square it is hovering over.

    Now I though maybe i should use the same variable for the "collision detection" algorithm but then I thought "this is getting messy". So I started thinking over the problem. Isnt it going to be better to represent each Block with Rectanlge possition? That way it will have more straight forward algorithm for detecting if we are hovering over something and it will be easier to manipulate on the screen. I think it will even be easier to draw the block on two separate parts (as this is one of the problems). It will be easier to split the block texture into two rectangles and draw them on top and bottom of the screen.

    After all that said I havent written the code :D but I will and I will post it here.

    After that long post I just wanted to ask to give me any tips how I can do the exercise with your version of the code? :)

    Sorry for long post.

    Evgeni

  •  03-05-2008, 10:30 PM 426 in reply to 424

    Re: Chapter 9 Exercise 3

    Evgeni,

    Thanks for the kind words, and thanks for buying the book.

    I need to step away from the computer for the evening to spend a little time with the family, but I plan on replying to your post in the morning.

    I just wanted to release your comments and questions from their moderation limbo.

    -- joe

    There are 10 kinds of people in this world -- Those that can count in binary, and those that can't.
  •  03-06-2008, 10:41 AM 427 in reply to 424

    Re: Chapter 9 Exercise 3

    Zammy:
    First I want to say that you have wrote great book, its my 4th book that I try to read on XNA and it is have been pleasant experience so far unlike some others. I have some constructive critique I am going to share with you when I have completely done the book.


    Thanks for the kind words. I look forward to reading your feedback.

    Zammy:
    On paper sounds very logical but when I try to work out with your algorithm things get messy.


    I didn't provide an algorithm. I just described the new feature that you should implement. Stick out tongue [:P]

    Zammy:
    So I started with making a method to control a variable

    // the amount of scroll
     private int yScroll = 0;

      private int lastScrollWheelValue;
            private void CheckScroll(MouseState mouse1)
            {
                if (mouse1.ScrollWheelValue > lastScrollWheelValue)
                    yScroll++;
                if (mouse1.ScrollWheelValue < lastScrollWheelValue)
                    yScroll--;
                lastScrollWheelValue = mouse1.ScrollWheelValue;
            }


    that I just add to the drawing position of the rectangle in the drawing loop. That made the thing scroll ok in drawing but not when the mouse need to detect on which square it is hovering over.

    ...

    After that long post I just wanted to ask to give me any tips how I can do the exercise with your version of the code? :)


    Actually your basic approach is the one that I would take myself. And as you rightly point out, you have to keep the drawing separate from the input.

    The point at which you and I diverge is what leads you to an implementation that "gets messy".

    Keep thinking of the grid as an array of states. Keep indexing it with simple, integer-based X and Y values. Don't worry about the rectangles that get drawn on the screen. You have to keep the data separate from the presentation.

    Here's a quick implementation that I threw together. As you can see, my "m_scrollY" is equivalent to your "yScroll", and the logic for both is very similar.

            protected int m_scrollY = 0;
            protected int m_lastMouseScroll = 0;

            protected override void Update(GameTime gameTime)
            {
               ...
                // get the mouse state, and record its current location
                MouseState mouse1 = Mouse.GetState();
                CursorPosition.X = mouse1.X;
                CursorPosition.Y = mouse1.Y;

                // which grid cell is this mouse over now?
                int gx = (int)Math.Floor(CursorPosition.X / CELL_WIDTH);
                int gy = (int)Math.Floor(CursorPosition.Y / CELL_HEIGHT);

                int deltaScroll = mouse1.ScrollWheelValue - m_lastMouseScroll;
                if (deltaScroll != 0) { deltaScroll /= Math.Abs(deltaScroll); }
                m_lastMouseScroll = mouse1.ScrollWheelValue;
                m_scrollY = (m_scrollY + deltaScroll) % GRID_HEIGHT;
                if (m_scrollY < 0) { m_scrollY += GRID_HEIGHT; }

                for (int y = 0; y < GRID_HEIGHT; y++) // for each row
                {
                    int adjustedY = (y + m_scrollY) % GRID_HEIGHT;

                    for (int x = 0; x < GRID_WIDTH; x++) // for each column
                    {
                        // if the current cell is under the mouse
                        if (gx == x && gy == adjustedY)
                        {
                            // start the fade effect
                            BlockStates[x, y].EffectAge = EFFECT_DURATION;
                            ...

    You use the adjustedY to check the cursor position in Update, but all references to the state array remain the same (indexed by "y" rather than "adjustedY").

    On the drawing side of things, just update the outer loop so that the pos.Y value is calculated from the adjustedY (rather than the plain-Jane "y" value).

                for (int y = 0; y < GRID_HEIGHT; y++) // for each row
                {
                    // row as pixel
                    int adjustedY = (y + m_scrollY) % GRID_HEIGHT;
                    pos.Y = adjustedY * 32 + 2;

    I hope this helps you. If you still have questions, let me know.

    -- joe


    There are 10 kinds of people in this world -- Those that can count in binary, and those that can't.
  •  03-06-2008, 6:14 PM 428 in reply to 427

    Re: Chapter 9 Exercise 3

    Oh.... I got the exercise different that is why i thought is hard. I though i need to shift the drawing not just shifting the states of the cells. :))) I was actually scrolling the whole rectangles trying to draw two halfs when they were sticking out of the screen. Your implementationg is not messy sorry. Thank you for the fast reply!
  •  03-17-2008, 12:01 AM 445 in reply to 428

    Re: Chapter 9 Exercise 3

    Zammy:
    Oh.... I got the exercise different that is why i thought is hard. I though i need to shift the drawing not just shifting the states of the cells. :))) I was actually scrolling the whole rectangles trying to draw two halfs when they were sticking out of the screen. Your implementationg is not messy sorry. Thank you for the fast reply!


    just for the record, i too misunderstood the concept and thought that having the grid being drawn to different Y offset values (values smaller than the height of one grid cell)... not the contents of the grid cells.

    just a thought, but perhaps this came from the prior chapter's program that had the grid background scrolling behind the ship.

    thanks for having this forum!  42
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems