LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

How to make it so that the sprite's collision stops the player/player's sprite from going through the sprite from ALL sides?

Root / Programming Questions / [.]

ssunlimitedCreated:
Yesterday I have been trying to do just that for hours and hours. I gave up on doing this by myself. I tried all sorts of things and still have problems. I looked at the youtube collision and mostly looked at the command list resources and I just couldn't do it. I know it can be done; this language is powerful enough for this AND the game GAME3JUMP that came with this game has done just that.

I struggled with this a few years ago, so I l know your frustration. Here are two examples I made that should be helpful: QD4DXXMD and XK284PK6. The first one is a console "game" that uses array collision detection. The second example is closer to what you want where the player sprite can't move through blocks. Check out lines 69, 73, 77, and 81 to see how I coded for solid objects in the second example (the file name is sphitbg). Note: both of these programs came from early stages in my other projects. I hope you find this helpful.

This is how i do it although there is more than one way.
FOR I = 0 to 255 'clear old sprites
IF SPUSED(I) THEN SPCLR I
NEXT I
CLS'clear the screen
SPSET 942 OUT ROBOT 'setup sprites 
SPSET 1469 OUT WALL
SPCOL ROBOT,0,0,16,16,TRUE,255'setup collision 
SPCOL WALL,0,0,16,16,TRUE,255
X=50 ' set initial x y cordinates
Y=50
SPOFS WALL,100,100 'place a wall
WHILE EXIT == 0 ' standard while loop
 OLDX = X 'set old coordinates do this before you get new coordinates for your character
 OLDY = Y
 BT = BUTTON() ' button input to get new coordinates you can also use stick out or whatever you want
 IF BT AND 1 THEN DEC Y
 IF BT AND 2 THEN INC Y
 IF BT AND 4 THEN DEC X
 IF BT AND 8 THEN INC X
 SPOFS ROBOT,X,Y 'place your character
 IF SPHIT(ROBOT) != -1 ' check for horizontal collision
  SPOFS ROBOT,OLDX,Y ' place character at OLDX position
   IF SPHIT(ROBOT) == -1 THEN X = OLDX ' check for horizontal collision again if it is negative make X = OLDX
  ENDIF
 IF SPHIT(ROBOT) != -1 ' check for vertical collision
  SPOFS ROBOT,X,OLDY ' place character at OLDY position
   IF SPHIT(ROBOT) == -1 THEN Y = OLDY ' check for vertical collision again if it is negative make Y = OLDY
  END IF
 VSYNC' use this to slow your character down to where you can control it
WEND

Please use [code] tags

Please use code tags. I used both of these and am still having difficulties. The second answerer's answer seems good in that you store the old x and the old y and I did but it keeps storing it again when I collide. Here is my code:
IF B AND #UP Y=Y-6 PRINT SPHITSP(1) IF SP
HITSP(1)!=-1 THEN PRINT "OLDY"; OLDY Y=OLDY ELS
E PRINT "OLDY";OLDY OLDY=Y ENDIF SPSTART 0 WAI
T 8 SPSTOP 0 
When it collides, I get stuck and can't move into it or backward. I want it so that I can move back but not into the object. EDIT: walorskia, why doesn't my code work? And about your code, how does it know which button was pressed so that it knows which direction to return to when it collides?

I'm so freaking frustrated here. I have been trying to do this for countless hours, no one helps me and no one answers me!!

And about your code, how does it know which button was pressed so that it knows which direction to return to when it collides?
ssunlimited, the code walorskia posted has some checks for which button is pushed, which adds or subtracts values from X and Y. I added comments to show exactly what each line is doing, but this is their code, not mine (just to be clear).
 OLDX = X 'set old coordinates do this before you get new coordinates for your character
 OLDY = Y
 BT = BUTTON() ' button input to get new coordinates you can also use stick out or whatever you want
 IF BT AND 1 THEN DEC Y       ' if BT is UP (1),  subtract 1 from Y
 IF BT AND 2 THEN INC Y       ' if BT is DOWN (2), add 1 to Y
 IF BT AND 4 THEN DEC X       ' if BT is LEFT (4), subtract 1 from X
 IF BT AND 8 THEN INC X       ' if BT is RIGHT (8), add 1 to X
You can see BT captures button input and then masks the bits associated with DPAD directions. If you are unfamiliar with bit masking, it's worth a quick google search. In order to move your sprite back to the uncollided position, you need to store the position before it gets moved. Look back at walorskia's example above, they store X and Y in OLDX/OLDY before they INC/DEC them based on the button input. Afterwards if you detect a collision, just set the colliding axis back to the old value (X=OLDX or Y=OLDY). I'd recommend just rereading walorskia's full example from their post (it's actually quite thorough), with the goal of understanding it, not necessarily grabbing the solution. Sometimes swapping from "solving" mode to "learning" mode helps when a problem becomes too frustrating.