BBC BASIC
« Sprite Detection »

Welcome Guest. Please Login or Register.
Jan 20th, 2018, 4:22pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Sprite Detection  (Read 230 times)
michael
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 141
question Sprite Detection
« Thread started on: Aug 7th, 2017, 5:55pm »

This was a question posted by Cliveb on BBC4W forum:

Quote:
I have 16 sprites each of a different colour and non-rectangular shape. I can detect the location of each by checking x & y coordinates but this does not distinguish between visible and transparent regions of the sprite. I want to be able to detect the sprite by clicking on the visible part. Detection must not occur (as it does with x & y coords) if I click on the transparent part. I have noticed that POINT(x,y) and TINT(x,y) both return the colour underneath the sprite, not the colour of the sprite. How can I do this?



I don't see the point of this since if each sprite had a defined color, then you only need to echo the color to the program once you enter into the sprites area.


This just got me doing some research. Is there a sprite collision detection method aside from knowing the locations of the sprites?

Any ideas Richard?

« Last Edit: Aug 7th, 2017, 7:57pm by michael » User IP Logged

I like reinventing the wheel, but for now I will work on tools for D3D
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
question Re: Sprite Detection
« Reply #1 on: Aug 7th, 2017, 8:13pm »

on Aug 7th, 2017, 5:55pm, michael wrote:
This was a question posted by Cliveb on BBC4W forum: Any ideas Richard?

That's quite a tricky one. The only method I can immediately think of is to create a 2-dimensional byte array that contains a copy of the sprite's transparency mask. Then if you detect that the click is within the 'sprite rectangle' - by comparing coordinates - you can then check the array to see whether it's on a 'transparent' or 'opaque' pixel.

In pseudo-code terms it would be something like this:

Code:
      IF clickX >= spriteX IF clickX < spriteX+spriteW IF clickY >= spriteY IF clickY < spriteY+spriteH IF mask&(clickX-spriteX, clickY-spriteY) THEN
        REM Clicked on opaque region of sprite
      ENDIF 

Note the use of cascaded IFs rather than AND. This short-circuit evaluation is faster and avoids the 'subscript out of range' errors that you would otherwise get by accessing the mask array when the click is outside the sprite.

I hope this is helpful.

Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls