12 Replies - 4087 Views - Last Post: 29 September 2011 - 07:33 AM Rate Topic: -----

#1 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

SDL collision detection problem

Posted 28 September 2011 - 09:59 AM

I thoroughly enjoy a good GBA-style RPG and I've decided to make one. Maybe not so visually pleasing, but operating in a similar manner with the same sort of feel I suppose.

Everything is going incredibly well and I'm mostly done with all of the drawing, but I'm having some issues with the physical aspect of my game. My collision detection is Boolean and for some strange reason always returns false, even when (to the best of my knowledge) it shouldn't.
Spoiler


That's the very basic code for my collision detection that I'm using as a placeholder until I have the time to find a more efficient way to check collision.

The next significant snippet is where I set the physical interaction layer (PIL) of my tiles. PIL[225] is an SDL_Rect defined above. This is most likely where the problem is occuring.
Spoiler


I have class called Sprite and an object of that class called Hero that has its own rect. When I call collision() with PIL for some reason it never returns true. Flabbergasted.

Spoiler


EDIT: Fixed my first problem and it came up with another one, please re-read.

I've attatched the *.cpp file just incase I've left out any significant code.
~If you cbf downloading it, let me know what you want to see and I'll post it here.

This post has been edited by Gungnir: 28 September 2011 - 10:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: SDL collision detection problem

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: SDL collision detection problem

Posted 28 September 2011 - 10:55 AM

Revised my collision detection function.

Spoiler


I'm still having the same issue... It always returns false. I'm going to go have a shower and when I come back I'll put a pre-defined rect straight into my collision() function and see if that works. I know it will, but for the purposes of debugging I have to rule everything out. Bbl.
Was This Post Helpful? 0
  • +
  • -

#3 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 78
  • View blog
  • Posts: 255
  • Joined: 22-April 11

Re: SDL collision detection problem

Posted 28 September 2011 - 11:04 AM

Are you using an IDE where you can easily debug this? Have you considered setting some breakpoints and following the data to make sure that your PIL is being set up properly? I'll look through the code a bit more but that's usually a good place to start.
Was This Post Helpful? 1
  • +
  • -

#4 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: SDL collision detection problem

Posted 28 September 2011 - 11:36 AM

Thank you for your reply, RevTora!

I'm using bloodshed Dev-C++ v 4.9.9.2 and the code doesn't return any compiling errors. The problem that I'm having is purely logical. The program thinks that it's doing the right thing because I've made an error in logic somewhere and it's not performing to my expectations.

I'm almost certain that __set__PIL__() is where the error is and I'll look into it now by substituting values that I know will work in its place.

Thanks for your feedback, it's greatly appreciated!

--------------------------------------------------------------------------------------------

Oh god... I'm a complete idiot. I've found the error in my logic before even opening Dev-C++. It seems so obvious now!

PIL doesn't ACTUALLY move, and neither does Hero, and since they aren't initially colliding of course they never collide! All I have to do is set my physical layer to move with my tiles.

Thanks anyway, D.I.C! But apparently I'm the only 'D.I.C head' here :dontgetit:

This post has been edited by Gungnir: 28 September 2011 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#5 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 78
  • View blog
  • Posts: 255
  • Joined: 22-April 11

Re: SDL collision detection problem

Posted 28 September 2011 - 11:52 AM

Hehe, glad you figured it out... I hate when I do that. I would strongly suggest Visual C++ Express 2010 instead of DevC++. VC++ has a nice debugger which would've made it 100x easier to find logic mistakes :)
Was This Post Helpful? 0
  • +
  • -

#6 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: SDL collision detection problem

Posted 28 September 2011 - 11:58 AM

Cheers. Okay so now my physical layer is moving with my image layer. Collision is definitely occurring, problem is... It doesn't halt my character's movement.

I'm looking through it now, I've definitely missed something. I've put a predefined rect into it and it still doesn't work. I might work on it further and wait to see if anyone else replies on here with anything that can point me towards an answer!
Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: SDL collision detection problem

Posted 28 September 2011 - 12:38 PM

:gun_bandana: Aaarrgh! Dev-C++

Please install this lovely free IDE :)
Was This Post Helpful? 1
  • +
  • -

#8 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 78
  • View blog
  • Posts: 255
  • Joined: 22-April 11

Re: SDL collision detection problem

Posted 28 September 2011 - 01:22 PM

Yeah I really really suggest using a different IDE. Even Qt Creator which is my new favorite IDE (linux ftw) would be better simply because it has a built in debugging interface so you can easily figure out where things are going wrong. Knowing how to debug is almost as important as learning how to program believe it or not :\
Was This Post Helpful? 0
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SDL collision detection problem

Posted 28 September 2011 - 04:16 PM

View PostRevTorA, on 28 September 2011 - 09:22 PM, said:

... Knowing how to debug is almost as important as learning how to program believe it or not :\

Developing software is about:

1. Creating code.
2. Understanding your code.
3. Making your code maintainable.

Miss out any one of these your code at some point will break and eventually have to be rewritten, so being able to debug is as important as being able to code. Anyone who says otherwise has never been near a large code base.
Was This Post Helpful? 0
  • +
  • -

#10 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 78
  • View blog
  • Posts: 255
  • Joined: 22-April 11

Re: SDL collision detection problem

Posted 28 September 2011 - 06:17 PM

View PostButchDean, on 28 September 2011 - 04:16 PM, said:

View PostRevTorA, on 28 September 2011 - 09:22 PM, said:

... Knowing how to debug is almost as important as learning how to program believe it or not :\

Developing software is about:

1. Creating code.
2. Understanding your code.
3. Making your code maintainable.

Miss out any one of these your code at some point will break and eventually have to be rewritten, so being able to debug is as important as being able to code. Anyone who says otherwise has never been near a large code base.


Agreed... I'm confused were you agreeing with me or disagreeing? :\
Was This Post Helpful? 0
  • +
  • -

#11 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SDL collision detection problem

Posted 29 September 2011 - 05:21 AM

I was disagreeing with you because you said debugging is almost as important when it is as important as writing code itself. If/when you you get into the real world of programming you will see that being a programmer isn't just writing code, it also includes debugging and maintaining code which are tasks that many new programmers try to avoid - hence the reason they end up with code they can't understand nor fix. :)
Was This Post Helpful? 0
  • +
  • -

#12 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: SDL collision detection problem

Posted 29 September 2011 - 06:19 AM

Debugging is the pinnacle of understanding code. Any kid can leech tutorials and not learn a thing, but to debug code you have to understand it.

Okay so I'm still not getting my two objects to collide. I've created a predefined SDL_Rect called Debug.
        xMap -= Hero.xVel;
        yMap -= Hero.yVel;
        
        Debug.x = (((-8*WIDTH)+Hero.x)+xMap);
        Debug.y = (((-4*HEIGHT)+Hero.y)+yMap);
        Debug.w = WIDTH;
        Debug.h = HEIGHT;

        if(collision(Hero.rect, Debug) == true)
        {                        
            xMap += Hero.xVel;
            yMap += Hero.yVel;
        }


"(-#*WIDTH)+Hero.x" is the offset (position on the map) of the PIL. xMap moves the PIL. The rects *SHOULD* be colliding and stopping the movement of the tiles under the Sprite. If that makes any sense.

It's still not working >:

Dev-C++ returns occasional glitches. But I enjoy that it only debugs syntax. The IDE is a glorified Gedit with a compiler slapped on. I've tried a few different IDEs and I prefer Dev-C++. It's all C++ anyway, Dev-C++ just gives me a little extra freedom. It uses Visual Studios SDL linkers anyway so not much is different there. There's none of that StdAfx.h stuff either.

...Idk, I just prefer it.
Was This Post Helpful? 0
  • +
  • -

#13 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: SDL collision detection problem

Posted 29 September 2011 - 07:33 AM

You can set up an empty project and link to SDL, you don't need to do any Win32 programming. That's the real beauty of SDL, it can be easily ported across platforms by simple linking with the correct version for each OS. So no excuse not to use visual studio due to StdAfx.h or anything like that. <back on topic>

Post some more code please :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1