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 / sprite collision help! 2D stop sprite collison

Author
Message
MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 28th Jul 2011 20:25
Hey there, i have been wondering about this for some time. However, i have only worked with collision scripts in games like frogger and asteroids. I want a collision to be put around an object, so when character collides with it, the character stops.

Thanks in advance
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 28th Jul 2011 21:27 Edited at: 29th Jul 2011 01:20
Hi Matty, how's it all going.

I am sure someone will post an example. But what I can do is illustrate it.

According to your previous post, you have your character moving up, left, right and down. Thanks to Max P's help, you have your map sprites moving.

But what would happen if we disabled movement during a collision? The character would stop, thus collide.

Therefore, if the character is colliding with something, we need to stop it. How do we stop it? We use an if statement; so that if a collision exists, don't move. Infact, we also want the character to move back to the position where it did not collide, bouncing off an obstacle like in the real world.

So far you have the following:



(By the way, why do you need to rotate the sprite hero if you are already adjusting camX and camY without the [move sprite] command?)

Continuing:

We need to change



to



And so on.

Now we are checking if there hero is colliding AFTER the move. If there is a collision, move back; like bouncing off a wall.

Finally, we need to create our HeroIsColliding() to return 1 (true) when there is a collision.

This part is up to you. You can use the [sprite collision] command, you could use a line collision or pixel collision function or you could check to see if the character is inside an area using



The CPU only needs to compare each co-ordinate if the previous one is true. Just call SpriteWithin( SpriteId, 0, 0, 200, 200) for example, and it will return true if a certain sprite is in the top left of the screen.

You can use the following to check collisions with your map sprites:



Note that sprite collision( Hero, 0 ) would return any sprite colliding with it; without the need of using for loops; which is also a true (non-zero) result. You just need to determine which sprites can collide and which sprites do not.

MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 28th Jul 2011 23:12
Thanks for the help Chris,

Iv been trying to get my head around the code you have given me. im having a little trouble. i keep getting an error on the line...



The line just gets highlighted when i try to run the program. Any thoughts?
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 28th Jul 2011 23:54
@MattyBoy
That is highlighting because, unless you've written it, that function does not exist. Note what Chris said here:

Quote: "Finally, we need to create our HeroIsColliding() to return 1 (true) when there is a collision.

This part is up to you. You can use the [sprite collision] command, you could use a line collision or pixel collision function or you could check to see if the character is inside an area"


The function HeroIsColliding() needs to use the principles Chris has outlined to detect a collision. Your choice of which type of collision to use will depend on your requirements. The sprite collision command uses box collision which simply checks whether square sprites are overlapping. If you need more accurate collision detection for sprites with invisible background and irregular outlines, consider line or pixel perfect collision. There are a number of pixel perfect algorithms around and if you're looking for line collision, I provide a plugin to handle that for you.

For now, try changing



to



This simply uses the function Chris wrote for you to check for collisions between your map and your hero. Hope this helps. Please do post again if this isn't clear.

Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 29th Jul 2011 01:24 Edited at: 29th Jul 2011 01:24
Following on from Lucas, there was a square bracket at the end of the 'if HeroIsColliding() ['. It was my fault.

As stated, Lucas's LineMap plugin would be good to detect collision with non square obstacles such as trees, rivers and mountains. After you get the basic collision done, you can run a [for ... next] loop to check line collisions per mapSprite().

Happy coding

MattyBoyIsBack
16
Years of Service
User Offline
Joined: 21st Apr 2010
Location:
Posted: 30th Jul 2011 16:29
Hmm, thank you again for your help i see what you mean now. i will look into the pixel perfect collision, it seems like the best choice. however, at the moment im using Lucas' idea of changing the HeroIsColliding() with the other line of code. The program runs now at least, however, i can only move to the right...iv looked over my code and i cannot see what has gone wrong. so if anyone has any thoughts, they will be greatly appreciated.

Login to post a reply

Server time is: 2026-07-10 10:20:12
Your offset time is: 2026-07-10 10:20:12