Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / 2D Sprite Collision for Open World RPG

Author
Message
Hurryingcheese
14
Years of Service
User Offline
Joined: 3rd Jul 2012
Location:
Posted: 2nd Dec 2012 17:41
Hey guys, I hate to bring up 2D collision, as it is mentioned pretty much everywhere, but I have a problem with all of the other examples I have found online so far. In my game, I am trying to detect when my character is hitting any other sprite (such as the side of a building, a tree, etc.). I have managed to get the detection down (despite my code being primitive to say the least), but I can't figure out how to tell the sprite that, "That house is above you, you are touching the bottom of it with your head, therefore you can go left, right, down, but not up until you stop touching that house." Any help is MUCH appreciated. Thank you in advance to anybody who can.

Here's my code:


-Live to code-
Rudolpho
20
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 2nd Dec 2012 18:25
One simple solution would be to have something like this in your player type:


Then when detecting for example that you have pixels above you you would set player.availableDirections.up = false (and reset all directions that don't have any obstacles blocking them to true). You would then only allow the player to move up if player.availableDirections.up == true and so on.


"Why do programmers get Halloween and Christmas mixed up?"
Hurryingcheese
14
Years of Service
User Offline
Joined: 3rd Jul 2012
Location:
Posted: 3rd Dec 2012 03:24
Thank you Rudolpho for the solution. However, me being new to DBPro, I have just a few questions. First, I've never used user defined data such as types before; therefore I'm a bit confused as to where I place the code you supplied me because I have no player type like you suggested I should have. Second, How could I detect pixels in a way that the program knows what direction they are relative to the player's position? If you can help me to understand these points, even if you just direct me to some informational article(s) somewhere, I will be eternally grateful. Thanks again.

-Live to code-
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 3rd Dec 2012 23:10 Edited at: 3rd Dec 2012 23:11
You can put the user defined types at the start of the program.

No time to go into detail; perhaps Rudolpho or someone else will help you out with you collision question. Ultimately you can deduct all possible character states, then determine what the character can do; something similar to:


Something like that, there are many ways; this is just off the top of my head.

More tutorials here

Hurryingcheese
14
Years of Service
User Offline
Joined: 3rd Jul 2012
Location:
Posted: 4th Dec 2012 01:44
Thank you both for taking the time to help! I will definitely try out both of your techniques. On the matter of learning about user defined types, I will study up on those using the DB Pro user guide as soon as possible; therefore you don't have to elaborate on them if you don't feel the need to Rudolpho. This game is remarkably important to me, therefore I'm exultant to see such effective and accessible assistance available on this forum. Thanks again guys.

-Live to code-
Hurryingcheese
14
Years of Service
User Offline
Joined: 3rd Jul 2012
Location:
Posted: 4th Dec 2012 02:56
Actually, I believe I just found a stupidly simple solution to figuring out which direction that the sprite the player is colliding with is relative to the player! Check it out:


The only problem is that the only direction you can move when you're hitting a sprite, is the direction opposite of the sprite you're hitting relative to the player. In other words, if I collide with a house that is on my left, I can only move right, not up or down. I know why this happens and have just not fixed it because it seems a little unnecessary to. :p If one of you guys would like to improve upon my code in order to debug this little problem, I would appreciate it, but as I said earlier, it is not necessary. Thanks again for all of the great assistance! See you around the forum!

-Live to code-
Ramon156
14
Years of Service
User Offline
Joined: 13th Jul 2011
Location: Netherlands
Posted: 4th Dec 2012 15:52 Edited at: 4th Dec 2012 15:54


A small example, the only downside is you cannot use the save array command if you'd like to save and load the UDT array. You'll have to do this another way.
Hurryingcheese
14
Years of Service
User Offline
Joined: 3rd Jul 2012
Location:
Posted: 4th Dec 2012 22:48
Thank you Ramon156, that makes perfect sense to me. I think I just needed to see an example and yours is perfect. I'll definitely try to incorporate it in my game. Also, to everybody who has put up suggestions of any kind on this board, I will add your forum names to the credits of the game as a final thank you.

-Live to code-

Login to post a reply

Server time is: 2026-07-07 09:28:28
Your offset time is: 2026-07-07 09:28:28