Hi, iv been trying to implement some code i found in a rts tutorial by Phaelax with no success.The tutorial was not in GDK but i think iv accurately translated it.
Heres the translated code. All the variables have been defined at the start of my program:
void ClickMatrixPosition(void)
{
mouseposx=dbMouseX() - screenwidth2;
mouseposy=screenheight2 - dbMouseY();
dist=dbSqrt(pow((dbCameraPositionX()- offsetx),2)+pow((dbCameraPositionY()-offsety),2)+pow((dbCameraPositionZ()-offsetz),2));
if (mouseposy!=0)
{
vectorang=dbAtanFull(scalefactor,mouseposy);
}
else
{
vectorang=90.0;
}
if (vectorang+dbCameraAngleX()>90.965)
{
ratio=mouseposy/(sin((vectorang+dbCameraAngleX() )-90.0));
cursorposy=ratio*sin(180.0-vectorang);
}
else
{
cursorposy=1000000.0;
}
hyplength=scalefactor/sin(vectorang);
cursorposx=(((ratio*sin(90.0-dbCameraAngleX() ))/hyplength)+1.0)*mouseposx;
if (mouseposx!=0)
{
angtotal=dbWrapValue(dbCameraAngleY()-dbAtanFull(mouseposy,mouseposx));
}
else
{
if (mouseposy<0)
{
angtotal=dbWrapValue(dbCameraAngleY()+90.0);
}
else
{
angtotal=dbWrapValue(dbCameraAngleY()-90.0);
}
}
cameraang=dbWrapValue(360.0-dbCameraAngleY() );
cosang=cos(cameraang);
sinang=sin(cameraang);
movex = (dist/scalefactor)*((cosang*cursorposx)+((-1*sinang)*cursorposy));
movez = (dist/scalefactor)*((sinang*cursorposx)+(cosang*cursorposy));
movex=movex+offsetx;
movez=movex+offsetz;
movey=0;
dbPositionObject(101,movex,movey,movez);
}
And the original straight from the tutorial:
These variables must be declared at the beginning of your program.
screenwidth2# = screen width() / 2.0
screenheight2# = screen height() / 2.0
scalefactor# = screen height() / 1.2
The offsetx#, offsety#, offsetz# variables is position at which the camera is pointing to.
_Click_Matrix_Position:
mouseposx#=mousex() - screenwidth2#
mouseposy#=screenheight2# - mousey()
dist#=sqrt((camera position x()-offsetx#)^2+(camera position y()-offsety#)^2+(camera position z()-offsetz#)^2)
if mouseposy#<>0
vectorang#=atanfull(scalefactor#,mouseposy#)
else
vectorang#=90.0
endif
if vectorang#+camera angle x()>90.965
ratio#=mouseposy#/(sin((vectorang#+camera angle x() )-90.0))
cursorposy#=ratio#*sin(180.0-vectorang#)
else
cursorposy#=1000000.0
endif
hyplength#=scalefactor#/sin(vectorang#)
cursorposx#=(((ratio#*sin(90.0-camera angle x() ))/hyplength#)+1.0)*mouseposx#
if mouseposx#<>0
angtotal#=wrapvalue(camera angle y()-atanfull(mouseposy#,mouseposx#))
else
if mouseposy#<0
angtotal#=wrapvalue(camera angle y()+90.0)
else
angtotal#=wrapvalue(camera angle y()-90.0)
endif
endif
cameraang#=wrapvalue(360.0-camera angle y() )
cosang#=cos(cameraang#)
sinang#=sin(cameraang#)
movex# = (dist#/scalefactor#)*((cosang#*cursorposx#)+((-1*sinang#)*cursorposy#))
movez# = (dist#/scalefactor#)*((sinang#*cursorposx#)+(cosang#*cursorposy#))
rem 3D position stored into these variables
movex#=movex#+offsetx#
movez#=movez#+offsetz#
movey#=0
RETURN
The math and stuff is beyond me - I was expecting movex, movey and move z to return the 3d location of the mouse so i could for instance get a building (or cube) to follow the mouse around on the matrix I have. But when i try to use it like that the building/cube is nowhere in sight.
So am i using it wrong? Am i wrong in what i think it does?
Here's my code:
// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
#include <iostream>
using namespace std;
int buttonx = 0;
int building = 0;
double screenwidth2d = (dbScreenWidth() / 2.0);
double screenheight2d = (dbScreenHeight() / 2.0);
double scalefactord = (dbScreenHeight() / 1.2);
int screenwidth2 = int(screenwidth2d);
int screenheight2 = int(screenheight2d);
int scalefactor = int(scalefactord);
int mouseposx;
int mouseposy;
float dist;
float offsetx;
float offsety;
float offsetz;
float vectorang;
float ratio;
float cursorposy;
float cursorposx;
float hyplength;
float movex;
float movey;
float movez;
float sinang;
float cosang;
float cameraang;
float angtotal;
float cameradistance = 1000;
float oldcameradistance = 1000;
int oldmousez;
void MoveCamera(void);
void Scrolling (void);
void Buttons (void);
void Building (void);
void ClickMatrixPosition(void);
// the main entry point for the application is this function
void DarkGDK ( void )
{
dbAutoCamOff();
dbSetDisplayMode(1024, 768, 32);
dbSetWindowPosition(1920/2-512,1080/2-384);
dbLoadImage("Bar0.png",1);
dbLoadImage("Bar1.png",2);
dbLoadImage("Bar2.png",3);
dbLoadImage("Bar3.png",4);
dbLoadImage("grass_t.bmp",5);
dbMakeMatrix(1,1000,1000,50,50);
dbPrepareMatrixTexture ( 1, 5, 1, 1 );
dbFillMatrix ( 1, 1000, 1000 );
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
dbMakeObjectCube (1,50);
dbPositionObject (1,500,0,500);
// position the camera
dbPositionCamera( 500, 1000, 500 );
dbPointCamera (500,0,500);
offsetx=500;
offsety=0;
offsetz=500;
// our main loop
while ( LoopGDK ( ) )
{
ClickMatrixPosition();
MoveCamera() ;
Scrolling() ;
Buttons() ;
Building();
// update the screen
dbSync ( );
}
// return back to windows
return;
}
void MoveCamera(void)
{
// move the camera using the arrow keys
if (dbUpKey())
{
dbPositionCamera (dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ()+2);
}
if (dbDownKey())
{
dbPositionCamera (dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ()-2);
}
if (dbLeftKey())
{
dbPositionCamera (dbCameraPositionX()-2,dbCameraPositionY(),dbCameraPositionZ());
}
if (dbRightKey())
{
dbPositionCamera (dbCameraPositionX()+2,dbCameraPositionY(),dbCameraPositionZ());
if (dbMouseClick()==2)
{
}
}
}
void Scrolling (void)
{
if(!(dbMouseClick()==2))
{
if (dbMouseX()<50)
{
dbPositionCamera (dbCameraPositionX()-2,dbCameraPositionY(),dbCameraPositionZ());
}
if (dbMouseX()>974)
{
dbPositionCamera (dbCameraPositionX()+2,dbCameraPositionY(),dbCameraPositionZ());
}
if (dbMouseY()<50)
{
dbPositionCamera (dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ()+2);
}
if (dbMouseY()>718)
{
dbPositionCamera (dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ()-2);
}
}
if (dbMouseMoveZ()!=0)
{
cameradistance=dbCameraPositionY()-(dbMouseZ()-oldmousez);
if (!( cameradistance<200 || cameradistance>1000))
{
dbPositionCamera( dbCameraPositionX(), cameradistance, dbCameraPositionZ() );
}
}
oldmousez=dbMouseZ();
oldcameradistance=dbCameraPositionY();
}
void Buttons (void)
{
buttonx=0;
if (dbMouseX()>(1024-300+20) && dbMouseX()<(1024-300+100) && dbMouseY()<400 && dbMouseY()>350)
{
buttonx = 1;
}
if (dbMouseX()>(1024-300+200) && dbMouseX()<(1024-300+280) && dbMouseY()<400 && dbMouseY()>350)
{
buttonx = 2;
}
if (dbMouseX()>(1024-300+20) && dbMouseX()<(1024-300+100) && dbMouseY()<470 && dbMouseY()>420)
{
buttonx = 3;
}
if (buttonx==0)
{
dbSprite(1,724,0,1);
}
else if (buttonx==1)
{
dbSprite(1,724,0,2);
}
else if (buttonx==2)
{
dbSprite(1,724,0,3);
}
else if (buttonx==3)
{
dbSprite(1,724,0,4);
}
}
void Building (void)
{
if (dbMouseClick()==1)
{
if (buttonx==1)
{
building=1;
dbMakeObjectCube (100,100);
dbMakeObjectCube (101,100);
}
}
if (dbMouseClick()==2)
{
building=0;
}
if (!(building==0))
{
dbPositionObject(100,movex,movey,movez);
}
}
void ClickMatrixPosition(void)
{
mouseposx=dbMouseX() - screenwidth2;
mouseposy=screenheight2 - dbMouseY();
dist=dbSqrt(pow((dbCameraPositionX()- offsetx),2)+pow((dbCameraPositionY()-offsety),2)+pow((dbCameraPositionZ()-offsetz),2));
if (mouseposy!=0)
{
vectorang=dbAtanFull(scalefactor,mouseposy);
}
else
{
vectorang=90.0;
}
if (vectorang+dbCameraAngleX()>90.965)
{
ratio=mouseposy/(sin((vectorang+dbCameraAngleX() )-90.0));
cursorposy=ratio*sin(180.0-vectorang);
}
else
{
cursorposy=1000000.0;
}
hyplength=scalefactor/sin(vectorang);
cursorposx=(((ratio*sin(90.0-dbCameraAngleX() ))/hyplength)+1.0)*mouseposx;
if (mouseposx!=0)
{
angtotal=dbWrapValue(dbCameraAngleY()-dbAtanFull(mouseposy,mouseposx));
}
else
{
if (mouseposy<0)
{
angtotal=dbWrapValue(dbCameraAngleY()+90.0);
}
else
{
angtotal=dbWrapValue(dbCameraAngleY()-90.0);
}
}
cameraang=dbWrapValue(360.0-dbCameraAngleY() );
cosang=cos(cameraang);
sinang=sin(cameraang);
movex = (dist/scalefactor)*((cosang*cursorposx)+((-1*sinang)*cursorposy));
movez = (dist/scalefactor)*((sinang*cursorposx)+(cosang*cursorposy));
movex=movex+offsetx;
movez=movex+offsetz;
movey=0;
dbPositionObject(101,movex,movey,movez);
}
Thanks