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 not working.

Author
Message
Interplanetary Funk
16
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 29th Apr 2010 23:46
I'm messing about making a 2D platformer, however, I've run into a fairly awkward snag and I cant figure it out. Basically the sprite collision(sID, tID) command isn't working when I tell it to detect if it's colliding with anything with a id above 3 and when I tell it a precise object the character sprite still falls entirely through the other sprite bar the top row of pixels. I have extends installed if that's doing anything, if so, how do I fix it? (I tried looking at the pixel perfect collision demo, but couldn't make heads or tails of it) will I have to use the SPR commands? If so how would I?

Heres my source code btw:


(note: I have tried both bmp and png format files and the same thing happens)

thanks in advance for any help =)
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 30th Apr 2010 00:08
Hi, the problem is that a sprite collision will only return a 0 or a 1. 0 meaning no collision between the specified sprites or 1 meaning there is a collision between the specified sprites.

In your code you state

the variable 'collision' is never going to greater than 3, it will only store a 0 or 1.

Try this


Hope this helps!!!

A clever person solves a problem, a wise person avoids it - Albert Einstein
Interplanetary Funk
16
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 30th Apr 2010 00:31 Edited at: 30th Apr 2010 00:31
I thought it did (as the documentation says it does
Quote: "If a target sprite has not been specified and a value of zero has been used, this command will return the sprite number of any sprite overlapping it. The parameters should be specified using integer values. "


Also, When I use

It still doesn't stop moving until the top row of pixels hits the object. I think the collision is off on the block, as when I move the character off of it DBP still thinks I'm colliding with it.

screenie:
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 30th Apr 2010 10:56
Quote: "when I move the character off of it DBP still thinks I'm colliding with it.
"

I had the same sort of problem when I wrote programs that included sprite collision. What you need to look at is pixel perfect collision.

eXtends comes with pixel perfect collision commands and I have been told that it is possible to make pixel perfect collision with memblocks but haven't done it myself, I just use eXtends.

A clever person solves a problem, a wise person avoids it - Albert Einstein
Interplanetary Funk
16
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 30th Apr 2010 13:29
I thought it did, but the example it comes with uses memblocks (which I don't understand) and i tried my hardest to get pixel perfect collision working but couldn't figure it out, would you be able to post a quick example as to how I'd use it?
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 1st May 2010 00:37
Okay I quick wrote this up. Something I noticed as I was testing my program was that a png format doesn't seem to work but a bitmap does. I don't know why.

Anyway here is the code


Unfortunately I cannot upload the images, the File uploader says that the files are over 50MB but they are only a few KB each. You should be able to insert you own media though.

A clever person solves a problem, a wise person avoids it - Albert Einstein
DVader
22
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 1st May 2010 00:44
Not used the eXtends addon but it may be the apha value of the png image possibly. Have you tried using a transparency with the png rather than a black image?

http://s6.bitefight.org/c.php?uid=103081
Interplanetary Funk
16
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 1st May 2010 01:54
I've changed my ApplyGravity() function to this:


and am now loading bmp files. But now its not detecting any collision. =(
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 1st May 2010 18:31
Quote: "the problem is that a sprite collision will only return a 0 or a 1"


Not true. In the example given:



The zero in '5,0' means if sprite number 5 collides with ANY other sprite then return the sprite number that 5 collided with.

So in this case, it could return the number of any sprite - not just 0 or 1.

Quote: "the variable 'collision' is never going to greater than 3, it will only store a 0 or 1"


0 or 1 only gets returned if you supply the function with two specific sprite numbers to check for collision with.

Does your program work as expected if you use BMP files instead of PNG's?

TDK

Interplanetary Funk
16
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 1st May 2010 18:59
No, same result no matter what image format I use.
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 2nd May 2010 00:02
Quote: ""the problem is that a sprite collision will only return a 0 or a 1"

Not true. In the example given"

Yeah, sorry about that. Its been a while since I used DBPro's sprite collision commands. I forgot about the target as 0 rule.

A clever person solves a problem, a wise person avoids it - Albert Einstein
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 2nd May 2010 02:57
Sprites are awesome but it's collision is confusing so I've only used arrays for collision. Here's an example.



Interplanetary Funk
16
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 2nd May 2010 11:22
Sorry, but I don't understand how your example works (I've never used arrays in DBPro) and I don't have a clue what the 'data' keyword really does
Visigoth
21
Years of Service
User Offline
Joined: 8th Jan 2005
Location: Bakersfield, California
Posted: 2nd May 2010 11:35 Edited at: 2nd May 2010 11:40
Sprite collision is pretty reliable. Rock solid, in fact, in my opinion.
I haven't run your code, but, something to consider. Sprites intially have an origin of 0,0. So, you may think you are colliding, but, you may in fact be missing.
It is best to use offset sprite to make the origin of the sprite at the center, instead of top left.
Just a thought.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd May 2010 16:13
Sprite collision tests when you specify a target of zero are carried out in numerical sequence and only reports the first collision it finds.

So, if you are using sprites for the background, make sure they have a higher sprite number assigned to them that the stuff you really want to collide with, and then ignore return values of 0 and those higher numbers.

Interplanetary Funk
16
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 4th May 2010 02:17
Offsetting the sprite has an effect, thank you. Mind you, I have to find the absolutely correct value for it to collide where I want it too. But again, thank you =)
jayell leedham
20
Years of Service
User Offline
Joined: 24th Jan 2006
Location: North Yorkshire
Posted: 25th Aug 2010 20:49
I have a similar (I think) problem in dbpro - or is it a different problem?. I am using the "rotate sprite 1,angle# :move sprite 1,velocity" technique, and cannot use "if point(x,y)=rgb(255,0,0)" to detect hitting a wall. Is there any alternative please?
Also, I tried to run User's code snippet, and the line "spr prepare collision red.num" was not understood by dbpro.
I have tried hiding my sprite before using point, but point again does not work.
Can you help please?
jayell leedham
Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 25th Aug 2010 23:26
There is an alternative, memblocks. Also, point doesn't work on sprites.

My computer surpasses all the technologies of the day. What computer do I have?

Login to post a reply

Server time is: 2026-07-23 07:43:05
Your offset time is: 2026-07-23 07:43:05