LoginLogin

SPHITSP/RC and SPCOL(VEC) tutorial

Root / Submissions / [.]

12Me21Created:

SPCOL

Before using any of these commands, you must enable collision detection using SPCOL.
SPCOL ID
If you want collision detection to work on SPSCALEd sprites, you must do
SPCOL ID,TRUE 'BEFORE using SPSCALE
You can also set a collision mask:
SPCOL ID,TRUE,Mask
Sprites will only collide if their masks have some of the same bits set. &B100 will collide with &B111 but not &B001.

SPHITSP

SPHITSP checks for collision between sprites. Check collision between one sprite and any other sprite:
CollisionID = SPHITSP( ID )
Check collision between two specific sprites:
CollisionID = SPHITSP( ID1, ID2 )
Check collision between one sprite and a range of other sprites:
CollisionID = SPHITSP( ID, LowID, HighID )

SPHITRC

SPHITRC checks if a sprite is touching a rectangular area.
CollisionID=SPHITRC(X,Y,Width,Height)

CollisionID=SPHITRC(ID ,X,Y,Width,Height)

CollisionID=SPHITRC(LowID,HighID ,X,Y,Width,Height)
You can also specify the mask and SPCOLVEC information for the rectangle:
CollisionID=SPHITRC([...,] X,Y,Width,Height,Mask[,VX,VY])

SPHITINFO

You can use SPHITINFO to get more information about the last collision.
SPHITINFO OUT Time[,X1,Y1[,VX1,VY1],X2,Y2[,VX2,VY2]]
Time is a real number between 0 and 1 and I have no idea what it does After calling SPHITSP/RC normally, you can call SPHITSP() or SPHITRC() again with no arguments. This will give you information about other collisions (if there are any) or just return -1.

SPCOLVEC

it's pretty cool

All the "vector" functions exist so that you can perform collision detection BEFORE moving the sprite. For instance, let's say you're walking forward and you hit a wall. Usually, you stop characters from hitting the wall by seeing if the place you're going to be in is IN the wall, then not updating the position if so. Let's say you want to use sprite collision detection to detect the hit wall. You'd want to use SPOFS to move the character sprite into the new position and use SPHITSP* or SPHITRC** to detect the hit, but unfortunately, the behavior of SPOFS isn't consistent when used with SPHITSP and SPHITRC, so this isn't reliable. With SPCOLVEC, you can say "this is how fast the sprite is currently travelling per frame." Now, SmileBASIC will detect hits wherever the sprite WILL BE on this frame rather than where it currently is. So, hitting a wall becomes as easy as setting the player velocity with SPCOLVEC, then performing the regular sprite hit detection. If all that is too much, think of SPCOLVEC as telling SmileBASIC to look into the future for hit detection rather than the present. * = sprite hitting sprite detection ** = sprite hitting rectangle detection

This is a system guide softly, unless you intend to add full use examples, alright?

Why not make this a general "sprite collision" tutorial?

Replying to:snail_
Why not make this a general "sprite collision" tutorial?
Because then I'd have to make an actual tutorial rather than just explaining how a few commands work.

Something to note about SPHITSP/SPHITRC They always return the first sprite colliding with the sprite/rectangle but if you call SPHITSP()/SPHITRC() then it will return the next sprite that is collliding. Example:
 'GET THE FIRST SPRITE THAT IS COLLIDING WITH 0.
VAR SPHT=SPHITSP(0)
WHILE 1
 IF SPHT==-1 THEN BREAK
 'HERE GOES THE CODE TO HANDLE COLLISSION.

 'GET THE NEXT SPRITE THAT IS COLLIDING WITH 0.
 SPHT=SPHITSP()
WEND

Replying to:haloopdy
All the "vector" functions exist so that you can perform collision detection BEFORE moving the sprite. For instance, let's say you're walking forward and you hit a wall. Usually, you stop characters from hitting the wall by seeing if the place you're going to be in is IN the wall, then not updating the position if so. Let's say you want to use sprite collision detection to detect the hit wall. You'd want to use SPOFS to move the character sprite into the new position and use SPHITSP* or SPHITRC** to detect the hit, but unfortunately, the behavior of SPOFS isn't consistent when used with SPHITSP and SPHITRC, so this isn't reliable. With SPCOLVEC, you can say "this is how fast the sprite is currently travelling per frame." Now, SmileBASIC will detect hits wherever the sprite WILL BE on this frame rather than where it currently is. So, hitting a wall becomes as easy as setting the player velocity with SPCOLVEC, then performing the regular sprite hit detection. If all that is too much, think of SPCOLVEC as telling SmileBASIC to look into the future for hit detection rather than the present. * = sprite hitting sprite detection ** = sprite hitting rectangle detection
This is very useful

Replying to:haloopdy
All the "vector" functions exist so that you can perform collision detection BEFORE moving the sprite. For instance, let's say you're walking forward and you hit a wall. Usually, you stop characters from hitting the wall by seeing if the place you're going to be in is IN the wall, then not updating the position if so. Let's say you want to use sprite collision detection to detect the hit wall. You'd want to use SPOFS to move the character sprite into the new position and use SPHITSP* or SPHITRC** to detect the hit, but unfortunately, the behavior of SPOFS isn't consistent when used with SPHITSP and SPHITRC, so this isn't reliable. With SPCOLVEC, you can say "this is how fast the sprite is currently travelling per frame." Now, SmileBASIC will detect hits wherever the sprite WILL BE on this frame rather than where it currently is. So, hitting a wall becomes as easy as setting the player velocity with SPCOLVEC, then performing the regular sprite hit detection. If all that is too much, think of SPCOLVEC as telling SmileBASIC to look into the future for hit detection rather than the present. * = sprite hitting sprite detection ** = sprite hitting rectangle detection
"You'd have to move the character sprite into the new position to check, but SB only performs SPOFS after a vsync! So, you'd have to move the character sprite into the wall, wait a frame, detect the hit, then move him back, resulting in complex code and a visibly vibrating player." Edit this because it's not entirely right, this is only necessarily true with SPHITRC. SPHITSP doesn't obey this weird behavior of having to wait for the next frame. idk why SPHITRC has to be so trashy though