View Full Version : Need calculate dates for Speedbutton
Heiko1
16th March 2008, 06:41
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
JO53PHS
16th March 2008, 09:30
Don't you just use Speed = Distance/time? :scratchch
Its probably more complicated than that because programming is complicated :razz:
Heiko1
16th March 2008, 09:40
distance time?
where is that?
JO53PHS
16th March 2008, 09:45
distance time?
where is that?
Speed = Distance travelled (metres) ÷ Time taken (seconds)
:shrug:
That will give you a speed in metres per second
Heiko1
16th March 2008, 17:02
and how i do a Kilometer counter?
also thats it count the meters where im driven.
A.Fedorov
16th March 2008, 19:33
You may calculate distance from MCI packet. Use old coordinates and new coordinates for get length of vector
Dygear
18th March 2008, 05:04
Something like this, A.Fedorov?
/* WARNING - UNTESTED CODE - WARNING */
function onMCI() {
extract($this->parent->packet);
if (!isset($this->$PLID->X))
$this->$PLID->X = 0;
if (!isset($this->$PLID->Y))
$this->$PLID->Y = 0;
if (!isset($this->$PLID->Z))
$this->$PLID->Z = 0;
$dist = ($this->$PLID->X[$PLID] - $X) + ($this->$PLID->Y - $Y) + ($this->$PLID->Z - $Z);
$this->$PLID->X = $X;
$this->$PLID->Y = $Y;
$this->$PLID->Z = $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?
Bob Smith
18th March 2008, 08:46
dist = [ a^2 + b^2 + c^2 ] ^ 0.5
where a = change in x, b is is change in y....
DarkTimes
18th March 2008, 12:22
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.
A.Fedorov
18th March 2008, 12:28
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 ;)
vane
31st March 2008, 18:16
why the hell doesnt anyone just work these things out for themselves? i managed to work it out easy enough...
mcgas001
31st March 2008, 18:26
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.
vane
31st March 2008, 20:35
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 :)
dougie-lampkin
31st March 2008, 22:24
Multiply by 3.6 IIRC :)
DarkTimes
31st March 2008, 22:52
Multiply by 3.6 IIRC :)
Or had, I dunno, looked five posts up.
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.
mcgas001
31st March 2008, 23:01
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 :schwitz:
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. :(
dougie-lampkin
31st March 2008, 23:02
or that :x
DarkTimes
31st March 2008, 23:06
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 (http://www.ibiblio.org/harris/500milemail.html)). 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. :)
mcgas001
31st March 2008, 23:09
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.
DarkTimes
31st March 2008, 23:14
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.
mcgas001
31st March 2008, 23:18
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.
DarkTimes
31st March 2008, 23:27
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.
mcgas001
31st March 2008, 23:31
I know it sucks, but i had to try atleast a few times to do the packet coding stuff myself. and....well...i failed. :schwitz:
Let me know if you find that bug, Although im gonna start hunting now too.:(
DarkTimes
31st March 2008, 23:48
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.
}
}
}
mcgas001
1st April 2008, 00:10
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! :)
DarkTimes
1st April 2008, 00:13
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. ;)
mcgas001
1st April 2008, 00:20
The day I meet a programmer who finds this stuff easy, is the day I officially resign. ;)
Someone (http://www.lfsforum.net/member.php?u=6700) once told me on msn its easy :p
DarkTimes
1st April 2008, 00:28
Well, TAA isn't like normal people. Look at that beard, Jesus...
Anyway my point was that programming isn't about being smart, it's about using your intelligence in meaningful way. Some people can knock great code together in their sleep, some of us need to pour over it for painstaking hours. Nobody finds it easy, if they did it wouldn't be any fun. It's like racing, if you didn't have to try really hard and practice for hundreds of laps, there would be no impetus to keep trying and improving and no sense of achievement. Anyone who says it's easy is either lying or trying to motivate you. Anyway, we've spamed this forum more than enough for tonight.
mcgas001
1st April 2008, 00:32
Well, TAA isn't like normal people. Look at that beard, Jesus...
Anyway my point was that programming isn't about being smart, it's about using your intelligence in meaningful way. Some people can knock great code together in their sleep, some of us need to pour over it for painstaking hours. Nobody finds it easy, if they did it wouldn't be any fun. It's like racing, if you didn't have to try really hard and practice for hundreds of laps, there would be no impetus to keep trying and improving, and no sense of achievement. And anyone who says it's easy is either lying or trying to motivate you. Anyway, we've spamming this forum more than enough for tonight.
Hahaha, I do agree though. Programming wouldnt be fun if you didnt sit there countless hours and then finally achive something. When i get this code finally done, I shall throw a party and your all invited :D
Like you say enough spam, Time to code and then sleep :)
Nock44
1st April 2008, 08:06
Eehii lol, he just wants a counter on the top like he saw on my server (i think) btw Heiko it's Impulser, and repace your MCI Packet Recived with this:
private void MCI(Packets.IS_MCI MCI)
{
for (int i = 0; i < Players.Count; i++)
{
decimal Speed = (decimal)((MCI.Info[i].Speed * (100f / 32768f)) * 3.6f);
decimal ConvSpeed = (decimal)(Speed * 25 / 1000);
Players[GetPlyIdx(MCI.Info[i].PLID)].Payout += ConvSpeed;
String CarSpeed = Speed * 5 / 8 + "";
string[] StrMsg = CarSpeed.Split('.');
Players[GetPlyIdx(MCI.Info[i].PLID)].RoundedSpeed = int.Parse(StrMsg[0]);
InSim.Send_BTN_CreateButton("^1Car Speed: ^7" + int.Parse(StrMsg[0]) + " MPH", Flags.ButtonStyles.ISB_DARK, 5, 30, 6, 130, 9, Players[GetPlyIdx(MCI.Info[i].PLID)].UniqueID, 2, false);
}
}
shaun463
8th April 2008, 11:57
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;
double metersTravelled = Math.Sqrt((distanceX * distanceX) + (distanceY * distanceY) + (distanceZ * distanceZ));
// Add this to our total.
totalMetersTravelled += metersTravelled.
// Convert the total into MPH/KPH...
double kilometersTravelled = totalMetersTravelled / 1000;
double milesTravelled = totalMetersTravelled / 1609.344;
Hope that helps some.
How do I do a button for the distance?
shaun463
9th April 2008, 06:07
Please help :)
dougie-lampkin
9th April 2008, 10:32
If you're using the old version of LFS_External, MCI packets come in at 1000ms intervals. In the new version (1.0.0+), they come in at 500ms intervals.
Assuming you haven't changed these values, then it's quite easy to work out distance travelled, by using Distance = Speed X Time.
Convert the received speed value into m/s ((speed / 32768f) * 100f), then divide by 2 if you have the new version, otherwise leave it. This value is then the distance travelled since the last received event :)
shaun463
9th April 2008, 15:39
If you're using the old version of LFS_External, MCI packets come in at 1000ms intervals. In the new version (1.0.0+), they come in at 500ms intervals.
Assuming you haven't changed these values, then it's quite easy to work out distance travelled, by using Distance = Speed X Time.
Convert the received speed value into m/s ((speed / 32768f) * 100f), then divide by 2 if you have the new version, otherwise leave it. This value is then the distance travelled since the last received event :)
Sorry for nagging you but I don't understand :)
dougie-lampkin
9th April 2008, 19:11
Ok...Assuming you're using the old version of LFS_External, this is what you need to do:
Convert the received speed value (MCI.Info[index].Speed) into m/s ((Speed / 32768f) * 100f). This value is then the distance travelled by the player in the last second, in metres. You can have a variable that you can then add this on to...
shaun463
10th April 2008, 14:35
So what would the code be? Sorry I'm nagging I just don't understand :(
EDIT: I'm using 1.04
dougie-lampkin
10th April 2008, 15:09
Short of doing it for you, that is the code...
The speed value that you receive in an MCI packet isn't a normal unit that you would deal with. By converting it into m/s (metres per second), you then know how far the person has travelled in the last second (MCI packets arrive once every second). To convert LFS speed units into m/s, use this:
float SpeedMS = ((MCI.Info[index].Speed / 32768f) * 100f);
I don't know what you want the code for, so I can't advise on the next part. But if you are making a total distance counter, you can then add this onto it...
Distance += SpeedMS;
XmaX
10th April 2008, 16:02
private void MCI(Packets.IS_MCI MCI)
{
float mph = mps * 2.2369362920544f;
float kph = mps * 3.6f;
// 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;
double metersTravelled = Math.Sqrt((distanceX * distanceX) + (distanceY * distanceY) + (distanceZ * distanceZ));
// Convert the total into MPH/KPH...
double kilometersTravelled = totalMetersTravelled / 1000;
double milesTravelled = totalMetersTravelled / 1609.344;
// Convert speed into meters per second.
float mps = (speed / 32768f) * 100f;
// Add this to our total.
totalMetersTravelled += metersTravelled.;
}
}
}
What i do wrong? Get always one error <.<
shaun463
10th April 2008, 16:16
I'm doing a Total distance button :)
I mean how do I put that information into a button?
XmaX
10th April 2008, 16:28
hmm good question. I wait for the answer too :P
dougie-lampkin
10th April 2008, 16:57
Erm...send a button with it as the text? It's no different to sending a normal text button...
DarkTimes
10th April 2008, 18:27
private void MCI(Packets.IS_MCI MCI)
{
float mph = mps * 2.2369362920544f;
float kph = mps * 3.6f;
// 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;
double metersTravelled = Math.Sqrt((distanceX * distanceX) + (distanceY * distanceY) + (distanceZ * distanceZ));
// Convert the total into MPH/KPH...
double kilometersTravelled = totalMetersTravelled / 1000;
double milesTravelled = totalMetersTravelled / 1609.344;
// Convert speed into meters per second.
float mps = (speed / 32768f) * 100f;
// Add this to our total.
totalMetersTravelled += metersTravelled.;
}
}
}
What i do wrong? Get always one error <.<
The code was just a generic example, you can't just copy and paste it into your own program, you will need to rewrite it for your own purposes.
XmaX
11th April 2008, 11:20
Erm...send a button with it as the text? It's no different to sending a normal text button...
Yes thats not the problem but how make in the button the distance ?
dougie-lampkin
11th April 2008, 12:38
Well, the distance is set soemwhere as a variable. If you need one for every player, you can make a new variable in the connection list (ClsConnection.cs in 1.1.0+), and then you can access it through:
Connections[GetConnIdx(UCID)].VARIABLENAME
XmaX
11th April 2008, 15:01
Hmm i dont understand. Must i do a new .cs?
dougie-lampkin
11th April 2008, 17:06
No, the Clients.cs contains the variables found in Connections[GetConnIdx..., so you can add a new one there that can contain a player's distance...
shaun463
12th April 2008, 18:01
This is just confusing :( Any chance you could unblock me on MSN so we can talk?
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.