To be brief, I am working on a real-time multiplayer game. In my game, the clients send updated position and velocity data to the server at a frequency of 20Hz. In the example code below, I am converting data from a Lua table into a C struct using LuaJIT FFI. It's a nifty way of transporting data over a network:
self.dt_f = self.dt_f + dt
if self.dt_f >= self.tick_f and self.id then
self.dt_f = self.dt_f - self.tick_f
local player = self.players[self.id]
local data = {
type = packets["player_update_f"],
id = self.id,
position_x = player.position.x,
position_y = player.position.y,
position_z = player.position.z,
velocity_x = player.velocity.x,
velocity_y = player.velocity.y,
velocity_z = player.velocity.z,
}
local struct = cdata:set_struct("player_update_f", data)
local encoded = cdata:encode(struct)
self.client:send(encoded)
end
When the server receives the packet, it tries to adjust the data to compensate for the latency between that particular client and itself:
local player = self.players[id]
player.position = update.position or player.position
player.velocity = update.velocity or player.velocity
local server = self.server.connection.socket
local peer = server:get_peer(id)
local ping = peer:round_trip_time() / 2 / 1000
player.position = player.position + player.velocity * ping
Once the data is normalized, it then broadcasts the updated position info to all other clients:
local data = {
type = packets["player_update_f"],
id = id,
position_x = player.position.x,
position_y = player.position.y,
position_z = player.position.z,
velocity_x = player.velocity.x,
velocity_y = player.velocity.y,
velocity_z = player.velocity.z,
}
local struct = cdata:set_struct("player_update_f", data)
local encoded = cdata:encode(struct)
self.server:send(encoded)
When the other clients finally get the packet, they adjust the data based on their latency with the server:
if id ~= self.id then
self.players[id] = self.players[id] or {}
self.players[id].position = update.position or self.players[id].position
self.players[id].velocity = update.velocity or self.players[id].velocity
local ping = self.client.connection.peer:round_trip_time() / 2 / 1000
self.players[id].position = self.players[id].position + self.players[id].velocity * ping
end
So herein lies the problem: The objects are very jittery. Every time I receive a packet, the other players warp a little forward or a little backward, so it seems like my latency compensation is off which makes my interpolation off. Perhaps someone could point out some obvious flaw in my code, or perhaps my understanding of how the process works?
For smooth animation, your server side position updates should take place on a fixed clock using the current values stored for your position/velocity vectors.
When a client update is received you need to make two calculations for the next tick:
First, using the client's vector, find the position the player should be at on the next tick.
Next, calculate a new vector for the server side player to reach that position and use that value to update the server velocity.
The server will then update all client positions in a very uniform way on the next tick. You can smooth the direction changes further by simply projecting 2 or more ticks into the future. The goal when trying to compensate for latency is really just to fall within an acceptable margin of error.
Related
For testing purposes, I am using the following custom source:
class ThrottledSource[T](
data: Array[T],
throttling: Int,
beginWaitingTime: Int = 0,
endWaitingTime: Int = 0
) extends SourceFunction[T] {
private var isRunning = true
private var offset = 0
override def run(ctx: SourceFunction.SourceContext[T]): Unit = {
Thread.sleep(beginWaitingTime)
val lock = ctx.getCheckpointLock
while (isRunning && offset < data.length) {
lock.synchronized {
ctx.collect(data(offset))
offset += 1
}
Thread.sleep(throttling)
}
Thread.sleep(endWaitingTime)
}
override def cancel(): Unit = isRunning = false
and using it like this within my test
val controlStream = new ThrottledSource[Control](
data = Array(c1,c2), endWaitingTime = 10000, throttling = 0,
)
val dataStream = new ThrottledSource[Event](
data = Array(e1,e2,e3,e4,e5),
throttling = 1000,
beginWaitingTime = 2000,
endWaitingTime = 2000,
)
val dataStream = env.addSource(events)
env.addSource(controlStream)
.connect(dataStream)
.process(MyProcessFunction)
My intent is to get all the control elements first (that is why I don't specify any beginWaitingTime nor any throttling). In processElement1 and processElement2 within MyProcessFunction I print the elements when I receive them. Most of the times I get the two control elements first as expected, but quite surprisingly to me from time to time I am getting data elements first, despite the two-second delay used for the data source to start emitting its elements. Can anyone explain this to me?
The control and data stream source operators are running in different threads, and as you've seen, there's no guarantee that the source instance running the control stream will get a chance to run before the instance running the data stream.
You could look at the answer here and its associated code on github for one way to accomplish this reliably.
We have here api on both mobile apps(Android and Ios) and web app(client dashboard) - and we have a problem encountered when we add truck parameters in api request - sometimes routes differ on mobile API and web API, we used totally same parameters, bu in the end routes still different, and we cant figure what else to change to make them same.
Here's one test example:
point1 = 37.7793808,-122.4184174(San Francisco),
point2 = 40.7559247,-73.9846107(New York),
vehicleWidth = 300 feet,
vehicleHeight = 400 feet,
vehicleLength = 200 feet,
limitedVehicleWeight = 50 lbs,
web part request:
var routingParameters = {
'mode': 'fastest;truck;traffic:enabled;boatFerry:-1',
'representation': 'display',
'routeAttributes': 'waypoints,summary,shape,legs'
};
var feet = 0.3048;
var ton = 0.00045359237;
routingParameters.vehicleWidth = (300 * feet);
routingParameters.vehicleHeight = (400 * feet) + 'm';
routingParameters.vehicleLength = (200 * feet) + 'm';
routingParameters.limitedVehicleWeight = (50 * ton) + 't';
for (var x = 0; x < points.length; x++) {
var point = points[x];
routingParameters['waypoint' + x] = 'geo!' + point.lt + ',' + point.lng;
}
var router = platform.getRoutingService();
var onResult = function (result) {
And then we display route.
As a result we have this route
Same stuff on app side(Ios example):
v
ar stops = [NMAWaypoint.init(geoCoordinates: NMAGeoCoordinates(latitude: Double(trip.startLatitude) ?? 0, longitude: Double(trip.startLongitude) ?? 0), waypointType: .stopWaypoint)]
stops.append(NMAWaypoint.init(geoCoordinates: NMAGeoCoordinates(latitude: Double(trip.endLatitude) ?? 0, longitude: Double(trip.endLongitude) ?? 0), waypointType: .stopWaypoint) )
let routingMode = NMARoutingMode.init(routingType: NMARoutingType.fastest, transportMode: NMATransportMode.truck, routingOptions: NMARoutingOption.avoidBoatFerry)
let dimensions = TruckDimensions.getSingleEntity()
routingMode.vehicleWidth = dimensions.width.floatValue * feet
routingMode.vehicleHeight = dimensions.height.floatValue * feet
routingMode.vehicleLength = dimensions.length.floatValue * feet
routingMode.limitedVehicleWeight = dimensions.weight.floatValue * ton
let penalty = NMADynamicPenalty.init()
penalty.trafficPenaltyMode = NMATrafficPenaltyMode.optimal
coreRouter.dynamicPenalty = penalty
progress = coreRouter.calculateRoute(withStops: stops, routingMode: routingMode) { (result, error) in
And as result we're getting totally different route:
We found all the parameters that sent by default with app request and tried to do same on web part(at least one that we found on documentation, as its quite empty):
But nothing helped, routed still different. Our guess that web and mobile API uses different calculations on api side, but we cant find any proofs. How can we have both app side and web side api's with trucks parameters give us same routes? This is critical part of the logic.
Thank you
The base algorithms are or started the same, but the code base has diverged between the mobile SDK and web services in the last years.
The Web services have newer features like HSP (historical speed patterns), but the mobile SDK service does not (and can not). Therefore you might get slightly different results.
The mobile SDK route calculation might also be done in offline mode, where the algorithm is different for sure.
I want to read a serail port every 0.1s and append the incoming data to an array, I can show the data this time but the array seems only store the newest data. Anyone can tell me why? Thanks.
Here is some code:
function wtsMat_OpeningFcn(hObject, eventdata, handles, varargin)
.....
%%tact is the array to store data
tact=ones(1,84);
handles.tact=tact;
% Update handles structure
guidata(hObject, handles);
Here is setting of scom
scom=serial(com_cur,'BaudRate',baud_curNum,'Parity','none','DataBits',8,'StopBits',1,...
'InputBufferSize',1000,...
'TimeOut',1,...
'TimerPeriod',0.1,...
'timerfcn',{#mycallback,handles});
fopen(scom);
handles.scom=scom;
guidata(hObject,handles);
here is mycallback function
function mycallback(scom,BytsAvailable,handles)
%start single frame acquisition
showData=ones(84,1);
showWin=ones(14,6);
%%get previous data from handles
tact=handles.tact;
fwrite(scom,uint8(hex2dec(['AA';'AA';'AA';'20';'01';'00';'00';'8F';'83'])));
myData = fread(scom,183);%read raw data from sensor
for i=1:84
showData(i,1)=myData(13+i*2)*16*16+myData(12+i*2);
end
%%append to tact array
tact=[tact;showData'];
handles.tact=tact;
....
save tact when close the scom
function pb_close_Callback(hObject, eventdata, handles)
% hObject handle to pb_close (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
scom=handles.scom;
%stop acquising
fwrite(scom,uint8(hex2dec(['AA';'AA';'AA';'22';'00';'00';'0E';'76'])));
fclose(scom);
tact=handles.tact;
save('tact.mat','tact');
Try creating a buffer and saving the new data in the last position at every iteration
buf_len = 100;
index = 1:buf_len;
%Initialize array
arrayOfData = zeros(buf_len,1);
% get Data here. Let's say the new value is theta
arrayOfData = [ arrayOfData (2:end) ; theta ];
plot(index,Tdata,'r','LineWidth',2);
I'm learning Corona SDK and I'm making small project in that purpose.
So, my problem is next one:
I managed to create 2 physics objects and make one of them "explode" when it collides with the other one. My question is how to make other object (it has linear impulse applied) stop when it collides? Also, when it stops, it has to be removed from the screen to avoid colliding with other objects...
Here is a part with removing first object on collision:
nloDrop = function()
local nlo = display.newImageRect("nlo.png", 65, 25)
nlo.x = 35 + mRand(410) ; nlo.y = -60
physics.addBody(nlo, "dynamic", {density=1, bounce = 0, friction = 0, filter = {maskBits = 4, categoryBits = 2}})
nlo:applyLinearImpulse(0, 0.8, nlo.x, nlo.y)
nlo.isSensor = true
nlo.collision = nloCollision
nlo:addEventListener("collision", nlo)
nlo.name = "nlo"
toFront()
end
And here is 'collision' function:
function nloCollision(self, event)
if ((event.other.myName == "weaponName") then
print("funkc")
self:removeSelf()
self:removeEventListener("collision", nlo)
self = nil
if weapon ~= nil then
-- stop moving of weapon
end
end
end
Thanks!
You can make the object bodyActive false and then it will not respond to physics. You cant remove a body from physics within the active screen so its a better option to keep that object out of the screen.
I made it setting the object like local variable and making a function that deletes/removes each variable (object) after some interaction or collision.
1st function contains object creating (that is a local type under function) and applying physics to that object.
2nd function contains deleting (self:removeSelf()) that works because each object is object for itself and when deleting it, physics stuff will continue to work because new local object is going to be created.
function create(event)
local weap1 = display.newImage("weap1.png", 0, 0)
weap1.x = turret.x ; weap1.y = turret.y
weap1.rotation = turret.rotation
weap1.collision = weap1Collision
weap1:addEventListener("collision", weap1)
physics.addBody(weap1, "dynamic", {density = 1, friction = 0, bounce = 0, filter = {maskBits = 2, categoryBits = 4}})
weap1:applyLinearImpulse(forceWeap1*xComp, forceWeap1*yComp, weap1.x, weap1.y)
function weap1Collision(self,event)
if (event.other.name == "collisionObject") then
self:removeSelf()
self:removeEventListener("collision", weap1)
self = nil
end
end
local type of variable (object) makes it work.
P.S.: vanshika, thanks for your answer, it's useful ;)
I am new to SalesForce (3 months).
Thus far I have been able to create an application in C# that I can use to preform Inserts and Updates to the SalesForce database. These transactions are one at a time.
No I have the need to preform large scale transactions. For example updating thousands of records at a time. Doing them one by one would quickly put us over our allotted API calls per 24 hour period.
I want to utilize the available bulk transactions process to cut down on the number of API calls. Thus far I have not had much luck coding this nor have I found any such documentation.
If anyone could either provide some generic examples or steer me to reliable documentation on the subject I would greatly appreciate it.
FYI, the data I need to use to do the updates and inserts comes from an IBM Unidata database sitting on an AIX machine. So direct web services communication is not realy possible. Getting the data from Unidata has been my headache. I have that worked out. Now the bulk api to SalesForce is my new headache.
Thanks in advance.
Jeff
You don't mention which API you're currently using, but using the soap partner or enterprise APIs you can write records to salesforce 200 at a time. (the create/update/upsert calls all take an array of SObjects).
Using the bulk API you can send data in chunks of thousands of rows at a time.
You can find the documentation for both sets of APIs here
The answers already given are a good start; however, are you sure you need to actually write a custom app that uses the bulk API? The salesforce data loader is a pretty robust tool, includes a command line interface, and can use either the "normal" or bulk data API's. Unless you are needing to do fancy logic as part of your insert/updates, or some sort of more real-time / on-demand loading, the data loader is going to be a better option than a custom app.
(this is the SOAP code though, not the Salesforce "Bulk API" ; careful not to confuse the two)
Mighy be below code provide clear insight on how to do bulk insertion.
/// Demonstrates how to create one or more Account records via the API
public void CreateAccountSample()
{
Account account1 = new Account();
Account account2 = new Account();
// Set some fields on the account1 object. Name field is not set
// so this record should fail as it is a required field.
account1.BillingCity = "Wichita";
account1.BillingCountry = "US";
account1.BillingState = "KA";
account1.BillingStreet = "4322 Haystack Boulevard";
account1.BillingPostalCode = "87901";
// Set some fields on the account2 object
account2.Name = "Golden Straw";
account2.BillingCity = "Oakland";
account2.BillingCountry = "US";
account2.BillingState = "CA";
account2.BillingStreet = "666 Raiders Boulevard";
account2.BillingPostalCode = "97502";
// Create an array of SObjects to hold the accounts
sObject[] accounts = new sObject[2];
// Add the accounts to the SObject array
accounts[0] = account1;
accounts[1] = account2;
// Invoke the create() call
try
{
SaveResult[] saveResults = binding.create(accounts);
// Handle the results
for (int i = 0; i < saveResults.Length; i++)
{
// Determine whether create() succeeded or had errors
if (saveResults[i].success)
{
// No errors, so retrieve the Id created for this record
Console.WriteLine("An Account was created with Id: {0}",
saveResults[i].id);
}
else
{
Console.WriteLine("Item {0} had an error updating", i);
// Handle the errors
foreach (Error error in saveResults[i].errors)
{
Console.WriteLine("Error code is: {0}",
error.statusCode.ToString());
Console.WriteLine("Error message: {0}", error.message);
}
}
}
}
catch (SoapException e)
{
Console.WriteLine(e.Code);
Console.WriteLine(e.Message);
}
}
Please find the small code which may help you to insert the data into salesforce objects using c# and WSDL APIs. I stuck to much to write code in c#. I assigned using direct index after spiting you can use your ways.
I split the column using | (pipe sign). You may change this and also <br>, \n, etc. (row and column breaking)
Means you can enter N rows which are in your HTML/text file. I wrote the program to add order by my designers who put the order on other website and fetch the data from e-commerce website and who has no interface for the salesforce to add/view the order records. I created one object for the same. and add following columns in the object.
Your suggestions are welcome.
private SforceService binding; // declare the salesforce servive using your access credential
try
{
string stroppid = "111111111111111111";
System.Net.HttpWebRequest fr;
Uri targetUri = new Uri("http://abc.xyz.com/test.html");
fr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(targetUri);
if ((fr.GetResponse().ContentLength > 0))
{
System.IO.StreamReader str = new System.IO.StreamReader(fr.GetResponse().GetResponseStream());
string allrow = str.ReadToEnd();
string stringSeparators = "<br>";
string[] row1 = Regex.Split(allrow, stringSeparators);
CDI_Order_Data__c[] cord = new CDI_Order_Data__c[row1.Length - 1];
for (int i = 1; i < row1.Length-1; i++)
{
string colstr = row1[i].ToString();
string[] allcols = Regex.Split(colstr, "\\|");
cord[i] = new CDI_Order_Data__c(); // Very important to create object
cord[i].Opportunity_Job_Order__c = stroppid;
cord[i].jobid__c = stroppid;
cord[i].order__c = allcols[0].ToString();
cord[i].firstname__c = allcols[1].ToString();
cord[i].name__c = allcols[2].ToString();
DateTime dtDate = Convert.ToDateTime(allcols[3]);
cord[i].Date__c = new DateTime(Convert.ToInt32(dtDate.Year), Convert.ToInt32(dtDate.Month), Convert.ToInt32(dtDate.Day), 0, 0, 0); //sforcedate(allcols[3]); //XMLstringToDate(allcols[3]);
cord[i].clientpo__c = allcols[4].ToString();
cord[i].billaddr1__c = allcols[5].ToString();
cord[i].billaddr2__c = allcols[6].ToString();
cord[i].billcity__c = allcols[7].ToString();
cord[i].billstate__c = allcols[8].ToString();
cord[i].billzip__c = allcols[9].ToString();
cord[i].phone__c = allcols[10].ToString();
cord[i].fax__c = allcols[11].ToString();
cord[i].email__c = allcols[12].ToString();
cord[i].contact__c = allcols[13].ToString();
cord[i].lastname__c = allcols[15].ToString();
cord[i].Rep__c = allcols[16].ToString();
cord[i].sidemark__c = allcols[17].ToString();
cord[i].account__c = allcols[18].ToString();
cord[i].item__c = allcols[19].ToString();
cord[i].kmatid__c = allcols[20].ToString();
cord[i].qty__c = Convert.ToDouble(allcols[21]);
cord[i].Description__c = allcols[22].ToString();
cord[i].price__c = Convert.ToDouble(allcols[23]);
cord[i].installation__c = allcols[24].ToString();
cord[i].freight__c = allcols[25].ToString();
cord[i].discount__c = Convert.ToDouble(allcols[26]);
cord[i].salestax__c = Convert.ToDouble(allcols[27]);
cord[i].taxcode__c = allcols[28].ToString();
}
try {
SaveResult[] saveResults = binding.create(cord);
}
catch (Exception ce)
{
Response.Write("Buld order update errror" +ce.Message.ToString());
Response.End();
}
if (str != null) str.Close();
}