Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
net.cpp AND what on earth is vector<string>::const_ite
06-05-2006, 12:15 PM,
#1
net.cpp AND what on earth is vector<string>::const_ite
Hi,
I had a look at the net.cpp module. Is there somewhere in the source I can take a look how these functions are used to write strings to a UDP port?

Also, I am not very familiar with c++ classes, what on earth is
Code:
vector<string>::const_iterator i
and how can I translate *i to an integer? None of the standard library functions seem to work, they are all choking on the type of i "const char" I believe it was.

I added two new command line inputs, both need to be converted to integers. One is the UDP port number, the other is the frequency of writing to the UDP port.

Thanks
Reply
06-05-2006, 05:41 PM,
#2
 
I am pretty unfamiliar with the network code, but when I search through net.cpp and net.h I don't see the line you're referring to in the code block.

What you see there is an iterator for a vector of strings. A vector<string> is like an array of strings, but a vector's a little more flexible than an array. An iterator is just a generic interface for looking at objects from a collection in order. So it makes sense to get the next string in the vector by doing ++i and to dereference the iterator to the string itself with *i. So if you have a vector that looks like this

["blah", "bleh", "bluh", "aaargh"]

and you do something like
Code:
vector<string> blah_string;
/* put the above values into blah_string*/
vector<string>::const_iterator i;
for( i = blah_string.begin(); i != blah_string.end(); ++i ) {
    cout << *i << endl;
}
it will print the values in order with each on a line.
Reply
06-05-2006, 05:53 PM,
#3
Re: net.cpp AND what on earth is vector<string>::const
nael Wrote:I added two new command line inputs, both need to be converted to integers. One is the UDP port number, the other is the frequency of writing to the UDP port.
btw, thanks for taking an interest in the network code, that area of the game hasn't gotten much attention lately. The port number is now configurable through the new menus, and config file. The frequency of writing to the UDP port could be easily added to the config file so we won't have to have more command line options... Smile in fact now we already have too many.
Reply
06-05-2006, 09:44 PM,
#4
 
For more info about vectors and iterators check out the STL documentation

http://www.sgi.com/tech/stl/
Reply
06-06-2006, 12:36 AM,
#5
 
The sgi docs help, but even I get lost in them sometimes...if you google for what you're looking for it's not hard to find on other sites. Of course there are those "book" things too...
Reply
06-06-2006, 09:41 AM,
#6
 
Code:
vector<string>::const_iterator i

That part was in main.cpp. I am still not sure how to convert this type to an integer. Ill check those docs, maybe ill find something.

As for the udp, I am doing this for the motion base I am working on (i.e. the flight simulator):
I modified the vamos source to calculate accelerations in all 6 dimensions. Those will be sent using udp to another computer that would map those accelerations to movements for the motion base. I did the same for FlightGear- a flight simulator and now I am working on a race car simulation. FlightGear had options that allow the user to split the physics engine from the game and graphics itself thus improving the response time of the motion base. The computer running the physics engine on FlightGear would then write to a udp port and the root FlightGear node would read from it.

I need the conversion to integer because I need it to output at 60Hz for example so that the motion base can operate properly.

If you know of any library function that would do this, that would be great.

Thanks.[/code]
Reply
06-06-2006, 12:49 PM,
#7
 
The "right way" to convert a string to an integer in C++ would be something like this:
Code:
string a = "55";
ostringstream s;
s.str( a );
int j;
s >> j;
To get the string out of the iterator just dereference it as I demonstrated before. You can look for some other examples of string conversions in the code, I think I recently put something like what's above in settings.cpp.
Reply
06-06-2006, 08:02 PM,
#8
 
The function sprintf is the way to do this in C and is faster that using string streams -- although both are equally insignificant if you're just doing this a few times a frame.

You can pass C++ strings to functions that expect C-style strings (arrays of chars) by using the .c_str() method. More info on C++ strings:

http://www.cppreference.com/cppstring/index.html
Reply
06-07-2006, 01:51 PM,
#9
 
Thanks guys, that was helpful.

In vamosworld.cc, whats the difference between the functions update and physupdate? Which one updates every frame?
Reply
06-07-2006, 08:52 PM,
#10
 
VAMOSWORLD::Update is called by the Update in main every graphic frame (dependent on current fps) and it calls VAMOSWORLD:TonguehysUpdate for each physics frame (currently locked at 250 fps).
Reply
06-11-2006, 03:12 PM,
#11
 
nael Wrote:
Code:
vector<string>::const_iterator i

That part was in main.cpp. I am still not sure how to convert this type to an integer. Ill check those docs, maybe ill find something.

I added that in. It is just looping through the command line arguments which have been converted to an std::vector<std:Confusedtring> object.

Just me trying to clean up the code.

Also for comverting from one type to another there is boost::lexical_cast. boost::lexical_cast<int>("3") would return 3. I think it uses stringstreams under the hood. We of course, are not using any boost libs, but I could write us something very similar.
Reply
06-12-2006, 09:30 AM,
#12
 
Code:
//----------Nael2:Begin check for acceleration update

        Vamos_Geometry::Three_Vector accel_sum;
        Vamos_Geometry::Three_Vector rotation_sum;


        if (accel_frm_count==frequency){
                accel_frm_count=0;
                cout<<"Accel="<<accel_sum<<endl<<"Rotation="<<rotation_sum<<endl;
                cout<<"Accel/FRM_RT="<<(accel_sum/frequency)<<endl<<"Rotation="<<(rotation_sum/frequency)<<endl;
                //world.writeUDP("Hello",(size_t)256,udpSocket);
                //accel_sum=accel_sum/ACCEL_FRM_RATE;
                //accel_sum[1]=accel_sum[1]/ACCEL_FRM_RATE;
                //accel_sum[2]=accel_sum[2]/ACCEL_FRM_RATE;
                //rotation_sum=rotation_sum/ACCEL_FRM_RATE;
                //rotation_sum[1]=rotation_sum[1]/ACCEL_FRM_RATE;
                //rotation_sum[2]=rotation_sum[2]/ACCEL_FRM_RATE;
                //cout<<"(X,Y,Z)="<<accel_sum<<endl<<"(X',Y',Z')="<<rotation_sum<<endl;
        }
        else{
                accel_frm_count++;
                accel_sum=accel_sum+GetCar(CONT_PLAYERLOCAL)->car->chassis().get_cm_acceleration();
                rotation_sum=accel_sum+GetCar(CONT_PLAYERLOCAL)->car->chassis().get_cm_rotation();
                //cout<<"------------------------------"<<endl;
                //cout<<accel_frm_count<<endl;
                //cout<<"Accel="<<accel_sum<<endl<<"Rotation="<<rotation_sum<<endl;
                //cout<<"Accel/10="<<(accel_sum/10)<<endl<<"Rotation/10="<<(rotation_sum/10)<<endl;
                //cout<<"-----------------------------"<<endl;
        }

I added that part in VAMOSWORLD::Update

I also added the following in RigidBody:Tongueropagate()

Code:
//-----------Begin Nael code to calculate accelerations
  cm_acceleration = total_force / m_mass;
  cm_rotation = total_torque;
//-----------End Nael code

The .h files were updated to work with these changes and it compiles fine. After running the following two lines I added in VAMOSWORLD::Update

Code:
cout<<"Accel="<<accel_sum<<endl<<"Rotation="<<rotation_sum<<endl;
                cout<<"Accel/FRM_RT="<<(accel_sum/frequency)<<endl<<"Rotation="<<(rotation_sum/frequency)<<endl;

just write zeros meaning the accelerations in RigidBody:Tongueropagate were not calculated/updated. The check against the frequency is made to make it write at a certain rate set through the command line depending on the specifications of the simulation platform - currently set to every 50 frames.

From my understanding, propagate() updates the center of mass every frame, correct?

The strange thing is when I did this with the source code from the last version release, it worked and updated properly - but I had a problem with the car bouncing up and down - apparently because of issues with gcc4 -
So, I got gcc 3.3 and the latest svn checkout and now thats fixed but the modifications I made dont update every 'x' frames anymore and I just get that line of zeros...If I somehow put my code in incorrect functions pls correct me.

Thanks guys, and I hope I did not lose anybody with my explanation.
Reply
06-12-2006, 08:09 PM,
#13
 
It looks to me like you've got a few problems. First of all, you're instantiating local variables accel_sum and rotation_sum (which will be set to zero upon creation) right before you either print out their values or increment them. Right after that, they'll be destroyed and the next time through the loop you'll create new local variables accel_sum and rotation_sum (which will be set to zero upon creation), etc. I suggest reading up on the way scope works in C++. Here's a random link from google: http://cplus.about.com/od/beginnerctutor...01902a.htm

Also, "rotation_sum=accel_sum+..." should be "rotation_sum=rotation_sum+..."
Reply
06-13-2006, 10:59 AM,
#14
 
woops, that was something I forgot to change. Previously I was printing out accelerations every frame, when I added the frequency stuff I added the declarations globally and forgot to remove them locally..silly me.

its working now.
Reply
06-13-2006, 02:02 PM,
#15
 
So, your work is just to get your sim to move the chair around, right? Does your sim have a single monitor or more than one? I work in a driving sim that uses 4 screens (3 front, 1 rear) and the shell of a real car; sadly it's not a single computer with several video cards, but several computers that pass car position data across the network to each other and each one projects a single view of a certain camera angle. How is your sim set up, does it have multiple views and if so how is it done?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)