Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 2 (42 posts) 1 2 Next >

Rotating a collision rectangle (racing game)

Last post 1/9/2010 7:53 AM by Kagadome. 41 replies.
  • 12/29/2009 3:36 PM

    Rotating a collision rectangle (racing game)

    Hi, I've looked all over for rotating collision rectangles but I couldn't find anything I could fully understand because the way I am doing collision is different to what i have seen.

    How I have made the game is I have rows of tyres that I simply placed on top of the background and made them each a collision rectangle.

    Here's an image of the track and highlighted in red are the collision rectangles.



    Below is another image but shows how I think the collision rectangle for the car is reacting to the rotation of the car.



    I am just clueless at the moment on how I could rotate the collision rectangle alongside the car.

    This is the code I have used to set the rectangles and how I move the car.

    protected override void Update(GameTime gameTime)
            {
    
                //setting rectangles for collision detection
                blueCarRect = new Rectangle((int)blueCarPosition.X - 29, (int)blueCarPosition.Y -61,
                    blueCar.Width,
                    blueCar.Height);
               
    
                v43Rect = new Rectangle((int)v43Position.X, (int)v43Position.Y,
                    v43Texture.Width,
                    v43Texture.Height);
                v43_2Rect = new Rectangle((int)v43_2Position.X, (int)v43_2Position.Y,
                    v43_2Texture.Width,
                    v43_2Texture.Height);
                v35Rect = new Rectangle((int)v35Position.X, (int)v35Position.Y,
                    v35Texture.Width,
                    v35Texture.Height);
                v19Rect = new Rectangle((int)v19Position.X, (int)v19Position.Y,
                    v19Texture.Width,
                    v19Texture.Height);
                v16Rect = new Rectangle((int)v16Position.X, (int)v16Position.Y,
                    v16Texture.Width,
                    v16Texture.Height);
                h22Rect = new Rectangle((int)h22Position.X, (int)h22Position.Y,
                    h22Texture.Width,
                    h22Texture.Height);
                h22_2Rect = new Rectangle((int)h22_2Position.X, (int)h22_2Position.Y,
                    h22_2Texture.Width,
                    h22_2Texture.Height);
                h11Rect = new Rectangle((int)h11Position.X, (int)h11Position.Y,
                    h11Texture.Width,
                    h11Texture.Height);
                h11_2Rect = new Rectangle((int)h11_2Position.X, (int)h11_2Position.Y,
                    h11_2Texture.Width,
                    h11_2Texture.Height);
                h11_3Rect = new Rectangle((int)h11_3Position.X, (int)h11_3Position.Y,
                    h11_3Texture.Width,
                    h11_3Texture.Height);
                h10Rect = new Rectangle((int)h10Position.X, (int)h10Position.Y,
                    h10Texture.Width,
                    h10Texture.Height);
    
                KeyboardState keys = Keyboard.GetState(); // This is making the variable "keys" which purpose will be to get the state of the keyboard inputs 60 times a second.
    
                //if the up arrow key is pressed and it collides with a tyre barrier then the car speed is reduced to 0.1
                if (keys.IsKeyDown(Keys.Up) && (blueCarRect.Intersects(v43Rect) || (blueCarRect.Intersects(v43_2Rect) ||
                   (blueCarRect.Intersects(v35Rect) || (blueCarRect.Intersects(v19Rect) ||
                   (blueCarRect.Intersects(v16Rect) || (blueCarRect.Intersects(h22Rect) ||
                   (blueCarRect.Intersects(h22_2Rect) || (blueCarRect.Intersects(h11Rect) ||
                   (blueCarRect.Intersects(h11_2Rect) || (blueCarRect.Intersects(h11_3Rect) ||
                   (blueCarRect.Intersects(h10Rect)))))))))))))
                {
                    blueCarSpeed = 0.1f;
                }
               
                //when no collisions are active while the up arrow is pressed then the car will move at 5
                else if (keys.IsKeyDown(Keys.Up) )
                {
                    blueCarSpeed = 5f;
                }
    
                //if the down arrow key is pressed and it collides with a tyre barrier then the car speed is reduced to -0.1
                if (keys.IsKeyDown(Keys.Down) && (blueCarRect.Intersects(v43Rect) || (blueCarRect.Intersects(v43_2Rect) ||
                   (blueCarRect.Intersects(v35Rect) || (blueCarRect.Intersects(v19Rect) ||
                   (blueCarRect.Intersects(v16Rect) || (blueCarRect.Intersects(h22Rect) ||
                   (blueCarRect.Intersects(h22_2Rect) || (blueCarRect.Intersects(h11Rect) ||
                   (blueCarRect.Intersects(h11_2Rect) || (blueCarRect.Intersects(h11_3Rect) ||
                   (blueCarRect.Intersects(h10Rect)))))))))))))
                {
                    blueCarSpeed = -0.1f;
                }
                //when no collisions are active while the down arrow key is pressed then the car will move at -5
                else if (keys.IsKeyDown(Keys.Down))
                {
                    blueCarSpeed = -5f;
                }
                //when the left arrow is pressed then rotate the car anti-clockwise by 5 radians
                if (keys.IsKeyDown(Keys.Left))
                {
                    blueCarRotation -= MathHelper.ToRadians(5f);
                }
                //when the right arrow is pressed then rotate the car clockwise by 5 radians
                else if (keys.IsKeyDown(Keys.Right))
                {
                    blueCarRotation += MathHelper.ToRadians(5f);
                    
                }
                //mathmatical calculations telling the car which direction to move depending on speed and rotation
                blueCarPosition.X += (float)Math.Sin(blueCarRotation) * blueCarSpeed;
                blueCarPosition.Y += (float)Math.Cos(blueCarRotation) * -blueCarSpeed;
                blueCarSpeed = 0f;

    Any help would be appreciated.

    Thank you.
  • 12/29/2009 6:07 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    One simple trick (instead of trying to figure out rotating rectangle collision) is to simply define your cars collision area with 3 circles instead.
    You can even have them overlap so you can make sure you're covering most of the car. This will give you an easy way of doing collision detection now
    even when the car is turning.
  • 12/29/2009 7:09 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    Mainly the code is ok but when drawing the texture you will need to set the origin of car rectangle to be at the center of the car texture and then it should rotate as the car rotates(if its not a problem could you post the draw code)
  • 12/29/2009 7:13 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    I have one noob question.
    How can you use three circles as a rectengle,what would code for that look like?

    p.s sry for my bad english.
  • 12/29/2009 9:37 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    I would also like to know more about the 3 circle collision, I've never seen it before I will look into it though.

    Also about setting the origin, I have set the origin of the car texture to it's center pixel which is (29, 61) because if I set the origin of the car to (0,0) when I turn the car it rotates from the top left corner. At the moment I have the collision rectangle as - 29 and - 61 as this makes the rectangle cover the texture. When I change the values to 29 and 61 it gives me errors such as "Expected :" and "(" along with "Invalid expression term int" and I don't know why as it works fine with -29 and -61.

    Below is the code for setting the car rectangle and the draw for the car texture.

    blueCarRect = new Rectangle((int)blueCarPosition.X - 29, (int)blueCarPosition.Y -61,
                    blueCar.Width,
                    blueCar.Height);

    protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                           spriteBatch.Draw(blueCar, blueCarPosition, null, Color.White, blueCarRotation, new Vector2(29, 61), 1f, SpriteEffects.None, 0f);// this code will effectivley place my blue car on screen and will give it the proprties it needs to perform.
                spriteBatch.End();
       
                           base.Draw(gameTime);
            }
    
    
    
    

    Here is all my draw code:

    
    
    protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                spriteBatch.Begin(); // this code will effectivley place my track and text on screen and will give it the proprties it needs to perform.
                spriteBatch.Draw(track, viewportRect, Color.White);
                spriteBatch.DrawString(lap_counterFont, lap_counter_output, lap_counterPos, Color.White, 0f, lap_counterOrigin, 1f, SpriteEffects.None, 0f);
                spriteBatch.End();
    
                spriteBatch.Begin(); // this code will effectivley place my tyre barriers and car on screen and will give it the proprties it needs to perform.
                spriteBatch.Draw(v43Texture, v43Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(v43_2Texture, v43_2Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(v16Texture, v16Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(v19Texture, v19Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(v35Texture, v35Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(h22Texture, h22Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(h22_2Texture, h22_2Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(h11Texture, h11Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(h11_2Texture, h11_2Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(h11_3Texture, h11_3Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(h10Texture, h10Position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
                spriteBatch.Draw(blueCar, blueCarPosition, null, Color.White, blueCarRotation, new Vector2(29, 61), 1f, SpriteEffects.None, 0f);// this code will effectivley place my blue car on screen and will give it the proprties it needs to perform.
                spriteBatch.End();
             
                
                
    
                base.Draw(gameTime);
            }
    
    

    Thank you for your input so far.

    
                                                                        
                                                                    
  • 12/31/2009 1:11 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    I'm still needing help with this, I couldn't find any sources on how to use circles instead of rectangles and changing the origins didn't work.


    Does anyone know any other way of doing this?

    Thanks.

  • 12/31/2009 2:19 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    The problem with your code, as you pointed out, is that you do not rotate the bounding rectangle of the car.
    Implementing collision detection on rotated rectangles is not so easy, which is why you got the suggestion of using circles instead.

    Divide your car in three sections, front, middle and back, find out the center of each section. You can use half of the width of the car as the radius of each circle.
    You may want to make the front of your car a bit more round, otherwise collisions with the front-right and front-left won't be detected. If that's not good enough, just keep adding more circles to cover more of the car.

    Whenever you rotate the car, update the positions of the centers of each circle.

    I don't know if XNA has 2d collision detection built-in, but you could just as well use the 3d primitives (BoundingBox and BoundingSphere).

    An alternative solution could be to use per-pixel collision: XNA CCO tutorial
  • 12/31/2009 3:54 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    Yep, define your collision area using 4 Bounding Spheres. They just move alone with the car and there's none of that hassle to rotate them. You could try and do it with 3 of them but it looks like you get better coverage with 4. Should look a little something like this.




    The BoundingSpeheres you define for your collision areas just move with your car, when the car rotates, they just move rather then rotating and you can still do checks to see if the BoundingSphere intersects your BoundingRectangles.

    I just tend to have this rule in development that if a problem is becoming overly complicated and I'm having difficulty solving it, I must be solving the wrong problem. Seems to me that's what you were doing with your rotating rectangle collisions.
  • 12/31/2009 8:52 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    Okay I have started to try and use circles because it seems a lot easier for me to do but I have no experience with them and I don't know where to start so if you could answer these questions that would be helpful as well.
    So far I have written this in the Update method. This I think has set the center of the circle to the cars origin and I have given the circle a radius of 29. And the other is representing one row of tyres on my track.

    blueCarSphere1 = new BoundingSphere(new Vector3(blueCarPosition.X, blueCarPosition.Y, 0), (float) 29f);

    v43Rect = new Rectangle((int)v43Position.X, (int)v43Position.Y,
                    v43Texture.Width,
                    v43Texture.Height);


    if (keys.IsKeyDown(Keys.Up) && (blueCarSphere1.Intersects(v43Rect)
    {
                    blueCarSpeed = 0.1f;
                }

    else if (keys.IsKeyDown(Keys.Up))
                {
                    blueCarSpeed = 5f;
                }


    Can a BoundingSphere be used to intersect with a Rectangle or would I have to convert all my Rectangles to BoundingBox's?

    Thanks again for straightening all of this out for me and I wish you a happy new year!

  • 12/31/2009 9:23 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    Yeah, you would have to check intersection with a BoundingSphere with a BoundingBox but you could just add an extension method so that BoundingSphere could check intersection with a Rectangle instead.

    Something like this would probably work.

        public static class BoundingSphereExtensions 
        { 
            public static bool Intersects(this BoundingSphere theSphere, Rectangle theRectangle) 
            { 
                Vector3 aMinimum = new Vector3(theRectangle.X, theRectangle.Y, 0); 
                Vector3 aMaximum = new Vector3(theRectangle.X + theRectangle.Width, theRectangle.Y + theRectangle.Height, 0); 
     
                BoundingBox aBox = new BoundingBox(aMinimum, aMaximum); 
     
                return theSphere.Intersects(aBox); 
            } 
     
        } 


    Then you wouldn't have to go through and convert all your Rectangles to BoundingSpheres. You could add a similar extension method for the Rectangle.Intersects method to take in a BoundSphere and return whether it intersects or not.

  • 1/1/2010 5:59 AM In reply to

    Re: Rotating a collision rectangle (racing game)

    What is up with all of the suggestions for bounding spheres for a 2d game?? Contrary to everything everyone
    else has posted I strongly suggest you keep everything in a 2D game 2D. You guys to realize as a Texture2D
    there is no get Bonding Sphere function. He would have to to create every bounding sphere by hand and
    also keep track of the geometry to keep the sphere is the correct place relative to the car.

    Check out the 3rd part in the collision series for examples per-pixel collision with with transformed 2d objects.
    Just swap out the per pixel part with the rectangle collision in the first part of the series. It's been a while since
    I've done anything in 2d but I really have no idea why you would ever use a bounding spheres in a 2d game.

    http://creators.xna.com/en-US/tutorial/collision2dperpixeltransformed

    Not to be rude but using 3D Bounding Spheres in a 2D game makes absolutely no sense.

  • 1/1/2010 6:30 AM In reply to

    Re: Rotating a collision rectangle (racing game)

    I couldn't get my head around the extension methods, I tried for a long time but didn't understand them so I did it the long way round and made 4 Bounding Spheres and a lot of Bounding Boxes.
    protected override void Update(GameTime gameTime)
            {
    
    
                //setting 4 bounding spheres to cover my car for collision detection
                blueCarSphere1 = new BoundingSphere(new Vector3(blueCarPosition.X, blueCarPosition.Y - 29, 0), (float) 29f);
                blueCarSphere2 = new BoundingSphere(new Vector3(blueCarPosition.X, blueCarPosition.Y - 15, 0), (float)29f);
                blueCarSphere3 = new BoundingSphere(new Vector3(blueCarPosition.X, blueCarPosition.Y + 15, 0), (float)29f);
                blueCarSphere4 = new BoundingSphere(new Vector3(blueCarPosition.X, blueCarPosition.Y + 29, 0), (float)29f);
                
    
                //setting Bounding Boxes the cover my tyres for collision detection
                v43Box = new BoundingBox((new Vector3 (v43Position.X, v43Position.Y, 0)), (new Vector3 (v43Position.X + v43Texture.Width, v43Position.Y + v43Texture.Height, 0)));
                v43_2Box = new BoundingBox((new Vector3(v43_2Position.X, v43_2Position.Y, 0)), (new Vector3(v43_2Position.X + v43_2Texture.Width, v43_2Position.Y + v43_2Texture.Height, 0)));
                v35Box = new BoundingBox((new Vector3(v35Position.X, v35Position.Y, 0)), (new Vector3(v35Position.X + v35Texture.Width, v35Position.Y + v35Texture.Height, 0)));
                v19Box = new BoundingBox((new Vector3(v19Position.X, v19Position.Y, 0)), (new Vector3(v19Position.X + v19Texture.Width, v19Position.Y + v19Texture.Height, 0)));
                v16Box = new BoundingBox((new Vector3(v16Position.X, v16Position.Y, 0)), (new Vector3(v16Position.X + v16Texture.Width, v16Position.Y + v16Texture.Height, 0)));
                h22Box = new BoundingBox((new Vector3(h22Position.X, h22Position.Y, 0)), (new Vector3(h22Position.X + h22Texture.Width, h22Position.Y + h22Texture.Height, 0)));
                h22_2Box = new BoundingBox((new Vector3(h22_2Position.X, h22_2Position.Y, 0)), (new Vector3(h22_2Position.X + h22_2Texture.Width, h22_2Position.Y + h22_2Texture.Height, 0)));
                h11Box = new BoundingBox((new Vector3(h11Position.X, h11Position.Y, 0)), (new Vector3(h11Position.X + h11Texture.Width, h11Position.Y + h11Texture.Height, 0)));
                h11_2Box = new BoundingBox((new Vector3(h11_2Position.X, h11_2Position.Y, 0)), (new Vector3(h11_2Position.X + h11_2Texture.Width, h11_2Position.Y + h11_2Texture.Height, 0)));
                h11_3Box = new BoundingBox((new Vector3(h11_3Position.X, h11_3Position.Y, 0)), (new Vector3(h11_3Position.X + h11_3Texture.Width, h11_3Position.Y + h11_3Texture.Height, 0)));
                h10Box = new BoundingBox((new Vector3(h10Position.X, h10Position.Y, 0)), (new Vector3(h10Position.X + h10Texture.Width, h10Position.Y + h10Texture.Height, 0)));
    

    The only problem is that I am having trouble pinning the spheres to the car because they are acting exactly the same as my first ever Collision Rectangle, meaning they aren't following/rotating with the car but instead all of the spheres are stuck in a vertical formation (by the looks of it). How would I make this work properly?

    So far all I've managed to do is make my code 4x longer, I'm doing something wrong and I can't work it out for the life of me :(

  • 1/1/2010 6:48 AM In reply to

    Re: Rotating a collision rectangle (racing game)

    Pinning the spheres to the car is going to be a pain, you're going to have to calculate the position manually using trig.
    The math's actually not that difficult but seriously the using 3d collision detection methods in a 2d game is stupid.
    Please look at the 2D collision tutorials. If you have trouble understanding them post here asking questions. It
    uses the same data used to draw the textures to see if their rectangles collide. Much less code and much more intuitive.

    Link again to the solution of all your problems. There is a whole serise on this very website about how to do this properly.
    http://creators.xna.com/en-US/tutorial/collision2drectangle
  • 1/1/2010 6:55 AM In reply to

    Re: Rotating a collision rectangle (racing game)

    George Clingerman:
    Yep, define your collision area using 4 Bounding Spheres. They just move alone with the car and there's none of that hassle to rotate them. You could try and do it with 3 of them but it looks like you get better coverage with 4. Should look a little something like this.




    The BoundingSpeheres you define for your collision areas just move with your car, when the car rotates, they just move rather then rotating and you can still do checks to see if the BoundingSphere intersects your BoundingRectangles.

    I just tend to have this rule in development that if a problem is becoming overly complicated and I'm having difficulty solving it, I must be solving the wrong problem. Seems to me that's what you were doing with your rotating rectangle collisions.


    They do NOT move with the car. You are totally making this the wrong problem and you making this 10x harder than it needs to be. This is not a 3D model as all of you seem to think it is a sprite.

    How on earth did we reach the conclusion rotating 4 Bounding Spheres was easier than rotating a rectangle?
  • 1/1/2010 8:29 AM In reply to

    Re: Rotating a collision rectangle (racing game)

    A sphere can be used like a circle. And moving circles is pretty dang easy. And it's WAY easier than rotating a rectangle. I use BoundingSpheres and BoundingBoxes in my 2D games all the time.

    There's actually no such thing as a 2D game, it's all built in a 3D gamespace just with a fixed camera :) 
  • 1/1/2010 8:56 AM In reply to

    Re: Rotating a collision rectangle (racing game)

    Rotating 4 circles keeping their positions relative to each other is not easy at all. Much harder than rotating a rectangle actually.

    Also thanks for the obvious technicality. All my personal studying and academic training never prepared me for such an out of the box idea.

    If he is actually drawing the model rotated as it appears he is in the second picture. He has all the data right
    there needed for the most accurate collision detecting possible. The Texture2D is already a rectangle. Math w/
    rectangles is easy. Math with 4 spheres that have to stay relative to each other is not.

    Not to mention rotating a bounding box is way more difficult than rotating a rectangle. A regular BB is aligned to the axis.

    Looking at your posts you constantly make it seem like its a circle that automatically sticks where you want it. It doesn't.
    All he needs to to is make 1 function using basic math, that can easily be found in the collision series, that replaces the rectangle's
    intersects function and takes rotation into account. All he needs to do is add 1 function and change 1 line of code.

    2D collision is simple when you don't spend hours trying to learn how to rotate 4 circles keeping their positions relative to each other.

    Rotate 4 circles keeping their positions relative to each other and a rectangle is what you are making him do and you never explain to him how to do it.
    Explain how to do this to him or your idea is useless. Or you could let him learn how to do normal 2D collision and he could reuse 2-3 functions and never have
    to worry about it again.



    @OP
    The code in the 2D example I liked to can be used with very little modification.
    And check out Riemer's 2D tutorial if you don't get the matrices
    http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Coll_Detection_Matrices.php 

  • 1/1/2010 10:38 AM In reply to

    Re: Rotating a collision rectangle (racing game)

    ClassicThunder:
    Pinning the spheres to the car is going to be a pain, you're going to have to calculate the position manually using trig.
    The math's actually not that difficult but seriously the using 3d collision detection methods in a 2d game is stupid.
    Please look at the 2D collision tutorials. If you have trouble understanding them post here asking questions. It
    uses the same data used to draw the textures to see if their rectangles collide. Much less code and much more intuitive.

    Link again to the solution of all your problems. There is a whole serise on this very website about how to do this properly.
    http://creators.xna.com/en-US/tutorial/collision2drectangle


    Rotating a rectangle and rotating a point relatively to another are equally easy, they use the same concept.

    Here is the first google hit on 2d rotation: 2D Rotation. It's really short, but all you need are the two lines below "hence:"
    x' = x cos f - y sin f
    y' = y cos f + x sin f

    The sprite-based approach is fine, using 3d works just as well. As a positive side effect of using the 3d API, you get to learn something that will be useful for the day you jump to 3d.
  • 1/1/2010 5:17 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    Kind of curious about the rudeness here ClassicThunder. I'm actually trying to be very helpful. And I *HAVE* done this before. When using the BoundSphere and BoundingBox you can actually ignore the 3D aspect of them. In fact, you can make their Z values 0 and make them just 2D objects.

    Here's a simple sample project showing the approach. I haven't added the 4 circles or rotation, but the logic doesn't really change as far as that goes. The positions for his sprites be they circles or squares just update their corresponding BoundingSpheres and BoundingBoxes with the new values.

    As far as chaining together BoundingSpheres and rotating them, the point is he doesn't HAVE to rotate them. They're perfect circles. He just has to move them. And they don't follow each other, they just all move at the same pace.

    Really confused why you're being so confrontational when I'm trying to help someone. There definitely could be something I'm not understanding here, but the approach I'm suggesting seems quite simple to me.


  • 1/1/2010 5:39 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    Ok, looks like rotating with the object is a little trickier. Straight line movement is simple, but you are correct, rotating the circles while the car sprite rotates is a little trickier. I'm going to keep playing with it to see if I can find an easy way to do it.

    You really SHOULD learn how to use extension methods though. I handed you the code, all you have to do it search in your favorite search engine for "Writing Extension Methods in C#" and that will teach you the rest..
  • 1/1/2010 6:18 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    Well I have learnt a lot from this and I am very grateful for all of your help and your discussions are also educational for me, I am doing those collision tutorials at the moment so I will report back once I've done them and understand them.

    When I first set out to do this I thought it would be easy ^^
  • 1/2/2010 11:53 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    Ok sry for my time-out.
    I think this line is the problem,
    cuz you arnt placing the cars rectangle in the middle of the car when you are drawing it.
    Change this:
    spriteBatch.Draw(blueCar, blueCarPosition, null, Color.White, blueCarRotation, new Vector2(29, 61), 1f, SpriteEffects.None, 0f);// this code will effectivley place my blue car on screen and will give it the proprties it needs to perform.

    to this;
    spriteBatch.Draw(blueCar, blueCarPosition, null, Color.White, blueCarRotation, new Vector2(blueCar.Width/2,blueCar.Height/2), 1f, SpriteEffects.None, 0f);// this code will effectivley place my blue car on screen and will give it the proprties it needs to perform.

    Hope this helps.
               
  • 1/3/2010 7:56 AM In reply to

    Re: Rotating a collision rectangle (racing game)

    George Clingerman:
    Ok, looks like rotating with the object is a little trickier. Straight line movement is simple, but you are correct, rotating the circles while the car sprite rotates is a little trickier. I'm going to keep playing with it to see if I can find an easy way to do it.


    My last 3-4 posts were about how the spheres were not automatically rotated in accordance to the rectangle. I apologize if my constantly increased emphasis of this fact was perceived as rude. While trying to be helpful you never offered a working solution and your proposed solution is a hack that, while it does give a bit of introductory for 3D, prevents accurate enough collision for more advanced 2D games.

    George Clingerman:
    You really SHOULD learn how to use extension methods though. I handed you the code, all you have to do it search in your favorite search engine for "Writing Extension Methods in C#" and that will teach you the rest..


    Agreed.

    @ johdex
    Like I said the math is simple however learning to use matricies like in the tutorials in needed in order move onto more advanced topics.

    @ All
    I don't get what the point of having such excellent educational resources on this very website if we're going to teach every new guy a hack around a basic problem such as 2D collision detecting.
  • 1/3/2010 2:50 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    I've managed to do the Per-Pixel collision but this 3rd Tutorial for Transformed objects is proving very difficult for me to follow as it is written for a totally different context to my game. I'll see what I can do today but it looks like I'm going to make a game that's easier to code.

    @ Slypro, that code is used to draw my car Texture not the car Rectangle.
  • 1/3/2010 4:20 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    Teddy1991:
    I've managed to do the Per-Pixel collision but this 3rd Tutorial for Transformed objects is proving very difficult for me to follow as it is written for a totally different context to my game. I'll see what I can do today but it looks like I'm going to make a game that's easier to code.

    @ Slypro, that code is used to draw my car Texture not the car Rectangle.


    The important bits are the matrices and the IntersectPixels(Matrix, int, int, Color[], Matrix, int, int, Color[]).

    Think of a matrix as a tiny box in the middle of your sprite. What ever it does you sprite does.

                    Matrix blockTransform = 
                        Matrix.CreateTranslation(new Vector3(-blockOrigin, 0.0f)) * 
                        // Matrix.CreateScale(block.Scale) *  would go here 
                        Matrix.CreateRotationZ(blocks[i].Rotation) * 
                        Matrix.CreateTranslation(new Vector3(blocks[i].Position, 0.0f)); 

    The first Translation places the matrix in the middle of your sprite, the Rotation Z rotates your object, and the second Translation actually moves your sprite.
    For the Vector3s the last value is always the height so set it as 0 always.

    For you:
    Matrix.CreateTranslation(new Vector3(-CarTexture.Width / 2, -CarTexture.Height / 2, 0.0f))
    Use the same rotation you use to rotate you sprite in the draw code for the Rotation.
    And for the last Translation use your blue car position.
  • 1/3/2010 6:51 PM In reply to

    Re: Rotating a collision rectangle (racing game)

    ClassicThunder:
    *matrix transformation explanation*

    Edit: Wow, I really wasn't thinking properly, ignore what I said about the Vector4. 
Page 1 of 2 (42 posts) 1 2 Next > Previous Discussion Next Discussion