Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. More Collision (XNA)

More Collision (XNA)

Scheduled Pinned Locked Moved C#
helpalgorithmsquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MasterSharp
    wrote on last edited by
    #1

    I have this code so far on checking collision. It doesn't quite work. if (Velocity.Y > 0) { for (int n = 0; n < 25; ++n) { if (Position.Y + Sprite.Height > grassTopBlock[n].Position.Y && Position.Y < grassTopBlock[n].Position.Y + grassTopBlock[n].Sprite.Height) { if (Position.X + Sprite.Width > grassTopBlock[n].Position.X && Position.X < grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width) { Position.Y = grassTopBlock[n].Position.Y - Sprite.Height; } } } } /// FIX ME! ( i wrote his for myself before I got off last night) if (Velocity.X < 0) { for (int n = 0; n < 25; ++n) { if (Position.Y + Sprite.Height > grassTopBlock[n].Position.Y && Position.Y < grassTopBlock[n].Position.Y + grassTopBlock[n].Sprite.Height) { if (Position.X < grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width && Position.X + Sprite.Width > grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width) { Position.X = grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width; } } } } What the problem is, I can never tell if the second statement acts, because every time I try, the sprite moves to the top of the grassTopBlock. Can anyone post (or help?) with some collision algorithm for 2D sprites? thanks

    - I love D-flat! - Need. More. Code.

    C 1 Reply Last reply
    0
    • M MasterSharp

      I have this code so far on checking collision. It doesn't quite work. if (Velocity.Y > 0) { for (int n = 0; n < 25; ++n) { if (Position.Y + Sprite.Height > grassTopBlock[n].Position.Y && Position.Y < grassTopBlock[n].Position.Y + grassTopBlock[n].Sprite.Height) { if (Position.X + Sprite.Width > grassTopBlock[n].Position.X && Position.X < grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width) { Position.Y = grassTopBlock[n].Position.Y - Sprite.Height; } } } } /// FIX ME! ( i wrote his for myself before I got off last night) if (Velocity.X < 0) { for (int n = 0; n < 25; ++n) { if (Position.Y + Sprite.Height > grassTopBlock[n].Position.Y && Position.Y < grassTopBlock[n].Position.Y + grassTopBlock[n].Sprite.Height) { if (Position.X < grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width && Position.X + Sprite.Width > grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width) { Position.X = grassTopBlock[n].Position.X + grassTopBlock[n].Sprite.Width; } } } } What the problem is, I can never tell if the second statement acts, because every time I try, the sprite moves to the top of the grassTopBlock. Can anyone post (or help?) with some collision algorithm for 2D sprites? thanks

      - I love D-flat! - Need. More. Code.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      So, comment out the code that changes the y position and see if the x position code works alone.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      M 1 Reply Last reply
      0
      • C Christian Graus

        So, comment out the code that changes the y position and see if the x position code works alone.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        M Offline
        M Offline
        MasterSharp
        wrote on last edited by
        #3

        OK: never mind; I figured it out, fianlly. Thanks for helping, though. The problem was that the Y value kept checking after a certain limit was supposed to be. I added 1 line and it was fixed.

        - I love D-flat! - Need. More. Code.

        modified on Monday, March 17, 2008 7:07 PM

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups