The demonstration program I have written is based on some of
my pervious work with 2D sprites. Although the original focus in
this thread was about animation with textures on a 3D plain,
I decided to take a 2D approach. Keep in mind that the principles
applied here will work for 3D as well. It's just a matter of
changing variable types to real numbers instead of integers for
positioning the 3D object, doing away with the sprite commands and
replacing them with 3D equivalents, and making changes to some of
the image media as well as the image indexing.
I'm assuming that you want to make a animated character from 2D
images in a 3D environment here. Even if this is not the case,
the principles still do apply for animating it...
Here are some helpful hints for doing a conversion from 2D from my example to 3D:
Creating the 3D object plain and setting object transparency:
Replace SET SPRITE 1,0,1
with MAKE OBJECT PLAIN 1,10.0:SET OBJECT 1,1,0,1
Note: The object size can be changed to suit your purposes.
The size above is for examplification.
Variable usage:
All the integer variables in the code that manipulates the object
position are now defined, calculated, and referenced as real
numbers (#) for 3D. Make sure that you make the appropriate changes
in the CASE statements in the subroutine as well.
Returning current position functions:
There are two instances that use a function that returns the
current position of the sprite in the jump portion of the
subroutine. Replace SX=SPRITE X(1) with SX#=OBJECT POSITION X(1)
Note: The calculations done for the jump will have to be
refigured to 3D spacial units instead of pixels. You will have
to experiment with the values to get the desired effect. Again
change the variables to real numbers here.
Issues with the image textures and mirroring:
Unless you make several sets of images simulating each
direction, you can't mirror a 3D object's texture in real time.
Sprites have the ability to do this with the MIRROR SPRITE command.
This makes it possible to use the same set of images to simulate
two different directions the character faces. This saves you time
and memory for the animations. For 3D you will have to get the
images from a bitmap for the first set of images and then mirror
the bitmap and repeat the process. This is the problem with using
3D objects like this. You have effectively doubled that amount of
media and memory used. That is why I chose to do this demo as 2D.
For DBPro users, 2D sprites are technically 3D.
Object travel boundries:
Again, all variables for X,Y, and Z are now real numbers. Since
the object rotates and moves according to its center axis, the 3D
units will have to be figured accordingly. The IF/THEN statements
at the end of the subroutine will have to be modified to fit your
needs.
Updating object position and texture animation:
Replace SPRITE 1,X,Y,FRAME
with POSITION OBJECT 1,X#,Y#,Z# : TEXTURE OBJECT 1,FRAME
The frames used for each action as well as the action itself
will have to be determined by you in the CASE statements.
A quick note about the character controls:
The input for controlling the characters is currently setup to
poll the keys from the keyboard. I made this part of the code
separate from the actual character action for a reason. What if
you wanted to code the inputs for a multi-key combination?
(i.e. Mortal Kombat or Street Fighter style. Up, up, down, left,
and fire button.) Maybe you want to switch the controls to a
joystick or other contol device. You could make several different
inputs depending on what you want and switch them out without
repeating the CASE statements over and over again. Although the
example doesn't show this, but You could even have two players
use different inputs to control each of their characters
(say one joystick and the other keyboard). Of course you will
require the use of arrays and/or variable types (DBPro) to keep
track each player in a loop within the subroutine. The are almost
limitless possiblities for input versus action. I think this is
probably a much cleaner approach rather than a bunch of IF/THEN
statements used to get inputs and do the action. At least I think
so anyways...
I hope this assists you in your project, Laszlo!
P.S. I had problems uploading the ZIP file. I will try again, but
I did provide the example as a snippet here just in case. The
file does have media included. Hopefully I will be able to upload
it. If not, I'll try something else.
`-----------------------------------------------
`Simple Character Control Demo (Media Included!)
`
` By D Ogre
`
`You could use 3D plain objects instead.
`However, I believe sprites are more appropiate
`for a 2D style game. Experiment and have fun!
`-----------------------------------------------
`-------------
`Setup Program
`-------------
sync on
sync rate 60
set display mode 1024,768,32
hide mouse
`-------------------
`Load And Seup Media
`-------------------
`Sprite Sheets
cell=0
for pics=1 to 2
cls
load bitmap "Sheet"+str$(pics)+".png",1
set current bitmap 1
for y=0 to 7
for x=0 to 4
inc cell
get image cell,(x*192),(y*256),(x*192+191),(y*256+255)
next x
next y
next sheet
`Extra Action Image
load image "wait.bmp",71,1
`Sound Effects For Player Action
load sound "boing.wav",1
load sound "slide.wav",2
`Program Maintenance
delete bitmap 1
set current bitmap 0
set sprite 1,0,1
`----------------------------
`Initialize Program Variables
`----------------------------
cell=0
frame=31
jump=0
slide=0
flag=1
x=416
y=512
`-----------------
`Main Program Loop
`-----------------
do
cls
gosub character_controls
sync
loop
`-----------------------------
`Character Controls Subroutine
`-----------------------------
`I designed the player input controls this way because it gives
`you the freedom to customize your inputs easily. For example
`you can design it to use a mutli-button combination for each
`action. Also you could design an option menu which would
`allow the player to select a different input device and/or
`change its button layout. The keyboard input below is just for
`this demo. You can change it to whatever as long as it satisfies
`the SELECT/ENDSELECT part of the code.
character_controls:
if jump=0 and slide=0
character_action=7
if rightkey()=1 and leftkey()=0 then character_action=0
if leftkey()=1 and rightkey()=0 then character_action=1
if controlkey()=1 and rightkey()=1 and sluggish=0 then character_action=2
if controlkey()=1 and leftkey()=1 and sluggish=0 then character_action=3
if spacekey()=1 and rightkey()=1 then character_action=4
if spacekey()=1 and leftkey()=1 then character_action=5
if upkey()=1 then character_action=6
endif
select character_action
Rem Walk Right
case 0
if flag=2 then flag=1:frame=1:mirror sprite 1
inc x,4:inc frame:if frame>30 then frame=1
endcase
Rem Walk Left
case 1
if flag=1 then flag=2:frame=1:mirror sprite 1
dec x,4:inc frame:if frame>30 then frame=1
endcase
Rem Jump Right
case 2
if jump=0 then jump=1:frame=15:a=270:sx=sprite x(1):play sound 1
if flag=2 then flag=1:mirror sprite 1
x=(sx+150)+150*sin(a):y=512+125*cos(a)
dec a,10:if a<90 then jump=0
endcase
Rem Jump Left
case 3
if jump=0 then jump=1:frame=15:a=90:sx=sprite x(1):play sound 1
if flag=1 then flag=2:mirror sprite 1
x=(sx-150)+150*sin(a):y=512+125*cos(a)
inc a,10:if a>270 then jump=0
endcase
Rem Slide Right
case 4
if slide=0 then slide=1:space=0:frame=1:play sound 2
if flag=2 then flag=1:mirror sprite 1
if space<6 then inc space:inc x,24
if spacekey()=0 or rightkey()=0 then slide=0
endcase
Rem Slide Left
case 5
if slide=0 then slide=1:space=0:frame=1:play sound 2
if flag=1 then flag=2:mirror sprite 1
if space<6 then inc space:dec x,24
if spacekey()=0 or leftkey()=0 then slide=0
endcase
Rem Mouth Open
case 6
frame=71:if flag=2 then flag=1:mirror sprite 1
endcase
Rem Standing Idle (When No Keys Are Pressed)
case default
if flag=2 then flag=1:mirror sprite 1
inc frame:if frame<31 or frame>70 then frame=31
endcase
endselect
Rem Check Character Limits
if x<0 then x=0
if x>832 then x=832
if y>512 then y=512
Rem Update Character Position And Animation
sprite 1,x,y,frame
return
Unfortunately I couldn't get the ZIP to upload. I will try just
sending the sprite sheets in separate posts. You will have to change
the sound files to something else or ommit them from the code.
Sorry.