I was going to add
Set Hardcol and
=Hardcol() to the bug list as this has cropped up a couple of times now. So I had a delve into the code and found the problem is that the AMOS Manual is way off track! It's not a bug at all.
So forget what the manual says, the correct syntax, parameters and usage are:
Set Hardcol EnableBits, MatchBitsEnableBits is a bitmap of which bitplanes you want checked.
MatchBits is a bitmap of what value to check for in each bitplane (%0 or %1). That may sound a little strange but it just means you can check for either a set bit (%1) which is like checking for hitting a wall, or a cleared bit (%0) which is like checking for hitting a hole.
EnableBits corresponds to bits 6 to 11 in CLXCON,
MatchBits corresponds to bits 0 to 5 in CLXCON.
Set Hardcol always sets bits 12 to 15 in CLXCON to %1111. This just means that the odd numbered hardware sprites will also be included in the checks. So effectively all hardware sprites are always included.
The problems start when you try to check the results!
Result=Hardcol(SpriteNumber)SpriteNumber is the number of the hardware sprite you want to check.
Result is only useful if you're checking hardware sprite to hardware sprite collision when it will be -1 for a "hit". For checking bitplane collisions, it will be zero even if you hit a bitplane pixel. You then need to use:
Result=Col(SpriteNumber)SpriteNumber is the number of the sprite to check for in sprite-to-sprite collisions. BUT -1 will check for bitplane collisions.
Result would be the hardware sprite number that you hit in a sprite-to-sprite check. Any non-zero value will be a bitplane hit if you specified -1 for
SpriteNumber.
I haven't investigated whether there's any special meaning to the values returned when a bitplane collision has occurred. I thought I'd get this and an example program posted a.s.a.p. to avoid any further confusion. I suspect the value would indicate odd or even bitplane hits and maybe even bitplane-to-bitplane hits (useful for dual playfield). I need to investigate a bit further....
This code (file also attached) is a quick demo of a sprite that collides with a blue box in the background but not with a pink one. You need the collisions sprite bank from the Tutorials directory if you're just copying the code. If you download the attachment, the bank's embedded in the program.
'==================================================
' Demo of Hardware Sprite Collision with Bitplane
'
' Author : bruceuncle
' Date : 04/03/2014
'==================================================
'
' This code just positions the screen at the same
' hardware coords as your Default screen:
'
G_ADDR=Screen Base
G_XPOS=Deek(G_ADDR+82)
G_YPOS=Deek(G_ADDR+84) and $FF
For S=0 To 7
Trap Screen Close S
Next S
Screen Open 0,320,200,8,Lowres
Screen Display 0,G_XPOS,G_YPOS,320,2000
'
' The embedded sprite bank is the one in Tutorials used
' in the Collision Detection example program:
'
Get Sprite Palette : Colour 0,0 : Curs Off : Flash Off : Cls 0 : Hide On
'
' Ink 2 means we'll be drawing in bitplane 1 (cyan colour).
' This is the bitplane we want to detect collisions in.
'
Ink 2 : Box 80,80 To 239,119
'
' Ink 1 means we'll be drawing in bitplane 0 (sorta pink colour).
' This is a bitplane we're not interested in.
'
Ink 1 : Box 120,20 To 199,175
Pen 2 : Paper 0
Locate 0,23 : Centre "Press Right Mouse Key to Exit"
X=160 : Y=40 : DX=2 : DY=2
Sprite 1,X Hard(X),Y Hard(Y),2
'
' The sprite must be masked!
'
Make Mask 1
'
' Turn on hardware collision detection.
' 1st param is which bitplanes to turn on collision detection for.
' Corresponds to bits 6 to 11 in the CLXCON hardware register.
' Always set unused bitplanes to %0 in the bitmap.
' 2nd param is what value in the bitplane to check against (%0 or %1)
' Corresponds to bits 0 to 5 in the CLXCON hardware register.
' Unused bitplanes have no effect in the bitmap.
' AMOS always sets bits 12 to 15 to %1 in the CLXCON hardware register.
' That means the odd numbered sprites will also be included in the checks.
' So, effectively, all hardware sprites are always checked in AMOS.
'
' For this example we're looking for a collision with bitplane 1 so
' we will get a "hit" on the blue box but not on the pink one.
' AMOS always trims leading zeros. The value we're really loading
' is %000010, which is a bitmap of all 6 possible bitplanes. As our screen
' is only 8 colours, we're only using the first three bitplanes.
'
Set Hardcol %10,%10
'
Do
Exit If Mouse Key=2
Add X,DX : Add Y,DY
If X>294 or X<26 Then DX=-DX
If Y>174 or Y<26 Then DY=-DY
Sprite 1,X Hard(X),Y Hard(Y),2
'
' After a hit, we delay for four frames to give the sprite a chance
' to get clear:
'
If G_WAIT>0
Dec G_WAIT
Else
'
' Check sprite 1 for a hardware collision:
'
C=Hardcol(1)
'
' =Col(-1) means check for bitplane collisions (which bitplanes
' will be the ones we set up using Set Hardcol.
' 0 is returned for no collision, non-zero is a collision.
' I haven't investigated what the actual returned values mean yet!
'
If Col(-1)
'
' If we got a hit, I'm just jiggling the direction and sounding
' a ding. The G_WAIT is just to delay the next checks until
' the sprite has moved away from the box a bit. Helps prevent
' "head banging"!
'
Bell : DX=-DX : Add X,DX : G_WAIT=4
End If
End If
Wait Vbl
Loop
'
Screen Close 0
Edit
Remember, its bitplanes that you're checking collisions with, not colour numbers. So checking set bits in bitplane zero will register a hit for colours 1 (%00000
1), 3 (%00001
1), 5 (%00010
1), etc. So you need to choose the colours in your backgrounds carefully.