? SPHITSP/RC and SPCOL(VEC) tutorial ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password
Creep into the Halloween / Horror Programming Contest (Deadline Extended)

SPHITSP/RC and SPCOL(VEC) tutorial

SPCOL

Before using any of these commands, you must enable collision detection using SPCOL. SPCOL IDIf you want collision detection to work on SPSCALEd sprites, you must do SPCOL ID,TRUE 'BEFORE using SPSCALEYou can also set a collision mask: SPCOL ID,TRUE,MaskSprites 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
Author
12Me21
Updated
Rating
2 votes
Categories
Keywords
  • unfinished
  • sphitsp
  • sphitrc
  • spcol
  • spcolvec
  • sphitinfo
  • sprites
7 Comment(s) raimondz raimondz First Week Joined in the very first week of SmileBASIC Source Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Night Person I like the quiet night and sleep late. Express Yourself 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 snail_ snail_ Helper Received for being very helpful around SmileBASIC Source Achievements Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength OSP Contest 1 Contest Participant I participated in the first SmileBASIC Source OSP Contest! Why not make this a general "sprite collision" tutorial? 12Me21 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Because then I'd have to make an actual tutorial rather than just explaining how a few commands work. Lumage Lumage Head Admin Day Person I like the warm sunshine and wake up early! Express Yourself Gardening I like to garden! Hobbies Touhou Project Is Awesome! I love Touhou Project! Express Yourself This is a system guide softly, unless you intend to add full use examples, alright? randomouscrap randomouscrap Robot Hidden Easter Eggs osu! Is Awesome! I love osu! Express Yourself Drawing I like to draw! Hobbies 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 MZ952 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies This is very useful snail_ snail_ Helper Received for being very helpful around SmileBASIC Source Achievements Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength OSP Contest 1 Contest Participant I participated in the first SmileBASIC Source OSP Contest! "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