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 Collision - Order of code causing problems

Author
Message
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 24th Jun 2011 07:14 Edited at: 28th Jun 2011 01:00
EDIT 2: I really got it working this time, unlike the last. Here's my movement and collision code for those who may need to do something similar in the future. It'll be better if you write your own code to make sure it works with your game, but hopefully mine can be used as a reference if you get stuck (no pun intended haha).




EDIT: Got it working! There were two more checks that I needed to add to each of the IF statements, and with them added there's no need for the "atr_CollisionFlag = 0" check. I've updated the pseudo code below to reflect the changes in case someone is interested in it.


Please let me know if the description of my problem isn't clear enough or you need more information; it's late and the problem is hard to describe with writing.

I'm working on some custom collision code since using SPRITE COLLISION and moving the player to the previous position when collision is detected causes there to be a gap between the wall and player. At this time my code uses four IF statements to detect when there's a collision (one for each side of a sprite); the pseudo code for it is below.





This code is working perfectly except for the fact that the player sprite only stops when I hit the bottom or top of the solid sprite (the first two IF statements). When the left side of the player sprite hits the right side of the solid sprite (and vice versa), the player sprite will jump to either the top or bottom of the solid sprite--depending on which it's closer to--and then keep moving. In essence it's detecting the collision and moving the player sprite to the nearest free location so it can keep going, instead of stopping like it should.

The reason I'm pretty sure this is an issue with the order of the code is that when I put the first two IF statements below the second two the opposite of what I described above is true. Moving L -> R or R -> L will stop the sprite, while moving T -> B or B -> T will make the sprite look for the nearest free location. I should also add that if I were to remove the "atr_CollisionFlag = 0" check in the IF statements the sprite would never fully stop; it would keep pushing it around the sides. Only the first two IF statements work properly, but it doesn't matter which two I have first.

I'm at a loss as to what may be causing this or how to fix it; any thoughts on the matter are greatly appreciated!
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 24th Jun 2011 16:05 Edited at: 24th Jun 2011 18:03
Bump
Ilidrake
15
Years of Service
User Offline
Joined: 20th Jun 2011
Location:
Posted: 24th Jun 2011 18:11
Can I see your movement code? I've been tinkering with 2d collision as well and maybe we can work this out together?
anwserman
15
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 25th Jun 2011 01:49
Hey Alaror! This is indeed a pain in the ass when it comes to 2D collisions. With the way you have your code now, you can either nail top/bottom collisions, or left/right. But not both. And it's a pain in the ass.

But I was able to figure out how to do this a while back, but you have to do a conditional IF/THEN statement to figure it out. Basically, you need to figure out how the player is approaching the block to determine what type of collision detection to do first.

You'll need:
PlayersOldPosition
PlayersTargetedPosition
BlockToBeCollidedAgainst

By using the PlayersOldPosition - determine whether or not the player is ABOVE/BELOW or LEFT/RIGHT of the BlockToBeCollidedAgainst.



So, with this image above as a guide, you can visually see that the Player is falling on-top of the BlockToBeCollidedWith. Thus, ignore the LEFT/RIGHT collision and perform a TOP/BOTTOM collision with the BlockToBeCollidedWith. Basically, if the player's old X and X+Width is within the bounds of the Block's X and X+Width, perform a TOP/BOTTOM collision on the Player's Targeted New Position.

The opposite of that is true, too. If the Player's Y value + Height is within the block's Y and Y+Height value, perform a LEFT/RIGHT collision.

ALSO REMEMBER, AND THIS IS VERY IMPORTANT: ONLY MOVE THE CHARACTER ONE DIMENSION AT A TIME. APPLY ALL OF YOUR HORIZONTAL MOVEMENTS FIRST, AND THEN APPLY ALL OF YOUR VERTICAL MOVEMENTS. OR VICE VERSA. BUT NEVER MOVE AN OBJECT IN TWO DIMENSIONS AT THE SAME TIME. I just wanted to let this point be emphasized.
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 25th Jun 2011 03:58
Ok, for clarity you could break up the conditionso of your if statments. So instead of this:


you could do this

I find the latter easier when trying to debug as it breaks up the conidtions so you can check each one individually and from your code I'm going to assume you know exactly what you are checking for. You also have an "If AND" in the first if statement but I don't think this is causing any problems.

So try breaking up your if statements and do something like printing "Pass" in each condition so using the example above:



If you only see two "pass"s being displayed then you know the problem is at 'if e < f'.

Hope that makes sense and good luck

And please don't "bump" your thread until it has atleast gone off the first page, it's not necessary to do it after one day.

A clever person solves a problem, a wise person avoids it - Albert Einstein
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 25th Jun 2011 22:51
Thank you all for your responses!


@Ilidrake

The following comes before the collision code, though it's not working properly right now because I've been messing with the collision code




@anwserman

One of the checks I do in each IF statement is where the player sprite is in relation to the solid sprite (the second one of each IF statement; some variation of "var_DistanceY > 0"). The third and fourth checks (some variation of "var_Distance_LandR < 0") are there to see if the player's x and x+width is within the bounds of the solid object. I've added the player's direction as an additional check recently which has solved some problems but caused others as well.

What conditions did you specify for your IF statement?


@Hodgey

That would probably make it easier to debug but I'm not having any issues doing that. The problem is that I don't know what conditions I should check for in my IF statements.

Sorry about bumping too soon as well.
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 26th Jun 2011 04:25
Quote: "The problem is that I don't know what conditions I should check for in my IF statements."

I see. Here is a small program that demonstrates what I think you want. It is mainly the concept of zones to check so don't worry about how the circle moves. In order for you to understand it you will probably have to sit down with pen and paper to work out why I check for the bounds that I do check for. I have tried to comment it but they aren't the best explanations. Good luck



Quote: "Sorry about bumping too soon as well."

Don't worry about it, I know how eager people can be to get an answer, especially when it is a show stopper problem like this.

Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 26th Jun 2011 05:34
I have a bigger problem on my hands now... I opened a new project to try out your code but instead of just using an untitled one it erased any trace of my game's code. I tried uploading a past file that I had saved on a jump drive but all it's doing is pulling up your code...

Am I going to have to start from step 1 again with my game?
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 26th Jun 2011 05:47
don't double click on the .dba or .dbpro file, try opening it from your IDE's "open file" dialog.


Why does blue text appear every time you are near?
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 26th Jun 2011 05:50
If you mean right click > open on the file I did that and it's pulling up the other code.
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 26th Jun 2011 07:56
Quote: "I opened a new project to try out your code but instead of just using an untitled one it erased any trace of my game's code."

Oh no, that's not good. Which IDE are you using? I've heard that the old one (grey background) has had problems like this but the new one seemed pretty stable. Follow Neuro's advice and open the .dbas from within the ide. Just check all of your dbas that may possible contain your code, you might get lucky.

Quote: "I tried uploading a past file that I had saved on a jump drive but all it's doing is pulling up your code"

Maybe the .dbpro file has changed and your source code is still existent just not being loaded into the project. If you are using the new ide check your solution explorer to see what source code files are being included into your project and compare this to what is actually in the project folder. If there's a spare .dba file, it could be the one.

Good luck, I really hope you recover your code. If worse comes to worse, could you roll back to a previous windows checkpoint with out losing too much and regain your code that way?

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 26th Jun 2011 08:40
Quote: "If you mean right click > open on the file I did that and it's pulling up the other code. "

no, I mean the opposite of that. Going into your IDE and selecting the file tab, click "open file", which will open up a file browser dialog, and find the source code file you want to open from there.

With the default IDEs, sometimes if you double click on a file, it doesn't open that file, it just opens the IDE. So double clicking on a .dba file is exactly the same as double clicking on the IDE's icon, and it opens whatever default file it feels like opening.


Why does blue text appear every time you are near?
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 26th Jun 2011 16:12 Edited at: 26th Jun 2011 16:12
Thanks for trying to help.

I've been opening the files from within the IDE, and have tried every single one. The file that has the code in it says it was last modified yesterday at 12:47 AM (hours before this happened last night), so it must all be there. I found that it saved an "untitled" file in AppData\Temp, and when I looked at it the file was modified yesterday at 10 PM. I deleted this file (since it's where the new code was stored), but now when I try to open my code the program tells me that it "Could not find main include file, the path stored in the include file is invalid or missing. You will have to add the file manually."

How would I go about doing this? There's no information I could find with a search. Also, rolling back is certainly an option if there's nothing else that can be done.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 26th Jun 2011 18:38
You can open up the project file (.dbpro) in Notepad and edit it so that it points to the right file with your code (.dba). Look for "main=".

When you open the .dbpro file it looks like this:


Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 26th Jun 2011 20:01
I saved my code in a .dbpro file, not a .dba file. Does it automatically create a .dba file for each project?
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 26th Jun 2011 22:46
Quote: "I saved my code in a .dbpro file, not a .dba file. Does it automatically create a .dba file for each project? "

code isn't saved in .dbpro files. The project settings (name, starting resolution, location of source code files) are stored in a .dbpro file. The actual code is stored as plaintext in a .dba file.


Why does blue text appear every time you are near?
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 26th Jun 2011 23:33 Edited at: 26th Jun 2011 23:41
Well I guess that's lost then. The new code probably replaced my code as "untitled" since I didn't save my code as a .dba file.

Life happens haha. Thankfully I found a .dba file from a while back which has a lot of the structure, though big pieces are missing. Better get to rewriting it!
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 27th Jun 2011 00:33
Quote: "Life happens haha. Thankfully I found a .dba file from a while back which has a lot of the structure, though big pieces are missing. Better get to rewriting it! "

Well, atleast you have something to work with and don't have to write if from scratch. I have found that if I re-write my programs I do a far better job the second time around.

So after all of that, did my code snippet do what you needed it to do besides erasing your code?

anwserman
15
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 27th Jun 2011 06:00
@Alaror,
I don't remember what I exactly did with my source code, as it is somewhere but Ihaven't looked at it in two years. Plus it was in XNA and C#. :3

Anyway, sorry to hear about your lost code. But on the bright side, typically after deleting code or losing it, you can easily reprogram it. I mean, it still sucks but you're not treading new water again
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 27th Jun 2011 06:39
Quote: "since I didn't save my code as a .dba file."

just saying, if you didn't save your code as a .dba file, you didn't save your code! I wouldn't think all is lost, you might wanna just go through searching for any and all .dba files that may contain relevant code.


Why does blue text appear every time you are near?
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 28th Jun 2011 00:39 Edited at: 28th Jun 2011 00:46
@Hodgey
Glad to say I've gotten most things working again, and my collision code is working much better this time around than before. Everything seems to be working perfectly except detecting when the player isn't standing on a block (if not the gravity flag should be activated).

EDIT: Just got that sorted out. My collision code is now working perfectly Thank you to all those who took time to help!


@anwserman

No problem, thanks for the help you've provided already. Also, you're right; it didn't take nearly as long to get things working the second time around as the first.


@Neuro Fuzzy

It's something I know now, but the save system doesn't seem to be intuitive. There are two save types, so you can choose between saving your settings (media, etc) or code, but not both at the same time (not reliably at least). If this hadn't happened I would have never guessed it was the case.

Login to post a reply

Server time is: 2026-07-11 12:03:10
Your offset time is: 2026-07-11 12:03:10