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.

Dark GDK / 2d top-down collision question.

Author
Message
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 21st Jul 2010 13:13
I have a top-down game, with a character in the middle of the screen and the background moving (to make it look like the character is moving. Now i have added some walls to the world. How do i make the character stop, when he hits a wall?

I just love the smell of code in the morning...
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 22nd Jul 2010 15:09 Edited at: 22nd Jul 2010 15:10
I've made a simplified version of the code. In this example, there are two sprites, that represent a wall. And one sprite that represents the character. The character can be moved about with the WASD keys.
How do i make collision detection?



I just love the smell of code in the morning...
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 22nd Jul 2010 15:57
First thing you need in this example above is to determine which side of the sprite 1 that you've hit. Top/bottom/left/right. Once you determine where you've hit it, you can decide what to do from there. One way you can determine where you've hit is by comparing the players position to the walls position.

I hope this helps,

JTK
Mumbles
13
Years of Service
User Offline
Joined: 21st Jul 2010
Location:
Posted: 22nd Jul 2010 16:11
Would you not want to put your call to wall_collision inside your while(LoopGDK()) loop?

Something to try.

->Move wall_collision so that it is called just before move_char
->Change wall_collision return type to bool
->Change dbSpriteHit to dbSpriteCollision (SpriteHit returns the number of unique hits since the last call. If it is the same collision, it returns 0. SpriteCollision returns 1 if the sprites are currently overlapping)
->If the sprite collision is true, then return true from the function, otherwise return false
->if wall_collision() returns true, return the sprite to it's previous position (so you will have to store the previous and current positions), otherwise call move_char().
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 22nd Jul 2010 16:39 Edited at: 22nd Jul 2010 16:40
I think the best method for simple collision is to keep track of you characters old x and y positions, if you detect a collision just put your character back where it was, if there is no collision then update the old positions to the current positions as they then become the old positions in your next loop.

Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 22nd Jul 2010 17:07
I had the same idea as you matty, but i just can't figure out how i would put that into code... i'm going to keep thinking about it, but any help would be apprechiated.

I just love the smell of code in the morning...
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 22nd Jul 2010 18:01
Here is some code I posted for Keto(thread still on top page), its for 3D objects but sprites have the relevant commands also.

It may seem more complicated as I have split the collision for the two axis(x and y), you may not want to do that but it does have advantages. You should maybe just look at the X-Component first as the Y-Component incorperates jumping which you probably wont need.

Sorry I have not got time to do it all again for a top down 2D game.



MPQC
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location:
Posted: 23rd Jul 2010 00:42
Here's some code from a basic top down platformer I made, much like matty's it splits up the sections into two different axis - x & y.



Basically, it checks if the main sprite (sprite number 1), is colliding with anything. If it is, it checks it's position, then pushes it to that side. So it checks all sides of the box. Now note, this only works if the main sprite is 16x16, and the sprites it's colliding with is 32x32. Since my game is built using a tile based designer, all the sprites I use are all the same size, so this is why this engine works perfectly for mine. If all your sprites are a different size, you'll need to use someone elses code.
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 27th Jul 2010 17:49
Ok.. this is waht i did... kind of works, but i don't like it.
At firs i was checking both axis' in the same loop. So for instance, if i moved the character into the wall pressing the A button (left)
It would stop as it hits the wall, but then if i also start pressing W (up), (so i'm holding A and W at the same time), i would like for the character to move up along the wall, but it doesnt. So, i sepperated the axis each into their own loop, but it still only works for the one i put first in my code. Example, in my code, the loop that checks the X axis is first and the Y one is second. So if i move into a wall from the side, i can also slide up and down a wall if pressing the key that moves the character towards the wall and the one that moves him up or down along the wall.
But if i approach the wall from the bottom or top, the character is stuck to the wall again. How can that be fixed?

my code:



I just love the smell of code in the morning...
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 27th Jul 2010 18:03
One thing you have done different from the code I posted is that you are moving the sprite along both axis at once then checking the axis seperately. Try making two move_char functions, move_charX() and move_charY(), you then do something like this:



Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 27th Jul 2010 22:47
Thanks a load!
I'll now try to transfer this into my game code (wich is a bit more complicated) I'll post here if i have any trouble. But in the simple code this works perfectly... thank you!

I just love the smell of code in the morning...
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 29th Jul 2010 18:50
Ok there is a problem... i've been tryng to figure this out for a while, but i just can't tell what's wrong. In the actual game, i have the character made from two sprites, the body, and the legs. The body rotates and always points toward the mouse pointer. The leggs are turned in the direction in wich the character is walking and are an animated sprite. The leggs sprite is 24x24 pixels big. In my collision detection i try to detect if the leggs are coliding with any og the 4 wall sprites. It almost works... but, if you are pressing S and going into a wall south of the character, the character stops by the wall, like it's supposed to, then if i combine the S button with the D button, the character slides right along the wall (like it's supposed to) Butm if i try the combination of S and A it wont slide left along the wall. Also if bumping into a northern wall, it wont slide RIGHT and if bumping into the wall on the left it won't slide UP and so forth..
I was unable to find the problem..

Also note: In this game, the character is always in the middle of the screen and the ground, and the walls are moving under it's feet, to make it look like the character is walking around.





Obviously this isn't the whole code. But i think all the important functions are presented here.
Thank you for loking at this post, any help would be greatly apprechiated. Thank you!

I just love the smell of code in the morning...

Login to post a reply

Server time is: 2024-07-02 09:32:26
Your offset time is: 2024-07-02 09:32:26