? Collision between two sprites (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password
What's going on?? See: Site Update March 2022

Collision between two sprites

1 2 3 4
  • #1 ✎ 31 AidenCodes Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Night Person I like the quiet night and sleep late. Express Yourself I'm trying to make a pac man game, but first I need a map. I have all the sprites required for it but when the pac man sprite collides with the map sprites it goes straight through. This is all of my code for it, please don't bother with my POS code other than the collision lol X=90 Y=90 FRAME=0 ACLS LOAD"GRP4:PAC",0 PACLOOP=0 SPSET PACLOOP OUT PAC SPSET 1, 6 SPOFS 1,36,36 SPSET 2,96,16 SPOFS 2,52,36 SPSET 3,6 SPOFS 3,60,60 SPHOME 3,8,8 SPROT 3,90 SPCOL PAC,TRUE SPCOL 1,TRUE @GAMELOOP SPHOME PAC,8,8 SPOFS PAC,X,Y OLDY=Y OLDX=X IF BUTTON()==#UP THEN Y=Y-2: ROT=90:GOSUB @Pac IF BUTTON()==#DOWN THEN Y=Y+2: ROT=270:GOSUB @Pac IF BUTTON()==#LEFT THEN X=X-2: ROT =0:GOSUB @Pac IF BUTTON()==#RIGHT THEN X=X+2: ROT =180:GOSUB @Pac IF X<8 THEN X=392 IF X>392 THEN X=8 IF Y<8 THEN Y=8 IF Y>232 THEN Y=232 'COLLISION???????? IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0, OLDX, Y IF SPHITSP(PAC, 1)==-1 THEN X=OLDX ENDIF IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0,X,OLDY IF SPHITSP(PAC, 1)==-1 THEN Y=OLDY ENDIF 'NOPE DOESN'T WORK GOSUB @Blinky GOSUB @Pinky GOSUB @Inky GOSUB @Clyde IF FRAME==15 THEN FRAME=0 FRAME=FRAME+1 VSYNC GOTO @GAMELOOP @Blinky RETURN @PinkY RETURN @Inky RETURN @Clyde RETURN @Pac IF (FRAME==5 OR FRAME==10 OR FRAME==15) THEN IF PACLOOP==2 THEN SPSET PAC, PACLOOP SPHOME PAC,8,8 SPOFS PAC,X,Y SPROT PAC, ROT PACLOOP=0 ELSE SPSET PAC, PACLOOP PACLOOP=PACLOOP+1 SPHOME PAC,8,8 SPOFS PAC,X,Y SPROT PAC, ROT ENDIF ENDIF RETURN Posted
  • #2 ✎ 405 CyberYoshi64 Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Great Page Hidden Achievements Night Person I like the quiet night and sleep late. Express Yourself
    ... IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0, OLDX, Y IF SPHITSP(PAC, 1)==-1 THEN X=OLDX ENDIF IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0,X,OLDY IF SPHITSP(PAC, 1)==-1 THEN Y=OLDY ENDIF 'NOPE DOESN'T WORK
    You cannot use -1 here. SPHITSP has 2 arguments which outputs differently. You've used this: SPHITSP(Sprite,Opponent sprite) It checks whether Pac-Man collides with the opponent sprite. It returns TRUE when collision was made and FALSE if it didn't. -1 works as expected if you only use 1 argument as: SPHITSP(Sprite) Then it outputs the opponent sprite ID or -1 if it doesn't. Using 1 argument isn't often viable because if Pacman hits a cherry e.g. he'll ignore the ghosts.
    Posted
  • #3 ✎ 31 AidenCodes Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Night Person I like the quiet night and sleep late. Express Yourself
    ... IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0, OLDX, Y IF SPHITSP(PAC, 1)==-1 THEN X=OLDX ENDIF IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0,X,OLDY IF SPHITSP(PAC, 1)==-1 THEN Y=OLDY ENDIF 'NOPE DOESN'T WORK
    You cannot use -1 here. SPHITSP has 2 arguments which outputs differently. You've used this: SPHITSP(Sprite,Opponent sprite) It checks whether Pac-Man collides with the opponent sprite. It returns TRUE when collision was made and FALSE if it didn't. -1 works as expected if you only use 1 argument as: SPHITSP(Sprite) Then it outputs the opponent sprite ID or -1 if it doesn't. Using 1 argument isn't often viable because if Pacman hits a cherry e.g. he'll ignore the ghosts.
    Ok so now I've edited my code to look like this ... IF SPHITSP(PAC, 1)==TRUE THEN SPOFS PAC, OLDX, OLDY ENDIF Is this correct?
    Posted Edited by AidenCodes
  • #4 ✎ 1580 rando Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Avatar Taboo I didn't change my avatar for 180 days Website
    ... IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0, OLDX, Y IF SPHITSP(PAC, 1)==-1 THEN X=OLDX ENDIF IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0,X,OLDY IF SPHITSP(PAC, 1)==-1 THEN Y=OLDY ENDIF 'NOPE DOESN'T WORK
    You cannot use -1 here. SPHITSP has 2 arguments which outputs differently. You've used this: SPHITSP(Sprite,Opponent sprite) It checks whether Pac-Man collides with the opponent sprite. It returns TRUE when collision was made and FALSE if it didn't. -1 works as expected if you only use 1 argument as: SPHITSP(Sprite) Then it outputs the opponent sprite ID or -1 if it doesn't. Using 1 argument isn't often viable because if Pacman hits a cherry e.g. he'll ignore the ghosts.
    Ok so now I've edited my code to look like this ... IF SPHITSP(PAC, 1)==TRUE THEN SPOFS PAC, OLDX, OLDY ENDIF Is this correct?
    Well that would only collide with one sprite. There's another option though, with 3 arguments. SPHITSP(sprite, start range, end range) This basically detects collision in a defined range of sprites. I don't remember what it returns, but I think it's a true/false situation. EDIT: Nope, it returns the colliding sprite ID.
    Posted Edited by rando
  • #5 ✎ 31 AidenCodes Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Night Person I like the quiet night and sleep late. Express Yourself
    Well that would only collide with one sprite. There's another option though, with 3 arguments. SPHITSP(sprite, start range, end range) This basically detects collision in a defined range of sprites. I don't remember what it returns, but I think it's a true/false situation. EDIT: Nope, it returns the colliding sprite ID.
    Well right now I only have two sprites down, pac-man and a wall. I just want to get pac-man to stop at this wall so I can expand off of that. I tested the code I made (didn't use it before lol) and pac-man went through the wall still.
    Posted
  • #6 ✎ 1580 rando Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Avatar Taboo I didn't change my avatar for 180 days Website Well I would check whether it's actually detecting collision, by putting a beep in the IF statement or something to listen for. IF CONDITION THEN BEEP DOSTUFF ENDIF Then you could figure out from there if the problem is in the IF statement or what it's doing in response. Posted
  • #7 ✎ 31 AidenCodes Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Night Person I like the quiet night and sleep late. Express Yourself
    Well I would check whether it's actually detecting collision, by putting a beep in the IF statement or something to listen for. IF CONDITION THEN BEEP DOSTUFF ENDIF Then you could figure out from there if the problem is in the IF statement or what it's doing in response.
    Ok, I did this and the problem is that it won't detect collision. I checked with a LOCATE 0,0 PRINT SPHITSP(PAC,1) And it doesn't change when I collide with the other sprite.
    Posted
  • #8 ✎ 380 ProKuku Pokemon Is Awesome! I love Pokemon! Express Yourself Night Person I like the quiet night and sleep late. Express Yourself QSP Contest 1 Contest Participant I participated in the first SmileBASIC Source QSP Contest! I’d recommend going to chat for your problem. It would be a lot faster and you can fix your problem in a few minutes rather than a few days (usually) (sometimes chat is dead). Posted
  • #9 ✎ 31 AidenCodes Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Night Person I like the quiet night and sleep late. Express Yourself
    I’d recommend going to chat for your problem. It would be a lot faster and you can fix your problem in a few minutes rather than a few days (usually) (sometimes chat is dead).
    I've tried to check the chat a few times, and it is usually dead. If I saw any activity, I would ask there, but I never see any activity
    Posted
  • #10 ✎ 407 hakke RNG Hidden Easter Eggs QSP Contest 2 Contest Participant I participated in the second SmileBASIC Source QSP Contest! Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength what are your walls? sprites or bg? Posted
  • #11 ✎ 380 ProKuku Pokemon Is Awesome! I love Pokemon! Express Yourself Night Person I like the quiet night and sleep late. Express Yourself QSP Contest 1 Contest Participant I participated in the first SmileBASIC Source QSP Contest!
    I've tried to check the chat a few times, and it is usually dead. If I saw any activity, I would ask there, but I never see any activity
    Lol did you get past the /accept when you first enter chat? I tried talking to you earlier today on there and you never responded, and you don’t have any chat stats.
    Posted
  • #12 ✎ 31 AidenCodes Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Night Person I like the quiet night and sleep late. Express Yourself
    I've tried to check the chat a few times, and it is usually dead. If I saw any activity, I would ask there, but I never see any activity
    Lol did you get past the /accept when you first enter chat? I tried talking to you earlier today on there and you never responded, and you don’t have any chat stats.
    I did before, I was at school when you sent those. lol it didn't show /accept when I did before so I thought it was empty :|
    what are your walls? sprites or bg?
    Sprites, though I have taken the recommendation of using maps
    Posted
  • #13 ✎ 524 chicken Deep Sleep Hidden Website ok Just saw you entering and leaving chat a bunch; you still don't have any chat statistics, which means you haven't accepted chat policy yet. if you see a message like this: then read the whole thing, please. If you don't, just send /accept and you'll join chat. Posted Edited by chicken
  • #14 ✎ 31 AidenCodes Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Night Person I like the quiet night and sleep late. Express Yourself
    Just saw you entering and leaving chat a bunch; you still don't have any chat statistics, which means you haven't accepted chat policy yet. if you see a message like this: then read the whole thing, please. If you don't, just send /accept and you'll join chat.
    I sent /accept, and that was me just checking to make sure of something lol
    Posted
  • #15 ✎ 524 chicken Deep Sleep Hidden Website
    chat stuff
    I sent /accept, and that was me just checking to make sure of something lol
    Hmm, seems like our test for seeing if someone could access chat was wrong this entire time. I wonder how many newcomers actually did join chat without us knowing...
    Posted
  • #16 ✎ 31 AidenCodes Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Video Games I like to play video games! Hobbies Night Person I like the quiet night and sleep late. Express Yourself
    chat stuff
    I sent /accept, and that was me just checking to make sure of something lol
    Hmm, seems like our test for seeing if someone could access chat was wrong this entire time. I wonder how many newcomers actually did join chat without us knowing...
    ( ͡° ͜ʖ ͡°)
    Posted
  • #17 ✎ 1580 rando Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Avatar Taboo I didn't change my avatar for 180 days Website WAIT I THINK I KNOW YOUR PROBLEM!!! YOU NEED TO CALL SPCOL ON EACH SPRITE BEFORE USING SPHITSP! It's simple to use: SPCOL sprite ID Posted
  • #18 ✎ 380 ProKuku Pokemon Is Awesome! I love Pokemon! Express Yourself Night Person I like the quiet night and sleep late. Express Yourself QSP Contest 1 Contest Participant I participated in the first SmileBASIC Source QSP Contest! SPSET 1, 6 SPOFS 1,36,36 SPSET 2,96,16 SPOFS 2,52,36 SPSET 3,6 SPOFS 3,60,60 SPHOME 3,8,8 SPROT 3,90 SPCOL PAC,TRUE SPCOL 1,TRUE It helps to look through all the code lol Posted
  • #19 ✎ 407 hakke RNG Hidden Easter Eggs QSP Contest 2 Contest Participant I participated in the second SmileBASIC Source QSP Contest! Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength another question: are the walls a single big sprite or multiple sprites? Posted
  • #20 ✎ 158 Vakore Something like this tends to work: if x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 > y2 and y1 < y2 + h2 then whateverYouWantHere = true For blocks four smaller hit boxes that each do different things one for top, bottom etc. Also and additional check to make sure the player doesnt run past the hitbox, and that only the bottom of the player collides with this hitbox: if x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 > y2 and y1 + h1 < y2 + 0.1 + velY and velY > 0 then y1=y2-h1 To save memory, only check the hit boxes around the player, like this(assuming tilemap is 16 by 16 in actual space) startX = floor(playerX / 16) startY = floor(playerY / 16) endX = ceil((playerX + playerW) / 16) endY = ceil((playerY + playerH) / 16) If startX<0 then startX = 0 elseif startX> levelLengthX then startX= levelLengthX 'Same for y For j=startY to endY For i=startX to endX If levelMap[j][i] == "1" then collider i*16,j*16,16,16 Next Next Posted Edited by Vakore
1 2 3 4