Spriting up things!

by Karthik Karanth


Sprites are my most high-priority in Just BASIC, and last time we had learnt a few commands. Let's even up some more!

    1. X, Y... and Z!

They say there are two positions in Just BASIC, X and Y. But with the art of SPRITES, we can include the Z position too!. Thus a "3D" image is accomplished.

A light is always nescessary for a 3D effect. The light is placed in a part of the object and darkens or lightens up the other directions. In Just BASIC, for a sphere with a 3D light in the center, you would use,

   WindowWidth=200                                                                                                                                                                                                                                     
   WindowHeight=200
    open "Pic" for graphics as #main
    #main "place 200 200"
    #main "down ; fill black"
    for i=200 to 50 step -2
    #main "color 0 0 "; i; " ; circle "; i
    next i
    wait

You can load this image with GetBMP and then use it as a sprite. It is recommended to "get" other scenes and use them while changing the sprite's Z position.

Merely, the Z position just uses the SpriteScale command to give the 3D illusion. I've done a version of this without using SPRITES, but this one "flickers" at every frame. Lighting is not used in this animation.

nomainwin

open "3d Graphics .::|::. Karthik Karanth" for graphics_fs_nsb as #main

#main "down ; fill black ; place 20 20 ; font ms_sans_seriff 10 bold ; color red ; backcolor black"
#main "|3d Graphics OPEN SOURCE"
#main "size 5"
#main "trapclose [exit]"
goto [loop]
wait
[redraw]
#main "place 200 80"
#main "backcolor 0 0 190"
#main "boxfilled 1150 650"
#main "place 300 180"
#main "backcolor 0 0 160"
#main "boxfilled 1050 550"
#main "line 200 80 300 180"
#main "line 200 650 300 550"
#main "line 1150 80 1050 180"
#main "line 1150 650 1050 550"
return


[loop]
for i=200 to 20 step -1
gosub [redraw]
#main "place "; (1050-300)/2+300; " "; (550-180)/2+180
#main "color red ; backcolor green"
#main "circlefilled "; i
call Pause 25
next i
for i=20 to 200
gosub [redraw]
#main "place "; (1050-300)/2+300; " "; (550-180)/2+180
#main "color red ; backcolor green"
#main "circlefilled "; i
call Pause 25
next i
call Pause 500
#main "place "; (1050-300)/2+300; " "; (550-180)/2+180
#main "font arial 22 bold"
#main "|CRASHED!!!"
wait
sub Pause mil
    t=time$("milliseconds")
    while time$("milliseconds")<t+mil
    wend
    end sub
[exit]
close #main
end

This merely activates a FOR/NEXT loop and reduces the size of the circle at every frame of the loop, while redrawing the whole image again.

To scale a sprite the "SPRITEscale sprtName sprtSize" command is used(as given above).

    2. Sprite collisions use the Z position

SpriteCollides will return only collisions between the X and Y positions, but you can modify it to use the position too! Every time you change the sprite's Z position(SCALE), make a note of in a variable the exact scale value. If you have many sprites, try using this:

SprtName.x=[x-pos]
SprtName.y=[y-pos]
SprtName.z=[scale/z-pos]

... or use an array,

Sprite(1, 1)=[x-pos]
Sprite(1, 2)=[y-pos]
Sprite(1, 3)=[scale/z-pos]

Sprite(2, 1)=[x-pos]
Sprite(2, 2)=[y-pos]
Sprite(2, 3)=[scale/z-pos]

This function returns whether a sprite has collided with another sprite or not, however, the function can sometimes return wrong answers, like you have three sprites, gd, bd, bdgd. Even if the sprite has collided with bdgd and you selected bd, it will return a "yes" value because it uses the INSTR() command to find out collisions.

To use the function, send the arguements in this order:

First Sprite Name
Second Sprite Name
First Sprite Z Position
Second Sprite Z Position
Offset

The offset is just the amount of space to be in between the sprites in the Z position. For example, if you want a ball to get the collision when it reaches a little distance of 35 in the Z position before hitting the ball, the offset would be 35.
If 3dCol() returns 0, the sprite collision is not true.

'************************************
'*              3D Collisions            *
'************************************
'*                      *
'*           Function 3dCol()        *
'************************************
'|                       |
'|                  |
'|Made by Karthik Karanth        |
'_______________________
'***********************************


Function 3dCol(sprt1$, sprt2$, sprt1, sprt2, offset)
#main "spritecollides "; sprt1$; " lst$" 'change #main to wanted handle
if instr(sprt2$, lst$)<>0 and sprt1-offset=sprt2 then 3dCol=instr(sprt2$, lst$) else 3dCol=0
end function