BBC BASIC
« Sprite Detection »

Welcome Guest. Please Login or Register.
Mar 31st, 2018, 10:43pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Cross-platform BBC BASIC (Windows, Linux x86, Mac OS-X, Android, iOS, Raspberry Pi)
BBC BASIC Resources
BBC BASIC Help Documentation
BBC BASIC for Windows Home Page
BBC BASIC Programmers' Reference
BBC BASIC Beginners' Tutorial
BBC BASIC for SDL 2.0 Home Page
BBC BASIC Discussion Group

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


member is offline

Avatar




PM


Posts: 157
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: 803
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 »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls