Posted: 13th Feb 2004 17:12
Really just the mouse, camera, and push scroll system from OIE.

Would work in DBC with a little modification. Note that this does not use the Pick Object command set, it just positions an object wherever you click, easy to impliment though.

+ Code Snippet
`RTS Mouse control demo by Van-B (DBPro)
`Note - would work in DBClassic with some minor modifications (mainly using an array for the globals)

`This system uses 0 as the lowest level - I like to use 0 as the water height, so when you click water,
`you click the surface of the water instead of underneath.

sync on : sync rate 0 : autocam off

`Globalised variables for MUS function
global musx#
global musy#
global musz#


`Make a test texture
cls rgb(0,255,0)
ink rgb(0,32,0),rgb(0,32,0)
box 1,1,31,31
get image 1,0,0,32,32

ink rgb(255,255,0),0

`Simple matrix
make matrix 1,6400,6400,64,64
prepare matrix texture 1,1,1,1
randomize matrix 1,100
update matrix 1

yang=0

`Object 2 is the fake mouse pointer, but it can be reused for other calculations too
make object cube 2,50 : hide object 2
`Object 3 is moved with the left mouse button
make object sphere 3,50

hide mouse

`Default camera start location
camy#=500
camx#=3200
camz#=3200



`Main loop

do


   mx=mousex()
   my=mousey()
   mc=mouseclick()

   if leftkey()=1 then yang=wrapvalue(yang-1)
   if rightkey()=1 then yang=wrapvalue(yang+1)
   if upkey()=1 then inc camy#,10
   if downkey()=1 then dec camy#,10

   gosub calc_camera

   gosub calc_mouse

   `Scale the ball, just to show the accuracy better
   scale=abs(sin(timer()/6.0)*100)
   scale object 3,scale,scale,scale

sync
loop









calc_mouse:
   `Calculate the MMZ# and MMX# as 0.0-1.0 floating point variables
   mmz#=(((my*1.0)/(screen height()*1.0))-1.05)*(camy#*2.10)
   mmx#=(((mx*1.0)/(screen width()*1.0))-0.500)*(camy#*2.0)*(1.10-(((my*1.15)/(screen height()*1.15))-0.575))

   mx#=newxvalue(camx#,camera angle y(),0-mmz#)
   mz#=newzvalue(camz#,camera angle y(),0-mmz#)
   mx#=newxvalue(mx#,wrapvalue(camera angle y()-90),0-mmx#)
   mz#=newzvalue(mz#,wrapvalue(camera angle y()-90),0-mmx#)

   position object 2,mx#,0,mz#

   osx=object screen x(2) : osy=object screen y(2)

   `Stand-in cursor
   line osx-10,osy,osx+10,osy
   line osx,osy-10,osx,osy+10

   `Click the mouse to position the box
   `NOTE: Everytime you click on the matrice MUSX# etc are set, so you'd do all your mouseclick chores here
   if mc=1
      mus(mx#,mz#)
      position object 3,musx#,musy#,musz#
   endif
return


calc_camera:
   `Very important, you can't change this without changing the forumulaes - live with it if you can
   rotate camera 55,yang,0
   mmx=0
   mmz=0
   msx=mx : if msx<0 then msx=0
   msy=my : if msy<0 then msy=0
   mox=screen width()-mx : if mox<0 then mox=0
   moy=screen height()-my : if moy<0 then moy=0

   `Check the edges of the screen and set the values for push scrolling
   if msx<50 then mmx=(-50.0+msx)/1.5
   if msy<50 then mmz=(-50.0+msy)/1.5
   if mox<50 then mmx=(50.0-mox)/1.5
   if moy<50 then mmz=(50.0-moy)/1.5

   `Calculate the new camera position based on the push scrolling values
   camx#=newxvalue(camx#,camera angle y(),0-mmz)
   camz#=newzvalue(camz#,camera angle y(),0-mmz)
   camx#=newxvalue(camx#,wrapvalue(camera angle y()-90),0-mmx)
   camz#=newzvalue(camz#,wrapvalue(camera angle y()-90),0-mmx)
   camh#=get ground height(1,camx#,camz#)
   if camy#<camh#+50.0 then camy#=camh#+50
   position camera camx#,camy#,camz#
return


`This function will return the 3D cursor location based on X, Z, and a height of 0
`It stores the results in MUSX#,MUSY#,MUSZ#
function mus(mx#,mz#)
musx#=mx#
musy#=0
musz#=mz#
position object 2,mx#,0,mz#
point object 2,camera position x(),camera position y(),camera position z()

while object position y(2)<camera position y()

   `NOTE - the 25 is for the accuracy, this needs to be as big as you can for speed sakes
   `about 1/4 of a tiles width is reasonable, however it can be much less without killing the speed
   move object 2,25

   mh#=get ground height(1,object position x(2),object position z(2))
   if object position y(2)<mh#
      musx#=object position x(2)
      musy#=object position y(2)
      musz#=object position z(2)
   endif
endwhile
musy#=get ground height(1,musx#,musz#)
endfunction



Van-B
Posted: 14th Feb 2004 21:12
Whoa this is cool! How could I add a texture to the terrain?
Posted: 16th Feb 2004 10:39
To add a texture, you'd just load in the image instead of generating it at the start, look for the GET IMAGE command and load an image instead.


Van-B
Posted: 16th Feb 2004 17:46
This was exactly what I was looking for, for I was going to do this in a way that's probably way more stupid and difficult. Though it doesn't work in DBC..

[error]:

`Globalised variables for MUS function
global musx#
global musy#
global musz#

Does anyone know how to change that into DBC code?
Posted: 17th Feb 2004 10:36
Yeah, you could just make simple arrays for them instead, like:

Dim musx#(1)

Then, you use the variable musx#(0) instead of musx#.

I would have converted it and posteb both versions, but I don't have DBC installed on my PC just now.


Van-B
Posted: 19th Feb 2004 23:39
That looks like some nice code there Van B

One thing I'm not too sure I understand is the lines;
+ Code Snippet
   mmz#=(((my*1.0)/(screen height()*1.0))-1.05)*(camy#*2.10)
   mmx#=(((mx*1.0)/(screen width()*1.0))-0.500)*(camy#*2.0)*(1.10-(((my*1.15)/(screen height()*1.15))-0.575))


What exactly are they calculating? Are they just converting the mouse positoin into a float between 0 and 1?

Thanks,
Jess.
Posted: 20th Feb 2004 10:22
Well, they sorta convert the 3D mouse location into a constrained 2D area - it does generate 0.0-1.0 floating points - but it has to adjust the coordinates to get the illusion of a 2D mouse pointer from the 3D object movement.


Van-B
Posted: 21st Feb 2004 11:15
Oh, I get it now... Sheesh, don't I feel like a dits, lol...

Thanks for the ( brief ) explanation Van B

Jess.
Posted: 10th Mar 2004 2:42
Wow! This code is just what I was looking for! Thanks alot!

I was trying to do exactly what you did only with an object for the terrain, but by looking at your code I have decided to go with a matrix for the terrain as it has the "get ground height" command which is extremely useful.

Thanks!
Posted: 10th Mar 2004 4:56
GuruSY, you can use a terrain that is an object by using ???'s code(don't know his name, lol). It has all of the matrix commands and it is an object. The only problem with and object matrix is that it takes up LOTS of polygons, if you look at a matix in wireframe, each triangle is a polygon, matrices don't count very many polygons though.

Bye,
Colin
Posted: 10th Mar 2004 17:00
Whoa VanB thats ... well ... complicated! I like it!

Heres another, somewhat simpler, way of doing the same thing, although I haven't bothered with any nice graphics like pulsing white spheres or nice randomised green matrix textures.

http://darkbasicpro.thegamecreators.com/?m=forum_view&t=27536&b=6

I'm just a simple bear at heart.

Philip
Posted: 11th Mar 2004 8:53
VanB...

I have been experimenting with your code. It is very acurate in the object placement.

but...

I have no idea how you did the formulas for the camera placement.

I say this because I was trying to adjust your code to work at different x-angles, and I have no idea how you do it. If you change the angle to anything but 55, the camera controls do not work. I thought it was something to do with your camera control formulas, but when I put my own camera control in, I had the same problem. So I think it might have to do with where the actual mouse is in relation to where the mouse object is.

Any ideas on how to adjust this to any x-angle?

P.S.
Can you explain these lines that JessTicular pointed out a little more, they seem like magic to me.
+ Code Snippet
mmz#=(((my*1.0)/(screen height()*1.0))-1.05)*(camy#*2.10)
   mmx#=(((mx*1.0)/(screen width()*1.0))-0.500)*(camy#*2.0)*(1.10-(((my*1.15)/(screen height()*1.15))-0.575))
Posted: 11th Mar 2004 12:21
Hehe, that line took a good 2 days to come up with, unfortunately it's trial and error - just messing about with the values until it was roughly 2D - with other X camera angles the formulae has to be changed so that the mouse becomes 2D again, not easy.

I'll have a mess around and see if I can simplify that, maybe have it so it can calculate the formulae values depending on the X angle. I'll get back to you.

Kevil made some great code snippets, go looking for Kevin Verbeeks memblock matrice examples in the codebase - it is very easy to replace that get ground height with Kevils interpolated version for mesh matrices. I hate the standard matrices and only use them for quick examples or editors (they're good for terrain editors etc because they're stable and quick enough for that).


Van-B
Posted: 11th Mar 2004 14:02
Personally I think my code as compared to VanB's demonstrates the ease of vector math over trig math for this sort of problem.

Mind you it also demonstrates that VanB has the bigger brain. But I'm happily to come second in that department. Remember: like Winnie the Pooh I am but a bear of little brain.

Philip
Posted: 11th Mar 2004 15:15
Well my small brained bear friend, you got them damn vectors working and I could'nt! - remember your Yogi, who is traditionally smarter than the average bear .

I could'nt really fathom how I could use the vectors and restrict it to a Y height of 0 (the terrain could go below 0, but 0 is the water height in OIE). Just ended up doing it the hard way because I could control it and not have to worry too much about it once it's done. Plus it let me control the collision for myself, like I could ignore certain objects like bushes - OIE still isn't great in that respect but nobody said RTS coding was easy .


Van-B
Posted: 11th Mar 2004 15:44
Hopefully after my vector tutorials are written then everyone will be able to use them! RTS game frenzy, here we come!

Philip
Posted: 5th Apr 2004 1:12
Here's the code for DBC, i modified it a little bit, but i cant get the mouse-relocating done.

I dont really know how to work with vectors and arrey's in dbc, but the code is done.
you can execute the code without any bugs, and do everything as the original code, except for the mouse functions.