Multiplayer glitching in smallbasic

121 views Asked by At

I've been working on a Multiplayer game (again) and I've got a problem with glitching.

If you Start it up and join the server on two Computers, you will notice that the ships will kind of glitch around a bit. I think this is due to the messages sent being received at irregular times and the ships getting updated before they are in the right position.

I need help figuring out the solution to it! Thanks!

http://www.mediafire.com/download/xf7ez07dxmh0hq3/Multiplayer_start.zip

Here is the code:

Server:

TextWindow.Write("Enter the Port to host on:")
Port = TextWindow.Read()
TextWindow.WriteLine(IOTCPServer.StartServer(Port))

IOTCPServer.OnClientConnect = OnClientConnect
IOTCPServer.OnMessageRecieved = OnMessageRecieved

Timer.Tick = SendClientnumbers
Timer.Interval = 1000
Sub OnClientConnect
NumClients = NumClients + 1
Clients = IOTCPServer.GetClients()
Info["Type"] = "Cc"
Info["Info"] = Clients
 TextWindow.WriteLine(Info)
  For i = 1 To Array.GetItemCount(Clients)
  IOTCPServer.SendMessage(Info,Clients[i])
  EndFor
EndSub

Sub OnMessageRecieved
For i = 1 To NumClients
  IOTCPServer.SendMessage(IOTCPServer.LastMessageData,Clients[i-1])
  EndFor
EndSub

Sub SendClientnumbers
  For u = 1 To NumClients
    Info2["T"] = "N"
    Info2["UN"] = u
    IOTCPServer.SendMessage(Info2,Clients[u-1])
    EndFor
  EndSub

Client:

GraphicsWindow.Width = 800
GraphicsWindow.Height = 600
TextWindow.Write("Enter the port: ")
Port = TextWindow.Read()
TextWindow.Write("Enter the IP: ")
ServIp = TextWindow.Read()
TextWindow.Hide()
IOTCPClient.Connect(ServIp,Port)
IOTCPClient.OnMessageRecieved = OnMessageRecieved

Ip = IOTCPClient.IP

NumOfShips = 3




For i = 1 to NumOfShips
Ship[i] = Shapes.AddImage(Program.Directory + "/Ship1.png")
Shipx[i] = GraphicsWindow.Width / 2
Shipy[i] = GraphicsWindow.Height - 200
ShipMass[i] = 5
ShipF[i] = 0
ShipRotForce[i] = 1500
ElapsedMill[i] = Clock.ElapsedMilliseconds
EndFor

GraphicsWindow.DrawImage(Program.Directory + "/Space.jpg",0,-50)

Keybind[1] = "Up"
Keybind[2] = "Down"
Keybind[3] = "Right"
Keybind[4] = "Left"
Keybind[5] = "Space"

For KeyUpNum = 1 To 4
  Key[KeyUpNum] = "Up"
EndFor

'UserNum = 1



GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp

While UserNum = "" 
Program.Delay(100)
Time = Time + 100
If Time > 5000 Then
GraphicsWindow.ShowMessage("Could not get reply from host at: "+ServIp+":"+Port,"")
Program.Delay(1000)
Program.End()
EndIf
EndWhile



While 1 = 1 
Program.Delay(5)
'TextWindow.Clear()
For i = 1 To NumOfShips




TimeElapsed[i] = (Clock.ElapsedMilliseconds - ElapsedMill[i]) / 1000
ElapsedMill[i] = Clock.ElapsedMilliseconds

ShipAccel[i] = (ShipF[i]/ShipMass[i]) 
AngleAccel[i] = (AngleF[i]/ShipMass[i]) 

VelocityX[i] = VelocityX[i] + ((ShipAccel[i] * TimeElapsed[i]) *           Math.Cos(Math.GetRadians(Angle[i]+90))) '<--- Find the Horizontal (X) Velocity
VelocityY[i] = VelocityY[i] + ((ShipAccel[i] * TimeElapsed[i]) *     Math.Sin(Math.GetRadians(Angle[i]+90))) '<--- Find the Vertical (Y) Velocity
AngleVel[i] = AngleVel[i] + (AngleAccel[i] * TimeElapsed[i])

Shipx[i] = Shipx[i] + (VelocityX[i] * TimeElapsed[i])
Shipy[i] = Shipy[i] + (VelocityY[i] * TimeElapsed[i])
Angle[i] = Angle[i] + (AngleVel[i] * TimeElapsed[i])



Shapes.Move(Ship[i],Shipx[i],Shipy[i])
Shapes.Rotate(Ship[i],Angle[i])

EndFor

CheckKeyStrokes()
EndWhile

Sub OnKeyDown
For KeydownNum = 1 To Array.GetItemCount(Keybind)
If GraphicsWindow.LastKey = Keybind[KeydownNum] Then
  Key[KeydownNum] = "Down"
EndIf
EndFor
EndSub

Sub OnKeyUp
For KeyUpNum = 1 To Array.GetItemCount(Keybind)
If GraphicsWindow.LastKey = Keybind[KeyUpNum] Then
  Key[KeyUpNum] = "Up"
EndIf
EndFor
EndSub

Sub CheckKeyStrokes
If Key[3] = "Down" And AngleF[UserNum] <> ShipRotForce[UserNum] Then
AngleF[UserNum] = ShipRotForce[UserNum]
ForceChange()
EndIf
If Key[4] = "Down" And AngleF[UserNum] <> -ShipRotForce[UserNum] Then
AngleF[UserNum] = -ShipRotForce[UserNum]
ForceChange()
EndIf
If Key[3] = "Up" And Key[4] = "Up" And AngleF[UserNum] <> 0 Then
AngleF[UserNum] = 0
ForceChange()
EndIf
If Key[2] = "Down" And ShipF[UserNum] <> 1500 Then
ShipF[UserNum] = 1500
ForceChange()
EndIf
If Key[1] = "Down" And ShipF[UserNum] <> -1500 Then
ShipF[UserNum] = -1500
ForceChange()
EndIf
If Key[1] = "Up" And Key[2] = "Up" And ShipF[UserNum] <> 0 Then
ShipF[UserNum] = 0
ForceChange()
EndIf

If Key[5] = "Down" Then
TextWindow.WriteLine("Checking...")
SendServerCheck()
EndIf
EndSub

Sub ForceChange

If Angle[UserNum] > 360 Then
Angle[UserNum] = Angle[UserNum] - 360
EndIf
If Angle[UserNum] < 0 Then
Angle[UserNum] = Angle[UserNum] + 360
EndIf

Position["x"] = Math.Round(Shipx[UserNum]*10000)/10000
Position["y"] = Math.Round(Shipy[UserNum]*10000)/10000
Info["T"] = "FU" 'Type
Info["N"] = UserNum 'Num
Info["P"] = Position
Info["VX"] = Math.Round(VelocityX[UserNum]*10000)/10000
Info["VY"] = Math.Round(VelocityY[UserNum]*10000)/10000
Info["A"] = Math.Round(Angle[UserNum]*10000)/10000
Info["AV"] = Math.Round(AngleVel[UserNum]*10000)/10000
Info["F"] = Math.Round(ShipF[UserNum]*10000)/10000 'Force
Info["N"] = UserNum 'Num
Info["AF"] = Math.Round(AngleF[UserNum]*10000)/10000 ' Angle
IOTCPClient.SendMessage(Info)
EndSub



Sub OnMessageRecieved
RecInfo = IOTCPClient.LastMessageData

If RecInfo["T"] = "N" Then
UserNum = RecInfo["UN"]
EndIf

If RecInfo["N"] <> UserNum Then



If RecInfo["T"] = "FU" Then
  Pos = RecInfo["P"]
  Shipx[RecInfo["N"]] = Pos["x"]
  Shipy[RecInfo["N"]] = Pos["y"]
  VelocityX[RecInfo["N"]] = RecInfo["VX"]
  VelocityY[RecInfo["N"]] = RecInfo["VY"]
  Angle[RecInfo["N"]] = RecInfo["A"]
  AngleVel[RecInfo["N"]] = RecInfo["AV"]
  ShipF[RecInfo["N"]] = RecInfo["F"]
  AngleF[RecInfo["N"]] = RecInfo["AF"]
EndIf
EndIf



If RecInfo["T"] = "CH" Then
If RecInfo["N"] = UserNum Then
TextWindow.WriteLine("Connected to Server!!!")
EndIf
EndIF

EndSub

Sub SendServerCheck
Info["T"] = "CH"
IOTCPClient.SendMessage(Info)
EndSub


Sub SendPosInfo
Position["x"] = Math.Round(Shipx[UserNum]*10000)/10000
Position["y"] = Math.Round(Shipy[UserNum]*10000)/10000
Info["T"] = "PU" 'Type
Info["N"] = UserNum 'Num
Info["P"] = Position
Info["VX"] = Math.Round(VelocityX[UserNum]*10000)/10000
Info["VY"] = Math.Round(VelocityY[UserNum]*10000)/10000
Info["A"] = Math.Round(Angle[UserNum]*10000)/10000
Info["AV"] = Math.Round(AngleVel[UserNum]*10000)/10000
IOTCPClient.SendMessage(Info)
EndSub
0

There are 0 answers