I'm working a little code snippet that alters matrix height using cosine to create a water-like effect. I also wrote a function for camera movement not using the wrap angle commands, because I wanted to work out the formulas myself.
I created an array that stores the various heights of the matrix and then updates the matrix accordingly. For some reason, the camera will not move in the right direction when I do this. I've narrowed it down to assigning the values to the different components of the array. When I created a regular variable storing the temporary height in order to update the height of the tile, or just directly plug in the value in the dbSetMatrixHeight command, the camera moves as it should. The correlation between the array and camera angle does not make much sense to me and was wondering if anyone has experienced this before or knows what's going on?
And for reference, here's my code:
//Include necessary headers
#include "DarkGDK.h"
#include <stdio.h>
#include <math.h>
//Declare global variables
#define camSpeed 0.08
#define mouseSensi 0.5
#define pi 3.14159
//Declare global matrix variables
#define mWidth 10
#define mDepth 10
#define mXSeg 10
#define mZSeg 10
//Camera variables
float cX=2,cY=2,cZ=2;
float caX=45,caY=0,caZ=0;
//Matrix variables
float mHeight[mXSeg][mZSeg];
float matHeight;
//Water variables
float wAmp=0.8,wSmooth=2;
//Function to control camera
void controlCamera()
{
//Check for forward motion
if (dbKeyState(17)==1)
{cX=cX+(camSpeed*sin(caY*pi/180));
cZ=cZ+(camSpeed*cos(caY*pi/180));}
//Check for backward motion
if (dbKeyState(31)==1)
{cX=cX-(camSpeed*sin(caY*pi/180));
cZ=cZ-(camSpeed*cos(caY*pi/180));}
//To the right
if (dbKeyState(32)==1)
{cX=cX+(camSpeed*cos(caY*pi/180));
cZ=cZ-(camSpeed*sin(caY*pi/180));}
//To the left
if (dbKeyState(30)==1)
{cX=cX-(camSpeed*cos(caY*pi/180));
cZ=cZ+(camSpeed*sin(caY*pi/180));}
//Rotate camera with mouse
caX=dbCameraAngleX()+mouseSensi*dbMouseMoveY();
caY=dbCameraAngleY()+mouseSensi*dbMouseMoveX();
//Update camera position and rotation
dbPositionCamera(cX,cY,cZ);
dbRotateCamera(caX,caY,caZ);
}
//Main Program
void DarkGDK (void)
{
//Initialize variables
char keyPressed[10], fps[10];
float t=0;
//Turn on sync and set rate
dbSyncOn();
dbSyncRate(60);
//Turn off the autocam
dbAutoCamOff();
//Black backdrop
dbColorBackdrop(dbRGB(0,0,0));
//Create the matrix
dbMakeMatrix(1,mWidth,mDepth,mXSeg,mZSeg);
//Main program loop
while (LoopGDK())
{
//Display the FPS
sprintf(fps,"FPS: %d",dbScreenFPS());
dbText(0,0,fps);
//Display the key pressed
sprintf(keyPressed,"Key Pressed: %d",dbScanCode());
dbText(0,12,keyPressed);
//Display the cos and sin of caY
sprintf(keyPressed,"Cosine: %lf Sine: %lf",cos(caY*pi/180),sin(caY*pi/180));
dbText(0,24,keyPressed);
//Display the cos and sin of caY
sprintf(keyPressed,"caX: %lf caY: %lf caZ: %lf",caX,caY,caZ);
dbText(0,36,keyPressed);
//Display the cos and sin of caY
sprintf(keyPressed,"cX: %lf cY: %lf cZ: %lf",cX,cY,cZ);
dbText(0,48,keyPressed);
//Increase the time counter
t+=0.01;
//Set matrix height
for (int i=0;i<=mXSeg;i++)
{
for (int j=0;j<=mZSeg;j++)
{
//Determine the matrix height
//mHeight[i][j]=wAmp*cos((float)(i*j*(pi/180)*wSmooth+t));
matHeight=wAmp*cos((float)(i*j*(pi/180)*wSmooth+t));
//Set the matrix height
//dbSetMatrixHeight(1,i,j,mHeight[i][j]);
//dbSetMatrixHeight(1,i,j,wAmp*cos((float)(i*j*(pi/180)*wSmooth+t)));
dbSetMatrixHeight(1,i,j,matHeight);
}
}
//Update the matrix
dbUpdateMatrix(1);
//Update camera
controlCamera();
//Update screen
dbSync();
}
//Return back to windows
return;
}