10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

Author Topic: Pixel perfect collision by Mysoft.  (Read 26233 times)

notthecheatr

  • Global Moderator
  • Forum Sage
  • *****
  • Posts: 351
  • Who's the guy from 21 Jump Street?
    • AOL Instant Messenger - notthecheatr
    • Yahoo Instant Messenger - TheMysteriousStrangerFromMars
    • View Profile
    • notthecheatr Home
    • Email
Re: Pixel perfect collision by Mysoft.
« Reply #15 on: August 22, 2008, 01:57:03 PM »
Using OpenGL Lachie?  *gasp*

Change your will to make sure you're buried with a parka.  Hell may finally have frozen over.
The funniest thing happened yesterday.

BadMrBox

  • Forum Sage
  • *****
  • Posts: 411
    • View Profile
    • BadMrBox.com
Re: Pixel perfect collision by Mysoft.
« Reply #16 on: August 24, 2008, 08:34:08 AM »
Before Lachies new computer, 'OpenGL sucks'.
After Lachies new computer, 'OpenGL rules'.
 :D

Lachie Dazdarian

  • Double dipper
  • Administrator
  • Forum Sage
  • *****
  • Posts: 1308
    • Yahoo Instant Messenger - lachie13
    • View Profile
    • The Maker Of Stuff
    • Email
Re: Pixel perfect collision by Mysoft.
« Reply #17 on: August 24, 2008, 12:35:09 PM »
All lies.
"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist

notthecheatr

  • Global Moderator
  • Forum Sage
  • *****
  • Posts: 351
  • Who's the guy from 21 Jump Street?
    • AOL Instant Messenger - notthecheatr
    • Yahoo Instant Messenger - TheMysteriousStrangerFromMars
    • View Profile
    • notthecheatr Home
    • Email
Re: Pixel perfect collision by Mysoft.
« Reply #18 on: August 27, 2008, 01:46:57 PM »
Search your feelings Lachie... you know it to be true.
The funniest thing happened yesterday.

Zamaster

  • Amoeba
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Pixel perfect collision by Mysoft.
« Reply #19 on: January 18, 2009, 10:33:35 AM »
Hey, I wrote a pixel perfect collision routine optimized for 32 bit using MMX back at the FB forums but nobody noticed : /. It goes through two pixels at a time so it should be at least a little faster than the one presented in this topic.

Code: [Select]

Function PPCollision(img1 As zpIMAGE, ax1 As Integer, ay1 As Integer ,_
                     img2 As zpIMAGE, bx1 As Integer, by1 As Integer , tcol As Integer) As Integer
    Dim As Integer ax2, ay2, bx2, by2
    Dim As Integer x1 , y1 , x2 , y2
    Dim As Integer ix1, iy1, ix2, iy2, sx, sy, WX1, WX2, C
    Dim As Uinteger Ptr sp1, sp2
    WX1 = zpGET_WIDTH(img1)
    WX2 = zpGET_WIDTH(img2)
    ax2 = ax1 + WX1
    ay2 = ay1 + zpGET_HEIGHT(img1)
    bx2 = bx1 + WX2
    by2 = by1 + zpGET_HEIGHT(img2)
    If ax2 <= bx1 Then Return 0
    If bx2 <= ax1 Then Return 0
    If ay2 <= by1 Then Return 0
    If by2 <= ay1 Then Return 0
    If ax1 < bx1 Then
        x1 = bx1
        If bx2 < ax2 Then
            x2 = bx2
        Else
            x2 = ax2
        Endif
        sx  = x2 - x1
        ix1 = WX1 - sx
        ix2 = 0
    Else
        x1 = ax1
        If ax2 < bx2 Then
            x2 = ax2
        Else
            x2 = bx2
        Endif
        sx  = x2 - x1
        ix2 = WX2 - sx
        ix1 = 0
    Endif
    If ay1 < by1 Then
        y1 = by1
        If by2 < ay2 Then
            y2 = by2
        Else
            y2 = ay2
        Endif
        sy  = y2 - y1
        iy1 = zpGET_HEIGHT(img1) - sy
        iy2 = 0
    Else
        y1 = ay1
        If ay2 < by2 Then
            y2 = ay2
        Else
            y2 = by2
        Endif
        sy  = y2 - y1
        iy2 = zpGET_HEIGHT(img2) - sy
        iy1 = 0
    Endif
    sp1 = @img1[(iy1*WX1+ix1)]
    sp2 = @img2[(iy2*WX2+ix2)]
    WX1 Shl= 2: WX2 Shl= 2
    C = 0
    Asm
        mov eax, [sp1]
        mov ebx, [sp2]
        add eax, zpIMAGE_HEADER
        add ebx, zpIMAGE_HEADER
      
        mov  ecx, [tcol]
        movd mm2, ecx

        mov ecx, [sx]
        mov esi, ecx
        Shl esi, 2
        Sub [WX1], esi
        Sub [WX2], esi
        mov esi, ecx
        mov edx, [sy]
      
        Y_Loop:
      
        X_Loop:
            movd    mm0, [eax]
            movd    mm1, [ebx]
            pcmpeqw mm0, mm2
            pcmpeqw mm1, mm2
            por     mm0, mm1
            movd    edi, mm0
          
            Or  edi, edi
            jnz SkipIt
            mov eax, 1
            mov [C], eax
            jmp EarlyOut
            SkipIt:
          
            add eax, 4
            add ebx, 4
          
            dec ecx
        jnz X_Loop
          
        mov ecx, esi
      
        add eax, [WX1]
        add ebx, [WX2]
    
        dec edx
        jnz Y_Loop
      
        EarlyOut:
        emms
    End Asm
    Return C
End Function
 
« Last Edit: January 18, 2009, 10:35:45 AM by Zamaster »

Dr_D

  • Forum Sage
  • *****
  • Posts: 257
    • Yahoo Instant Messenger - dr_davenstein
    • View Profile
    • Dr_D's apps
    • Email
Re: Pixel perfect collision by Mysoft.
« Reply #20 on: March 09, 2009, 03:17:18 PM »
Whoa Zamaster... You're right. I guess nobody did notice. I sure did notice now though! Good job! :)

EDIT: What is a zpImage?
« Last Edit: March 09, 2009, 03:19:05 PM by Dr_D »

Dr_D

  • Forum Sage
  • *****
  • Posts: 257
    • Yahoo Instant Messenger - dr_davenstein
    • View Profile
    • Dr_D's apps
    • Email
Re: Pixel perfect collision by Mysoft.
« Reply #21 on: October 29, 2009, 08:10:58 PM »
Hi. This doesn't use the winapi, but it does plot a pixel on the desktop with a small amount of code.

Code: [Select]
randomize timer
#include "fbgfx.bi"

screenres 640,480,32,,FB.GFX_NO_FRAME or FB.GFX_SHAPED_WINDOW

line(0,0)-(640,480),&hff00ff,bf

for x as integer = 0 to 5000
    pset( rnd*640, rnd*480 ), rnd*4294967295
next

sleep

creek23

  • Amoeba
  • *
  • Posts: 4
  • KonsolScript Developer
    • View Profile
    • KonsolScript
Re: Pixel perfect collision by Mysoft.
« Reply #22 on: November 05, 2010, 09:34:59 PM »
I know this post is kind of old but...

Anyone knows how to get this working with PNGLOAD? Because I loaded two PNG files with transparent backgrounds into ANY PTRs, and tried to collide those sprites. I get box collision. :(

...I'm also having the same problem.

Probably the alpha value is different - if the collision check assumes alpha is &HFFFF00FF, then if PNG Load is setting alpha = &H00FF00FF, then it won't work. ;)

with &HFFFF00FF --> is 00 the green one? is it RGBA or ARGB? does it mean that mysoft's collision only works to a single specific value of transparent color? what is it's originally red (#FF0000) but transparent?

Well, I found a way to work around it. I just need to paste the sprites with ALPHA on a magic pink rectangle, and grab them again with GET.

I'm in OPENGL window so this is basically happening off screen (it just needs proper memory buffers).

could you please post a snippet.  :'(
When somebody tells you this same line, "proprietary software created jobs", tell them "so did marijuana".
-------------------------------------------------------
KonsolScript | Programming Language designed for Games Development

darkhog

  • Recruit
  • **
  • Posts: 21
    • View Profile
Re: Pixel perfect collision by Mysoft.
« Reply #23 on: October 06, 2011, 09:35:23 AM »
Please update code so it will be cross-compatible. Now when using it on Linux you'll get access violation and your game will end. The best solution would be rewrite whole so no assembly is used. I know it isn't easy task for pixel-perfect collision (otherwise I'd do this myself) but bounding box is easier. In fact I can start routine for bounding box collision.

Code: [Select]
Function DidCollide( x1 as integer, y1 as integer, w1 as integer, h1 as integer, img1 as any ptr = 0, x2 as integer, y2 as integer, w2 as integer, h2 as integer, img2 as any ptr = 0, mode as integer = 0) as integer
'Checks collision between two sprites or two rectangles on screen
'If optional img<number> parameters are added which are image buffers, width and height are calculated automatically using imageinfo function
'Mode is mode of collision. 0= bounding box (default), 1 = pixel perfect.
'Call = DidCollide(x1,y1,w1,h1[, img1], x2,y2,w2,h2[,img2][,mode])
'Return 1 if collided, 0 if didn't, -1 on error
If mode = 0 then
'Bounding box
if img1<>0 then
'if img1 points on something
dim res as integer
res = imageinfo (img1, w1, h1)
if res = 1 then
'if image wasn't successfully readed
return -1 'we exit function with error
end if
end if
if img2<>0 then
'if img2 points on something
dim res as integer
res = imageinfo (img2, w2, h2)
if res = 1 then
'if image wasn't successfully readed
return -1 'we exit function with error
end if
end if
'Now we get to actual collision checking
If x1+w1>w2 Then return 1
If x1<x2 Then return 1
If y1+h1>h2 Then return 1
If y1<y2 Then return 1
'At this point we checked every possibility so we know it didn't collide
return 0
'...so we just exit function and show that nothing happened.
else
'Pixel-perfect
if img1=0 and img2=0 then
'At least one image must be specified if we want pixel-perfect collision
'so if it isn't
return -1 'we exit function with error
end if
'for now rest of this part is dummy as I don't know how to make it
end if
end function

Code compiles well, but need testing as I don't have time to do it (there may be some logic errors)
« Last Edit: October 06, 2011, 10:15:29 AM by darkhog »

mysoft

  • Recruit
  • **
  • Posts: 49
    • MSN Messenger - mysoft@bol.com.br
    • View Profile
    • MyTDT Software
    • Email
Re: Pixel perfect collision by Mysoft.
« Reply #24 on: October 06, 2011, 11:44:47 AM »
Please update code so it will be cross-compatible. Now when using it on Linux you'll get access violation and your game will end. The best solution would be rewrite whole so no assembly is used. I know it isn't easy task for pixel-perfect collision (otherwise I'd do this myself) but bounding box is easier. In fact I can start routine for bounding box collision.

can you provide an example of the code that crashed?
because i tried it on several linux and it worked flawlessy (not even valgrind complained) , but i heard some linux have problem with fbgfx... (the example i provided on the link, fails to work too?)

so more details like fb version.... and how you're using it... is required to track this problem...
Programming is like love... you will never acomplish anything by treating your language as if it was a tool, or a slave of yours...

darkhog

  • Recruit
  • **
  • Posts: 21
    • View Profile
Re: Pixel perfect collision by Mysoft.
« Reply #25 on: October 06, 2011, 03:04:52 PM »
This example failed to work: http://lachie.phatcode.net/fbscroll.php . After initial "blahblahblah, press any key" screen it crashed with access violation. You'll have to compile it yourself though as it comes only with win32 binaries. BTW. Lachie may contact you about this if he didn't already.

fbc - version output:
Code: [Select]
FreeBASIC Compiler - Version 0.23.0 (08-07-2011) for linux (target:linux)
Copyright (C) 2004-2011 The FreeBASIC development team.
Configured with prefix /usr/local
objinfo enabled using FB BFD header version 217

Didn't try your example (but seen many third party ones crashing with same result), but I will and tell results. Probably will be same.

//edit: Sample in OP ( http://lachie.phatcode.net/mysoft_collision.zip ) works, but still this is no-no for me as I use png_load and don't know if my images are load as ARGB or RGBA and since your code assuming one of those (forgot which one), I can't use it.
« Last Edit: October 06, 2011, 03:11:51 PM by darkhog »

Lachie Dazdarian

  • Double dipper
  • Administrator
  • Forum Sage
  • *****
  • Posts: 1308
    • Yahoo Instant Messenger - lachie13
    • View Profile
    • The Maker Of Stuff
    • Email
Re: Pixel perfect collision by Mysoft.
« Reply #26 on: October 06, 2011, 11:06:20 PM »
I do think that an working example with PNGs would help many people. I'll give it a go darhkog and see what needs to be done. I remember I had problems with PNGs in OpenGL and had to "hack" my way through it (mentioned in this thread). But got it working.

So it seems that my FB Scrolling code has some problems in Linux. Would you be so kind darkhog to remove all the calls to pixel perfect collision in my code and see if it still crashes? I would like to pin point the culprit.
"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist

darkhog

  • Recruit
  • **
  • Posts: 21
    • View Profile
Re: Pixel perfect collision by Mysoft.
« Reply #27 on: October 07, 2011, 01:58:06 AM »
Since mysoft's demo compiled and ran fine on my computer, chances are that something else is a blocker. And OpenGL isn't that important for 2d games as for 3D (unless you using shaders, but shaders can be emulated with graphics with alpha channel and speed is not an issue since such classics like Jazz Jackrabbit 2 didn't use OGL acceleration and were pretty intense games and now our processors are several times faster than those from '98) so FBGfx + png_load do fine for me.

But I think that blocker, if not Mysoft's (btw. I'm making games under name of MyCrossSoft, strange coincidence, huh? http://mycrosssoft.net ) is some sort of windows-specific call, e.g. since it's access violation related to memory (memory protection violation to be exact) I bet pointer programming is cause since somewhere some pointer points to area of memory which is forbidden on Linux.

Lachie Dazdarian

  • Double dipper
  • Administrator
  • Forum Sage
  • *****
  • Posts: 1308
    • Yahoo Instant Messenger - lachie13
    • View Profile
    • The Maker Of Stuff
    • Email
Re: Pixel perfect collision by Mysoft.
« Reply #28 on: October 07, 2011, 02:03:35 AM »
Yeah, someone pin-pointed the problem in FB.net forum. Just rename MAP.MAP in the source to map.map.

Linux is case-sensitive with filenames, and I often forget about it when programming in Windows.

Also, that custom font printing routine demo doesn't work because I use variable "st" there. Rename it to "stt".

So basically, all works! I'll update my download links for future Linux users asap.

Cheers!

BTW, there is a 2D programming wrapper for OpenGL in FB. It's called GL2D. And I think it has its own pixel perfect collision. You have links in the tutorial I sent you.
"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist