Presentation and help with sprite on/off

By bradstallion

Supporter (5)

bradstallion's picture

25-01-2022, 10:31

Hi All,
I've started my computer journey with a MSX Toshiba HX-10 in 1984, when I was 9. A story that, I suppose, is very similar to your own Smile
Emulation and retrocomputing have been always part of my hobbies.

I've recently discovered the great book "Modern MSX-BASIC Game Development" written by shalafi, I've bought it and I'm having a lot of fun!

Anyway, I've a problem playing with sprites. In chapter 5 ("collision detection") it explains the sprite off/on trick around the put sprites to avoid collision between layers of a multicolor sprite. It's pretty clear, but I have problems with it.

I've stripped the code to the minimum here:

https://msxpen.com/codes/-MuFTHZah2XJ1cui-KiL

lines 1000-1040 load data, then the mail loop is between lines 140-620. If you run it, after a few frames a collision is detected and ON SPRITE GOSUB is fired.

What's my error?
Thanks a lot!

Login or register to post comments

By ARTRAG

Enlighted (6832)

ARTRAG's picture

25-01-2022, 11:47

It is long time since I've coded in Basic so mine is just a guess
Try adding a delay between line 570 and 580
Yes, add
575 for i=0 to 10:next
to make it work

By bradstallion

Supporter (5)

bradstallion's picture

25-01-2022, 11:49

Thank a lot @ARTRAG, this seems to work:

https://msxpen.com/codes/-MuFplUexb1x8XUeZxZM

I've added a 1 frame delay. Is this idiomatic? @shalafi's book doesn't add it.

Probably I'm still missing something...

Thanks again!

By aoineko

Champion (408)

aoineko's picture

25-01-2022, 11:51

I almost never code in Basic, but according to MRC documentation, SPRITE ON|OFF instruction seem to be asynchronous. If it's the case (detection not occur during your loop but during the interruption handler I guess), there is great chance that SPRITE is ON at the moment of check.
What you are trying to do looks like the OR-ed sprite management for the MSX2 screen modes.
That said, it's possible that Basic does magic that I don't understand ^^

By ARTRAG

Enlighted (6832)

ARTRAG's picture

25-01-2022, 11:56

The problem rises when the vdp frame ends between the two put sprite instructions.
The VDP, at the end of the frame, reports a collision (that in the meanwhile, the second put sprite has removed) and the basic triggers the interrupt because in the meanwhile sprites on has been executed.

By bradstallion

Supporter (5)

bradstallion's picture

25-01-2022, 13:25

Thanks a lot for the explanation. So waiting for a frame (a change to TIME) seems to be the correct choice, if I understand correctly.

By Latok

msx guru (3866)

Latok's picture

25-01-2022, 13:36

If you have multiple sprites in your game, then the ON SPRITE GOSUB also triggers a collission between enemy sprites, right? Im making my own maze game in BASIC atm and I choose to keep track of the location of all the sprites and as soon as an enemy sprite is at the same spot as the player sprite, there is a collission which is relevant for the game....

By ARTRAG

Enlighted (6832)

ARTRAG's picture

25-01-2022, 13:43

Yes. It is. In addition, if you put the put sprite commands just after a VDP frame has ended (and if your code is fast enough, i.e. all the parameters are already computed and stored in variables), the sprites will be set in VRAM before the raster has reached them. This implies that collisions will be computed on the coordinates of the current iteration of your game loop and you can avoid the SPRITE ON/SPRITE OFF trick.

https://msxpen.com/codes/-MuGFFMX5_X1KuZc4tXd

Sadly the interpreter is slow and can introduce delays so you cannot be always sure of its timings

By bradstallion

Supporter (5)

bradstallion's picture

25-01-2022, 14:04

This is a script from the book github page:

https://github.com/plattysoft/Modern-MSX-BASIC-Game-Dev/blob...

It doesn't seem to be affected from the collision problem: https://msxpen.com/codes/-MuGKP_8zGndBJRIC3cc

I'll try to undestand the differences...

By ARTRAG

Enlighted (6832)

ARTRAG's picture

25-01-2022, 14:36

The difference has to be in the the timing and in the duration of the code that sets the sprites
Anyway, note that this line
780 IF TIME<2 GOTO 780 ELSE TIME=0
waits for frame ending

By bradstallion

Supporter (5)

bradstallion's picture

25-01-2022, 15:28

yes, I've noticed. But I had the same problem with it. Instead, waiting *before* SPRITE ON, as you suggested, works for me.
Strange...
Thanks!