The online racing simulator
.
.
Don't you just use Speed = Distance/time?

Its probably more complicated than that because programming is complicated
-
(Heiko1) DELETED by Heiko1
Quote from Heiko1 :distance time?

where is that?

Speed = Distance travelled (metres) ÷ Time taken (seconds)




That will give you a speed in metres per second
-
(Heiko1) DELETED by Heiko1
You may calculate distance from MCI packet. Use old coordinates and new coordinates for get length of vector
Something like this, A.Fedorov?


<?php 
/* WARNING - UNTESTED CODE - WARNING */
function onMCI() {
    
extract($this->parent->packet);
    if (!isset(
$this->$PLID->X))
        
$this->$PLID->0;
    if (!isset(
$this->$PLID->Y))
        
$this->$PLID->0;
    if (!isset(
$this->$PLID->Z))
        
$this->$PLID->0;
    
$dist = ($this->$PLID->X[$PLID] - $X) + ($this->$PLID->$Y) + ($this->$PLID->$Z);
    
$this->$PLID->$X;
    
$this->$PLID->$Y;
    
$this->$PLID->$Z;
    return 
$dist;
}
/* WARNING - UNTESTED CODE - WARNING */
?>

Never fully understood vectors. Cords ... is an odd thing, especially when you deal cords over time. What's your thoughts?
dist = [ a^2 + b^2 + c^2 ] ^ 0.5

where a = change in x, b is is change in y....
Quote from Heiko1 :Hello guys i need the calculate dates for the Speed
at me it shows unrealistic numbers Example:
im driving 120kph and it shows 10900

Can anybody say me the calculate dates?

Greetings Marco

I'm guessing you're using C#, as you have been posting in the LFS_External threads. If you want to convert the speed into KPH and MPH you can do something like this.

// Convert speed into meters per second.
float mps = (speed / 32768f) * 100f);

// Convert meters per second into MPH/KPH...
float mph = mps * 2.2369362920544f;
float kph = mps * 3.6f;

If you are looking at distance travelled, then you can use the X, Y and Z positions. You need to store the players last X, Y and Z co-ordinates and when when you receive a new MCI packet, you just check to see how far the player has travelled since the last one you received.


// Convert X, Y and Z into meters.
float currentX = x / 65536f;
float currentY = y / 65536f;
float currentZ = z / 65536f;

// Figure out how many meters we have moved since the previous MCI packet was received.
float distanceX = currentX - previousX;
float distanceY = currentY - previousY;
float distanceZ = currentZ - previousZ;
float metersTravelled = Math.Sqrt((distanceX * distanceX) + (distanceY * distanceY) + (distanceZ * distanceZ));

// Add this to our total.
totalMetersTravelled += metersTravelled.

// Convert the total into MPH/KPH...
float kilometersTravelled = totalMetersTravelled / 1000f;
float milesTravelled = totalMetersTravelled / 1609.344f;

Hope that helps some.
oldMCI = A(2,1) //x=2, y=1
newMCI = B(6,4)
|AB| = sqrt( (6-2)^2 + (4-1)^2) ) = sqrt(25) = 5 //length of AB

with this method we may make button-counter of real kilometers for cruise server
#9 - vane
why the hell doesnt anyone just work these things out for themselves? i managed to work it out easy enough...
Quote from vane :why the hell doesnt anyone just work these things out for themselves? i managed to work it out easy enough...

Please dont post stuff like that, If you havnt got anything helpfull to say. Dont, its not nice and you wouldnt like it if people said that to you.
#11 - vane
i know i may have sounded a bit stupid but it is easy enough to work out, i just worked out what 100m/s was in km/h then divided that by something and somethinged the something and eventually got to the answer, and if i can do it heiko can
Multiply by 3.6 IIRC
Quote from dougie-lampkin :Multiply by 3.6 IIRC

Or had, I dunno, looked five posts up.

Quote from Vane :i know i may have sounded a bit stupid but it is easy enough to work out, i just worked out what 100m/s was in km/h then divided that by something and somethinged the something and eventually got to the answer, and if i can do it heiko can

Just cause you can do something, doesn't mean other people can. Everyone is different, we all have different experiences and reference points. In my view the OP was perfectly justified in asking his question, and it's a question that a lot of other new InSim programmers will at some point ask as well. If they can come in here, find the answer and maybe learn some stuff along the way, then great. I see no reason to discourage that.
Yes, Exactly. Like me for example, For some reason even know ive looked up on tonnes of stuff. I still cannot recieve more the one packet from LFS due to my programming limitations

So it helps for threads like these to be around.

P.S: DarkTimes - The code in your InSim tutorial for some reason doesnt recieve more then one packet at once.
or that
Quote from mcgas001 :P.S: DarkTimes - The code in your InSim tutorial for some reason doesnt recieve more then one packet at once.

It does, but it has a bug when receiving certain packets (mainly MSO), because I made an assumption when coding it that turned out to be wrong (Can you figure out what the bug is? Took me a hell of a long time. It's one of those 500 mile bugs). I pondered about uploading a fix, but I decided to wait instead to see if anyone else figured out something was wrong. It has taken over a month.

I'll upload a fixed version soon.
Quote from DarkTimes :I'll upload a fixed version soon.

There maybe no need, Does your lib code handle the packets correctly? I think i still have that somewhere on my PC.
Quote from mcgas001 :There maybe no need, Does your lib code handle the packets correctly? I think i still have that somewhere on my PC.

No, the bug is there in the old version of my InSim API too. I'd really be interested to see if you can figure out what the bug is. It's one of the weirdest bugs of my making that I've ever tracked down.
Quote from DarkTimes :No, the bug is there in the old version of my InSim API too. I'd really be interested to see if you can figure out what the bug is. It's one of the weirdest bugs of my making that I've ever tracked down.

OK, Thats strange then. I also have tried loads of different ways too. This is one way i coded my self but after looking at your tutorial. Looks bad, but works(JUST!):

private void RecieveWorker()
{
byte[] buffer = new byte[MaxBuffer];
int BytesRecieved = 0;
int CurrentPacketSize = 0;
while (!_exit)
{
try { BytesRecieved = Client.Receive(buffer); }
catch { }
if (BytesRecieved == 0)
continue;
CurrentPacketSize = buffer[0];
while ((CurrentPacketSize != 0) & (BytesRecieved >= CurrentPacketSize))
{
byte[] Packet = new byte[CurrentPacketSize];
Buffer.BlockCopy(buffer, 0, Packet, 0, CurrentPacketSize);
Array.Clear(buffer, 0, CurrentPacketSize);
ProcessPacket(Packet);
CurrentPacketSize = buffer[0];
}
}
}

Probley sucks but it works ok atm,(only with one packet per time though :schwitz Ill see if i can find that bug.
Your code is flawed as you do not take into consideration the way in which the TCP protocol works. Remember, TCP is a constant stream of data, it is not separated into different packets (unlike UDP).

In your code it appears to only be receiving one packet at a time as you are only processing the first packet from the stream each time data is received, and you're disregarding any packets which may follow it.

You need to design your packet parsing loop with the idea that at any one time the buffer will contain multiple packets, and will often end with an incomplete packet. You need to parse out each packet from the buffer and then store any incomplete packets so they can be completed on the next receive call.

This, incidentally, is not the bug in the tutorial code, although it can have the same results, depending on which packet is received.
I know it sucks, but i had to try atleast a few times to do the packet coding stuff myself. and....well...i failed.

Let me know if you find that bug, Although im gonna start hunting now too.
This is some code hacked from LFSPoints 3.0... It is untested in its hacked form, but within the program itself it has flawlessly dealt with thousands of packets across many league races. Edit: It's missing a couple of methods which are called, but I'm sure you can figure out yourself how to get the packet size etc...


const int BufferSize = 512;

void ReceiveThread()
{
// Global buffer where we store our packets between receive calls.
List<byte> buffer = new List<byte>();

while (true)
{
// We read the incoming TCP stream into our receiveBuffer.
byte[] receiveBuffer = new byte[BufferSize];
int bytesReceived = socket.Receive(receiveBuffer);

// If zero bytes were received it means the TCP connection has been lost.
if (bytesReceived == 0)
{
// Handle lost connection.
return;
}

// We copy the packets out of the receiveBuffer and add them to our
// global buffer.
byte[] tempBuffer = new byte[bytesReceived];
Array.Copy(receiveBuffer, tempBuffer, bytesReceived);
buffer.AddRange(tempBuffer);

// We loop through the global buffer and parse any completed packets out.
byte packetSize = GetNextPacketSize(buffer);
while ((packetSize != 0) && (buffer.Count >= packetSize))
{
byte[] packet = new byte[packetSize];
buffer.CopyTo(0, packet, 0, packetSize);
buffer.RemoveRange(0, packetSize);

// INSERT CODE TO FIRE PACKET RECEIVED EVENT
// OnPacketReceived(packet);

if (buffer.Count > 0)
packetSize = GetNextPacketSize(buffer);
else
packetSize = 0; // Setting this to zero breaks the loop.
}
}
}

Heh, You make it look so easy. Thanks for this. I dunno if it makes sence, but im incapable of copying code and being happy with it. Just keeps playing on my mind, If they can do it, I can. For that reason i get like this. But anyway, Thank you!
The only easy thing I did was copy & paste it out of Visual Studio. It's one of many hundreds of methods in a program I've been working on every day for two months. It took many hours to get that bit of code right. The day I meet a programmer who finds this stuff easy, is the day I officially resign.
Quote from DarkTimes :The day I meet a programmer who finds this stuff easy, is the day I officially resign.

Someone once told me on msn its easy
1

FGED GREDG RDFGDR GSFDG