if "position" in changed:
newposition = client.extrapolate(car.position.copy(),
data.get("position", (0,0,0)),
obj = car)
Reuse.Move(car, newposition)
def Move(object, position, times=1):
object.applyMovement( ( mathutils.Vector(position) - object.position ) * times )
def extrapolate(original, value, obj=None, objKey="position"):
if obj:
try:
original = obj["net-finals"][objKey].copy()
except:
pass
original = mathutils.Vector(original)
value = mathutils.Vector( value )
offset = time.time() - queue.get("time", time.time())
frame = queue.get("time", time.time()) - queue.get("tick", time.time())
diff = value - original
withext = diff * ( frame + offset )
final = original + withext + diff
if obj:
if "net-exp" not in obj:
obj["net-exp"] = {}
obj["net-finals"] = {}
obj["net-originals"] = {}
obj["net-exp"][objKey] = ( diff + withext ) * ( ( 1 / ( frame + offset ) ) / bge.logic.getAverageFrameRate() )
obj["net-exp"]["time"] = int(round(( frame + offset ) * bge.logic.getAverageFrameRate()))
obj["net-exp"]["time-full"] = obj["net-exp"]["time"]
obj["net-finals"][objKey] = final
obj["net-originals"][objKey] = original
return final
def PhysicsUpdate(car):
scene, car = GetSceneAndCar(car)
toFPS = Opt.ToFPS()
carD = Opt.FastDict(car)
initfirst = False
if not getCarPhysics(car):
InitCarPhysics(car)
initfirst = True
x = 0
z = -DownForce(car)
df = DragForce(car)
car.applyForce(df, False)
car.applyForce([0, 0, z], not car.get("underwater", False))
dirt = False
ground = OnGround(car)
if "ground" in str(ground).lower():
dirt = True
s = 50 * abs(car.localLinearVelocity.y)
vec = (random.uniform(-s, s),
random.uniform(-s, s),
0)
car.applyTorque(vec, True)
if car.get("active") and ( dirt or ( abs(car.localLinearVelocity.x) > 2 and ground ) ) :
speed = math.dist((0,0,0), car.localLinearVelocity)
if speed > 10:
Input.VibrateJoystick(speed/100, -car.localLinearVelocity.x)
if car.get("active") or Opt.GoodFPS(str(id(car))+"_physics") or initfirst:
GetPhysicsForces(car)
SwingDoors(car)
for n, wheel in enumerate(car["specs"]["wheels"]):
wheelObject = car["wheels"][n]
health = wheelObject["health"] ** 2
suspension = wheelObject["suspension"] * health
hbf = 1.0
if car.get("handbraking"):
if not wheel.get("front"): hbf = 0.5
else: hbf = 0.6
= car.getVectTo(wheelObject.position)[2][2]
k = car["specs"]["suspentionDamping"]
suspensionDamping = 2 * k * math.sqrt(suspension)
getCarPhysics(car).setSuspensionStiffness(suspension, n)
getCarPhysics(car).setSuspensionDamping(suspensionDamping, n)
DFS = car["specs"].get("driftSpeed", 50)
DRIFTINESS = car["specs"].get("driftiness", 1)
DF = max(0, min(1, abs(car.worldLinearVelocity.y) / DFS ) )
if car.get("breaking"): DF = 1.0
try:
DFT = max(0, min(1, (math.dist((0,0,0),car.localAngularVelocity)
/ car["specs"].get("maxAngularVelocity", 1.5)) / DF ) )
except: DFT = 0.0
DF *= DFT
grip = car["specs"]["grip"]
if (car.localAngularVelocity.z > 0) != (car.localLinearVelocity.x > 0):
dgrip = wheel.get("drift_grip", car["specs"]["grip"])
grip = (grip*(1-DF)) + (dgrip * DF)
car["DriftFactor"] = DF
roll = car["specs"]["roll"]
if dirt:
grip *= 0.4
roll += 0.8
grip = grip*math.sqrt(car.mass)*hbf
getCarPhysics(car).setTyreFriction(grip, n)
getCarPhysics(car).setRollInfluence(roll, n)
ApplyEngine(car)
car["accelerating"] = False
car["braking"] = False
def ApplyEngine(car):
carD = Opt.FastDict(car)
toFPS = Opt.ToFPS()
hp = HorsePower(car)
force = HorsePowerToWatts(hp) / 100
grip = car["specs"]["grip"]
notresistance = EngineResistance(car)
if car.get("gear",0) == 0:
notresistance = 1
FO = force * notresistance / GetGearRatio(car) / grip
T = time.time()
if force > 0:
D = max(0, FO - carD["prevF"])
else:
D = min(0, FO - carD["prevF"])
if car.get("accelerating") and car["specs"].get("type") not in ("skating vehicle", "kart"):
try:
calc = ( 1 / ( D * ( ( T - carD["prevT"] ) ** 2 ) ) )
F = FO + calc
except:
F = FO
else:
F = FO
carD["prevT"] = time.time()
carD["prevF"] = FO
for n, wheel in enumerate(car["specs"]["wheels"]):
wheelObject = car["wheels"][n]
health = (wheelObject["health"] + carD["health"]) / 2
rightwheel = ( wheel["front"] and force > 0 ) or ( not wheel["front"] and force < 0 )
if rightwheel and ( not car.get("braking") or car.get("drifting") ):
getCarPhysics(car).applyEngineForce(F * health, n)
else:
getCarPhysics(car).applyEngineForce(0, n)
if not car.get("accelerating") and not NetworkControlled(car):
carD["rpm"] *= 0.98 * EngineResistance(car)
( 1 / ( D * ( ( T - carD["prevT"] ) ** 2 ) ) )