[Android Apps and Games] Caramelslice:
I have the code for a working prototype in DBpro

as follows..

`PRESS SPACE TO:

`on LHS of screen, draw objects in dword space.

`on RHS, draw them with positions relative to "player" position

`check that player position loops around right.

sync on

sync rate 60:skipsyncmode=0:set display mode desktop width(),desktop height(),32,1 `STANDARD MODE

`sync rate 30:skipsyncmode=1:set display mode 848,480,32,1 `FOR YOUTUBE VID CAPTURE

autocam off

hide mouse

backdrop on:color backdrop 0

set camera aspect 1.66667

dim thing_posx(100) as dword

dim thing_posz(100) as dword

dim thing_color(100) as dword

for t=1 to 100

thing_posx(t)=rnd(255)<<24 `256=2^8 , 8+24=32

thing_posz(t)=rnd(255)<<24

`thing_color(t)=0xFF000000 OR rgb(rnd(255),rnd(255),rnd(255))

thing_color(t)=rgb(rnd(255),rnd(255),rnd(255))

next t

thing_color(0)=0xFFFFFF00 `make sure "player" is visible!

`now add terrain array (ground heights)

dim map_height(1023,1023) `whole map is 1024 square

dim map_tile(1023,1023) as byte `only need 4 values

for a=0 to 1023

for b=0 to 1023

`map_height(a,b)=rnd(255) `used rnd(255) so can easily get colour out if want to draw heightmap

map_height(a,b)=0.3* (rnd(50) +100*cos(50*a) +50*cos(40*a +50*b))

map_tile(a,b)=1+rnd(3) `tiles are numbered from 1-4

map_tile(a,b)=1+rnd(2) `no red

next b

next a

`make a square "launchpad", virus style.

for a=0 to 7:for b=0 to 7: map_tile(a,b)=2: next b:next a

for a=1 to 7:for b=1 to 7: map_height(a,b)=100.0: next b:next a

mat_divs=13 `13 squares - means that after cropping with box, 12x12 will be visible

mat_square_size=1<<22 `2^32 / 2^10, since whole world is dword (2^32) and divided into 1024x1024 squares.

subtract_factor=6*mat_square_size `(matrix size - mat square size)/2 =`6*mat square size

`subtract factor is taken from player position in order to find where from map array to take beginning corner of matrix from.

mat_view_size#=1000.0

conversion_factor#=mat_view_size#/(mat_square_size*12) `to convert from dword positions to float for positioning in DBP

`"real world" and rendering

mat_actual_size#=mat_view_size#*(13.0/12.0)

make matrix 1,mat_actual_size#,mat_actual_size#,13,13

`position matrix centrally.

position matrix 1,-0.5*mat_actual_size#,0.0,-0.5*mat_actual_size#

`load image "test2.bmp",1:prepare matrix texture 1,1,1,1

draw_mat_tex() `for no media

`load image "test3a.bmp",1

prepare matrix texture 1,1,2,2 `2 by 2 mat tex.

`set matrix trim 1,0.01,0.01 `prevent tex bleeding due to filtering

set matrix texture 1,2,0 `no mipmap. stops adjacent matrix tiles (on tile map) bleeding into eachother

`downside is that lines look ugly

set matrix 1,0,1,0,0,0,0,0

`position camera above it so can see it. will be repositioning camera every frame anyway.

`position camera 0,1000,0:point camera 0,0,0

`fill mat with info to check it's viewable

for a=0 to 13

for b=0 to 13

set matrix height 1,a,b,map_height(a,b)

next b

next a

update matrix 1

`box object to disguise edges moving

make object box 100,mat_view_size#,-10.0*mat_view_size#,mat_view_size#

`single_pixel_image(100,50,0,0,200)

single_pixel_image(100,0,0,0,255)

texture object 100,100

set object 100,1,3,1,0,0,0,0

`player (red ball on ground)

make object sphere 1,20

single_pixel_image(2,255,0,0,255)

texture object 1,2

`make some other objects positioned near to the player.

single_pixel_image(101,100,100,100,255)

for t=1 to 20

thing_posx(t)=rnd(10*mat_square_size) + 10*mat_square_size

thing_posz(t)=rnd(10*mat_square_size)

`make object box 50+t,10,100,10

make object sphere 50+t,100

offset limb 50+t,0,0,50,0

texture object 50+t,101

next t

`make a skyscraper

t=20

thing_posx(t)=15*mat_square_size

thing_posz(t)=12*mat_square_size

delete object 50+t

wide#=4.0*mat_square_size*conversion_factor#

make object box 50+t,wide#,500,wide#

offset limb 50+t,0,0,200,0

texture object 50+t,101

`make a "virus" style ship object!

make object box 200,50,20,80

`ADD HELICOPTER BLADES AS A LIMB!

make object box 201,10,2,150

make mesh from object 201,201

add limb 201,1,201

delete mesh 201

rotate limb 201,1,0,90,0

make mesh from object 201,201

delete object 201

add limb 200,1,201

delete mesh 201

offset limb 200,1,0,25,0

speedx#=0.0

speedz#=0.0

speedy#=0.0

hi#=0.0

do

if keystate(35):helimode=1:totalthr#=thr# * defaultthrust#:endif `h for heli

`added line to set thrust is not really necessary.

`stops heli crashing when switch to heli mode.

if keystate(31):helimode=0:endif `s for ship

if helimode

turnsmooth#=50.0

defaultthrust#=0.35 `should make this vary - increase when get close to ground => auto terrain following

clickthrust#=0.5

thrustsmooth#=50.0

pointsmooth#=10.0

show limb 200,1 `show helicopter blades

text 0,100,"HELICOPTER MODE (press S for spaceship)"

else

turnsmooth#=10.0

defaultthrust#=0.0

clickthrust#=1.0

thrustsmooth#=1.0

pointsmooth#=1.0

hide limb 200,1

text 0,100,"SPACESHIP MODE (press H for helicopter)"

endif

`text 0,100,str$(helimode)

text 0,120,"THRUST="+str$(totalthr#)

`MOUSE TO TILT SHIP `t denotes target (am adding smoothing to get mx,mz from these)

tmx#=tmx#+mousemovex()

tmz#=tmz#-mousemovey()

msq#=tmx#*tmx#+tmz#*tmz#

m#=sqrt(msq#)

if m#>100.0

tmx#=(tmx#/m#)*100.0

tmz#=(tmz#/m#)*100.0

endif

mx#=curvevalue(tmx#,mx#,pointsmooth#)

mz#=curvevalue(tmz#,mz#,pointsmooth#)

`get length of this vector

msq#=mx#*mx#+mz#*mz#

m#=sqrt(msq#)

`draw circle with cursor in

circle 50,50,48

dot 50+(50.0/100.0)*mx#, 50-(50.0/100.0)*mz#

dot 50+(50.0/100.0)*tmx#, 50-(50.0/100.0)*tmz# ,0xFFFF0000 `red dot for target

ya#=atanfull(mx#,mz#)

rotate object 200,m#,ya#,0.0

`want to 1st rotate to get "spin"

`then rotate to tilt to thrust angle (from horizontal)

`then rotate to "point" in thrust direction (viewed from above)

`for obj on ground this is

`1st "spin"

`then ground slope elevation

`then slope direction.

`this works!!

tu#=curveangle(ya#,tu#,turnsmooth#)

rotate limb 200,0,0,wrapvalue(tu#-ya#),0

`helicopter blades

br#=wrapvalue(br#+10.0) `blade rotation

rotate limb 200,1,0,wrapvalue(br#-ya#),0

`movespeed=1<<23 `bitshift<< makes bigger! (this number can be anything)

movespeed=1<<18

inc thing_posx(0), -movespeed*(leftkey()-rightkey())

inc thing_posz(0), movespeed*(upkey()-downkey())

mc=mouseclick()

thr#=2000.0

`speedx#=0.95*speedx#+thr#*mx#*mc `small angle approximation!

`speedz#=0.95*speedz#+thr#*mz#*mc

targetthrust#=thr# * ( clickthrust#*mc + defaultthrust#)

totalthr#=curvevalue( targetthrust# , totalthr# , thrustsmooth# )

speedx#=0.95*speedx#+ totalthr# *mx#*(sin(m#)/m#)*(180.0/3.14) `NOTE THAT CAN INCORPORATE (180/PI) INTO THRUST CONSTANT

speedz#=0.95*speedz#+ totalthr# *mz#*(sin(m#)/m#)*(180.0/3.14) `FOR FEWER CALCULATIONS. ALSO SIN NEED ONLY CALC ONCE.

inc thing_posx(0),speedx#

inc thing_posz(0),speedz#

`speedy#=0.95*speedy# + conversion_factor#*thr#*mc*(180.0/3.14) - 1.0 `should use proper trig! last part is gravity

speedy#=0.95*speedy# + conversion_factor#*totalthr# *cos(m#)*(180.0/3.14) - 1.0

inc hi#,speedy#

pgridx=thing_posx(0)>>22

pgridz=thing_posz(0)>>22

`gridx=(thing_posx(0)-subtract_factor)>>22 `not player grid, but grid where want to start reading landscape info to matrix

`gridz=(thing_posz(0)-subtract_factor)>>22

`can also achieve this by using (playergrid-6) &&1023

gridx=(pgridx-6)&&1023

gridz=(pgridz-6)&&1023

`using bitshift to right 22, grid goes from 0-1023.

`1024=2^10 22+10=32 (bits in a dword!)

`should only update when grid square changes.

`after that maybe use shifting- maybe faster.

if last_gridx<>gridx or last_gridz<>gridz

for a=0 to 13

for b=0 to 13

set matrix height 1,a,b,map_height((a+gridx)&&1023,(b+gridz)&&1023)

next b

next a

for a=0 to 12

for b=0 to 12

set matrix tile 1,a,b,map_tile((a+gridx)&&1023,(b+gridz)&&1023)

next b

next a

update matrix 1

last_gridx=gridx

last_gridz=gridz

endif

`POSITION CAMERA USING "REMAINDER"

`cx=-2097152+ ( (thing_posx(0)-subtract_factor) && 4194303 ) `2^22 -1 . =0x4FFFFF ?!

`cz=-2097152+ ( (thing_posz(0)-subtract_factor) && 4194303 )

cx=-2097152+ ( thing_posx(0) && 4194303 ) `dur! subtract factor && ... =0

cz=-2097152+ ( thing_posz(0) && 4194303 )

camx#=conversion_factor#*cx `bit lame that are getting +ve and -ve values. perhaps should have mat corner at origin.

camz#=conversion_factor#*cz

`position camera camx#,1000,camz#-400:point camera camx#,0,camz# `down looking. with this no need for clip plane

position object 100,camx#,0,camz#

position object 1, camx#,ground_height(thing_posx(0),thing_posz(0) ),camz#

`position object 200, camx#,100,camz#

gh#=50.0+ground_height(thing_posx(0),thing_posz(0) )

if hi#<gh#:hi#=gh#:speedy#=0.0:endif `really should be some other speed if moving horizontally..

position object 200, camx#,hi#,camz#

position camera camx#,hi#,camz#-995:point camera camx#,hi#,camz# `horiz looking. AT PLAYER

set camera clip 0,1,0,0,camz#-0.5*mat_view_size#,0,0,1

`POSITION OTHER OBJECTS! should decide whether to show (are on visible matrix)

`do that later.

for t=1 to 20

dx=thing_posx(t)-thing_posx(0)

dz=thing_posz(t)-thing_posz(0)

`tx#=(conversion_factor#*dx) +camx#

`tz#=(conversion_factor#*dz) +camz#

tx#=conversion_factor#*(dx+cx) `equivalent to above

tz#=conversion_factor#*(dz+cz)

position object 50+t,tx#,ground_height(thing_posx(t),thing_posz(t) ),tz#

next t

text 0,140,"DWORD CO-ORDS="+str$(thing_posx(0))+","+str$(thing_posz(0))

text 0,150,"GRID CO-ORDS="+str$(gridx)+","+str$(gridz)

text 0,170,str$(screen fps())+" FPS"

`text 100,20,str$(conversion_factor#)

`OLD TEST THING ===========================================

IF SPACEKEY()

`DRAW DOTS TO SCREEN

box 0,50,256,50+256,0xFF000080,0xFF000080,0xFF000080,0xFF000080

for t=0 to 10

spx=thing_posx(t)>>24 `gets 0-255 (2^8)

spz=50+(thing_posz(t)>>24)

dot spx,spz,thing_color(t)

next y

`NOW DRAW WITH POSITIONS RELATIVE TO PLAYER!

box 300,50,300+256,50+256,0xFF000080,0xFF000080,0xFF000080,0xFF000080

for t=0 to 10

REMSTART

spx=300+((thing_posx(t)-thing_posx(0))>>24) `gets 0-255 (2^8)

spz=50+((thing_posz(t)-thing_posz(0))>>24)

REMEND

`put player in middle

spx=300+((thing_posx(t)-thing_posx(0)-(1<<31))>>24) `gets 0-255 (2^8)

spz=50+((thing_posz(t)-thing_posz(0)-(1<<31))>>24)

dot spx,spz,thing_color(t)

next y

ENDIF

`============================================================

if skipsyncmode

skipsync=1-skipsync

if skipsync then sync

else

sync

endif

loop

end

function ground_height(x as dword, z as dword)

xg=x>>22

zg=z>>22

xgp=(xg+1)&&1023

zgp=(zg+1)&&1023

rx=x && 4194303

rz=z && 4194303

fx#=rx / (1.0*4194304)

fz#=rz / (1.0*4194304)

`text 100,100,str$(fx#)+","+str$(fz#)

height#=(map_height(xg,zg)*(1.0-fx#)*(1.0-fz#) + map_height(xgp,zg)*fx#*(1.0-fz#) +map_height(xg,zgp)*(1.0-fx#)*fz# +map_height(xgp,zgp)*fx#*fz#)

`^^ simplified version : mat square is treated as surface formed by straight lines in x, and z direction when viewed from above,

`connecting the square polygon grid. that there are actually 2 flat triangles is ignored.

endfunction height#

function single_pixel_image(imagenum,red,green,blue,alpha)

#constant temp_memblocknum 1

make memblock temp_memblocknum,16

write memblock dword temp_memblocknum,0,1

write memblock dword temp_memblocknum,4,1

write memblock dword temp_memblocknum,8,32

write memblock dword temp_memblocknum,12,rgba(red,green,blue,alpha)

make image from memblock imagenum,temp_memblocknum

delete memblock temp_memblocknum

endfunction

`RGBA function by Aaron Miller

function rgba(r,g,b,a)

c = (a and 0xff) << 24 or ((r and 0xff) << 16) or ((g and 0xff) << 8) or (b and 0xff)

endfunction c

`FOR NO MEDIA

function draw_mat_tex()

create bitmap 1,256,256

box 0,0,128,128,0xFFDFDFDF,0xFFFFFFFF,0xFFFFFFFF,0xFFEFEFEF

box 0,128,128,256,0xFF808080,0xFF808080,0xFF707070,0xFF808080

box 128,0,256,128,0xFF484848,0xFF606060,0xFF505050,0xFF606060

box 128,128,256,256,0xFFFF4040,0xFFFF4040,0xFFFF4040,0xFFFF4040 `red tile. not currently used.

`graduated boxes. means can see seam between tiles even if same type

`should play with graduations for best effect. should i have opposite corners same colour?

`should all corners be different?

`lines

ink 0xFF606060,0

w=0 `width `CURRENTLY LINES SWITCHED OFF

box 0,0,w,256

box 0,0,256,w

box 0,128-w,256,128+w

box 128-w,0,128+w,256

box 0,256-w,256,256

box 256-w,0,256,256

ink 0xFFFFFFFF,0

get image 1,0,0,256,256

`save image "testx.bmp",1

delete bitmap 1

endfunction