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.

Newcomers DBPro Corner / Help with complete platform collision

Author
Message
gameangel147
11
Years of Service
User Offline
Joined: 2nd Dec 2012
Location:
Posted: 16th Apr 2013 03:34
I have been trying to perfect platform collision, but haven't had much luck. I have it so that the player can on and off a platform and even fall off the edge, but the collision is only for the top of the platform, and the trouble is getting it to collide right on the left, right and bottom of the platform.
I have looked around the forums, and found a few other ways, like checking the color of the pixels around the player, though it was unclear as to how to do this exactly. The other way I found was to check for collision before the sprite is moved and short-circuit the command, like the example below, but I can't get it to work for all 3 sides at once.

Example:


If anyone has anyway to do this collision better, it would be greatly appreciated. And thanks in advance.


Full code:
Derek Darkly
12
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 17th Apr 2013 20:21 Edited at: 17th Apr 2013 20:22
The way I'm doing sprite collisions with my current project is by pasting a solid-colored, identically shaped sprite underneath my main (target) sprite.

Then I just tell my program to register a hit anytime the current coordinates of a bullet (for example) return that special color using the POINT command.

After checking for hits, the main sprite is of course then pasted to cover up the collision sprite.

You can also (alternately or combined) perform a distance check to determine a desired radius of collision.

D-Zone 2d Scrolling Shooter

D.D.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 18th Apr 2013 07:26
gameangel147:

I tried running your code and it would not run because the display mode was set to 1000,1000,32, which is not a viable mode on my computer. When I set it to 1024,768,32, it ran, but you had placed the window at 400,0, so it was largely off the screen. Anyway, after adjusting a few things, I got it to run, but I wonder if it wouldn't be better for you to use 2D tiles to make up a map and have your character sprite interact with the tile map?

If you are dead-set on using sprites, you could use a variable to hold the player's current direction (perhaps =1 is up and =2 is down). Then when you collide with a platform sprite, you can react accordingly.

Derek Darkly:
The POINT command is really slow. Using a MEMBLOCK would be a faster/better choice, but it is a more advanced technique that requires a little more understanding. You can find out about memblocks on the forum here if you search.

So many games to code.....so little time.
gameangel147
11
Years of Service
User Offline
Joined: 2nd Dec 2012
Location:
Posted: 20th Apr 2013 04:43
Derek Darkly:

I see what you are saying, so that it looks like it collides with the sprite but it actually looks for the color behind it, but it doesn't really help me. I don't need it to just detect collision, but rather where on the sprite it is colliding, so that the player lands on top or bounces off the side or bottom.

LBFN:

Sorry about the window settings, I was playing around and trying to see more of the screen. The reason I am not using tiles is because I am not sure how to do that, I just started playing around with sprites so this is where I am, and I think I would have the same problem. If I have tiles like so:

000000000000000000
000000000222200000
000000000222200000
000000000000000000
000000000000000000
111111111111111111

where the 1's are the ground and 2's are a platform, how would I be able to detect which part of the platform it is hitting? I can detect one side, but not all at once, it just contradicts itself. And could you further explain using variables to hold a player direction?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 20th Apr 2013 22:08
Hi gameangel147,

2D tile collision uses math rather than sprite collision to determine if a sprite has entered a particular space. Here are some basic ideas for you to consider:
1. Decide how large you want the individual tiles to be. You show 18 wide X 6 high. Your tiles would be rather odd shaped with a map like that. In the code example, the tiles are 48 X 48, but you can easily change this by altering the TileSizeX and TileSizeY variables.
2. Make the graphics. They need to be the size you set or multiples of that size (i.e. assuming 48 X 48, you could have a large background item that is 144 X 96, which would take six tiles to display). You can load the images individually, as you have done, or you can place them onto one bitmap and then pull the images off within the code.
3. Dimension an array that will hold all of the screen data. The example dimensions a 50 X 50 array, of which approximately 21 X 16 is actually displayed at one time, so you have roughly 7 screens worth of data.
4. The player's movement is allowed based upon where the sprite x and y coordinates are. You simply divide by the x/y amount per tile to determine where the sprite will be on the map.
5. This example was not coded using a direction for the player. It could be handled differently, but this works fine.

This is an overhead movement example made by pizzaman/purple pickle years ago. I altered it a little bit. The code is commented pretty well and it seems to work fine. Take a look at how things were done and it should help you.





So many games to code.....so little time.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Apr 2013 01:24
I'd suggest storing the tile data into a separate file and loading it.

"You're all wrong. You're all idiots." ~Fluffy Rabbit
gameangel147
11
Years of Service
User Offline
Joined: 2nd Dec 2012
Location:
Posted: 23rd Apr 2013 02:42
LBFN:
It is going to take me a while to get a good grip on all that code, but thanks for the example. Though in the declare variables subroutine, you write "if SCREENSIZEX mod TileSizeX then TilesPerScreenX = TilesPerScreenX + 1." What does "mod" mean, since it doesn't seem to be a command, so is it short for something?

Phaelax:
I actually haven't loaded outside files, besides images, into a program yet. I guessing that it still has to be in correct syntax right?


Also, this question is for anyone, regarding screen position, is there any way to see beyond the coordinates 0, 0? The top left corner is always 0, 0 and when you make the screen bigger, it extends the right and bottom parts of the window, so is there any way to look at the negative parts of space?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 23rd Apr 2013 06:40 Edited at: 23rd Apr 2013 06:41
MOD is short for modulo. The wiki definition explains it pretty well:


basically, it is the remainder that is left over when you divide two integer numbers.

Regarding the question about seeing negative numbers, you can indeed place sprites in negative space, but the only part that is actually drawn is that part that is on the screen. This way you could slowly move something in from the left or top of the screen. Not sure where you are going with this.

So many games to code.....so little time.
gameangel147
11
Years of Service
User Offline
Joined: 2nd Dec 2012
Location:
Posted: 23rd Apr 2013 07:41
Yeah I remember that, so if there is a remainder when the two variables are divided, then add 1 to the variable.

And I know you can paste sprites in negative space, but is there a way to view that space, so that, for example, (0, 0) is at the center of the screen rather than in the top-left corner?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 23rd Apr 2013 22:13
The screen coordinates are always going to be positive numbers. I don't know why you would want to have part of the screen as negative space, as that would only complicate things. If you wanted some space above your playing field/map/level/etc., why not just locate the graphics a little further down in the first place?

If you want to view 'negative space', you could always make a 3D game. 3D objects can be placed at negative coordinates.

So many games to code.....so little time.
gameangel147
11
Years of Service
User Offline
Joined: 2nd Dec 2012
Location:
Posted: 25th Apr 2013 23:43
It was more of me just wondering if it could be done, but you have given me some good ideas, thanks again for the help.
gameangel147
11
Years of Service
User Offline
Joined: 2nd Dec 2012
Location:
Posted: 26th Apr 2013 10:36
Actually I have another question, or a few more. I have sprites that are randomly drawn along the x-axis, but some overlap each other, so how can I get them to not overlap of be a certain distance away from each other, while keeping them random?

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 26th Apr 2013 18:29
I thought I could quickly throw something together to demonstrate how you can do this, but it was a little trickier than I thought. Anyway, take a look at this code and see if it works for you. I basically am looking at the positions of the other sprites (which you have stored in an array) and checked to make sure the new position does not cause for an overlap. I would suggest writing your createPlatforms subroutine as a function (note: I changed the name also, as it seems more descriptive). This way, you can place additional sprites more easily. I created the sprites initially so that the code does not error when going through the function the first time.



You'll notice I changed the random placing position and the movement speed. This was only done so I could see the effects.

So many games to code.....so little time.
gameangel147
11
Years of Service
User Offline
Joined: 2nd Dec 2012
Location:
Posted: 9th May 2013 21:55
LBFN:

Well I got a bit busy with other things, but I've finally understood the code and got it to work right(thanks a lot for that). Though I am wondering what the difference is between subroutines and functions. Or more specifically when I should use one over the other.

Also, I am trying to get a score to show up, though since I have a sprite as a scrolling background, so the standard text can't be seen. How else can I print a score over sprites?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 9th May 2013 22:52
There are a couple of main differences between subroutines and functions. With subroutines, the variables are not considered local, so if you change a variable's value within a subroutine, it will remain the same value outside of the subroutine. The variables within a function default as local variables unless declared as global. Here are some quick examples:

This uses a subroutine to change the value of the variable MyVar.

If you write the subroutine ChangeTheValue as a function, it will return 101 every time. This is because I did not declare MyVar as global. Here is the example:



With this one, when you press the ENTER key, it will flash the value of MyVar in the function, while the MyVar value in the main loop remains unchanged. Using the same code and declaring MyVar as a global variable, makes it show the value in the main program.



One nice thing about functions is that they can return a value. We could get away with not declaring MyVar as global if we did it this way:


You'll notice that I purposely changed MyVar to DifferentVar in the function to show you that it is because of the way the function call is setup in the main loop that assigns the value, not because the variable has the same name.


Here is another quick example of function usage.



It is up to you whether you choose to use a function or a subroutine.
Quote: "
Also, I am trying to get a score to show up, though since I have a sprite as a scrolling background, so the standard text can't be seen. How else can I print a score over sprites? "


Try putting the DRAW TO FRONT command early in your code.

Hope this helps.

So many games to code.....so little time.
gameangel147
11
Years of Service
User Offline
Joined: 2nd Dec 2012
Location:
Posted: 26th May 2013 12:36
LBFN:

Well the DRAW TO FRONT command didn't work, but I did find another way to post the score, or sort of.

http://forum.thegamecreators.com/?m=forum_view&t=190556&b=1

I just increase a variable by 1, though I am trying to figure out how to slow it down, so that it counts up slower. I have tried wait and sleep but they slow down the whole program.

Also when I go to the properties tab and try to change the version number, it becomes bold, but if I open the program again then it goes back to V1.0. Nearly everything else such as the name and details stay changed except the version number. I save everything, and this is in the DBPro file, but it doesn't stay changed. If anyone knows whats going on it will be a big help.

Thanks again for all the assistance!
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 26th May 2013 22:51 Edited at: 26th May 2013 22:53
I tried placing the command DRAW SPRITES FIRST early in the code and it works.

Quote: "I just increase a variable by 1, though I am trying to figure out how to slow it down, so that it counts up slower. I have tried wait and sleep but they slow down the whole program."


I'm not sure what variable you are talking about here. Could you post a snippet of code? There are a couple of ways to make it count slower. You could use a variable to control a timer to have a certain amount of time expire before the count is incremented.

Quote: "Also when I go to the properties tab and try to change the version number, it becomes bold, but if I open the program again then it goes back to V1.0. Nearly everything else such as the name and details stay changed except the version number. I save everything, and this is in the DBPro file, but it doesn't stay changed. If anyone knows whats going on it will be a big help.
"


Not sure which IDE you are using; I think the CodeSurge IDE will do this. If it is a problem, you could use a different IDE. Typically this is for a game that you are developing for someone or you have a game that has a thread on the WIP board. You could always list the version in the filename. i.e. save it as "GameAngel_1_05.dbpro"

So many games to code.....so little time.

Login to post a reply

Server time is: 2024-04-19 19:15:00
Your offset time is: 2024-04-19 19:15:00