Off-Topic Discussion > Programming

Couple of questions...

(1/1)

Stormkeeper:
What's the best way to load data from a text file? Presuming the data is in this order:

1111111111
1000003001
1030002001
1020002001
1020003001
1030000001
1111111111

How would you load it ?

Also, how do you lock the camera to the mouse view ? As in make it into an FPS view.

I'm abit stuck on this two problems atm. And have been for the better part of an hour.Any help would be greatly appreciated.
Programming in C++, with freeglut, btw.

portej05:
Can help you with the first one, but not the second (don't yet know enough about the SCP internals)
You need to be much more specific with your question... Is it always going to be in that particular format, or could there be more/less data in each row/column

For the most general solution, I'd use something along these lines:

Includes:

--- Code: ---#include <fstream>
#include <vector>
#include <string>

--- End code ---


--- Code: ---std::string line;
std::vector< std::string > read_data;
std::ifstream in_file( "filename.ext" );
if ( !in_file.is_open( ) )
   return Could_not_read;

bool eof = false, oldeof = false;
while ( true )
{
  std::getline( in_file, line );
  eof = in_file.eof( );
  if ( eof && oldeof )
    break;
  oldeof = eof;

  read_data.push_back( line );
}

--- End code ---
(note, bool may be specific to microsoft compilers, I haven't tested it anywhere else)

You'll notice in the code that it can handle finishing on a newline, and checks that the file is valid
Again, without more information, I can't help you further.
To process the lines, you can use read_data.size( ) to get the size of the first vector and then
read_data.at( index ).size( ) to get the size of each line.

Hope this helps.

blackhole:
bool is a native C++ datatype and is not compiler specific. BOOL, however, is a typedef found in windows.h (or a derivative thereof).

WMCoolmon:
Method A
For the mouse thing, you would want to translate the mouse x,y position and use it to rotate the view matrix along its x and y axis.

int mouseX = getMouseX();
int mouseY = getMouseY();

Then in another function
xAxisRotation = 120*( (mouseY-screenHeight/2) / (screenHeight/2) );
yAxisRotation = 120*( (mouseX-screenWidth/2) / (screenWidth/2) );

Yes, x and y do, nominally speaking, get flipped. This is because if you picture an axis with all 3 dimensions on it, y goes up and x goes from left to right, while z goes from the back of the screen to you. So if you rotate around an axis, you're actually moving perpendicular to it. Just picture lines with arrows around them.

First, you'd need to rotate the object into position (Eg if it's oriented differently, put that matrix on). Then you do the view stuff:
glRotate3f(xAxisRotation, 1.0, 0.0, 0.0);
glRotate3f(yAxisRotation, 0.0, 1.0, 0.0);

Make sure you remember to push/pop matrices if needed.

Now that should give you a FOV of 60 degrees up, down, left, right. Good if you're going to walk and then turn using the keyboard.

Method B
If you want to do all the turning using the mouse, then you need to store the last mouse position, get the difference between the last mouse position and current mouse position, determine the number of degrees you want it to be (This can be arbitrary, the bigger it is, the more responsive your 'character' will be) and then add that rotation 'slice' on to the last rotation of the character.

I should probably add that I've never actually done this before, so take what I say with a grain of salt and try everything out, and if it works a different way, use that way. By no means is what I say supposed to be authoritative or complete, just give you some idea of what you need to do.

Navigation

[0] Message Index

Go to full version