Skip to content

Commit

Permalink
Network now work without stutter!
Browse files Browse the repository at this point in the history
  • Loading branch information
anirul committed Mar 16, 2024
1 parent 31e0927 commit 7e6f87d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
15 changes: 11 additions & 4 deletions Client/state_play.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ namespace darwin::state {
darwin_client_->GetServerTime());
server_time = darwin_client_->GetServerTime();
}
double now =
std::chrono::duration<double>(
std::chrono::system_clock::now().time_since_epoch()).count();
auto character_name = darwin_client_->GetCharacterName();
auto character =
world_simulator_.GetCharacterByName(character_name);
Expand All @@ -214,19 +217,23 @@ namespace darwin::state {
// Send a report movement to the server.
darwin_client_->ReportHit(name);
}
// Get the delta time.
double delta_time = now - world_simulator_.GetLastServerUpdateTime();
// Get the close uniforms from the world simulator.
auto uniforms = world_simulator_.GetCloseUniforms(
Normalize(character.physic().position()));
Normalize(character.physic().position()), delta_time);
assert(uniforms.spheres.size() == uniforms.colors.size());
auto& program = GetProgram();
bool is_character_valid = false;
// Update Uniforms.
program.Use();
UpdateUniformSpheres(program, uniforms);
if (character.name() != character_name) {
logger_->warn("Character {} not found", character_name);
if (character.name() != character_name)
{
logger_->info("Character {} not found", character_name);
}
else {
else
{
is_character_valid = true;
UpdateUniformCamera(program, character);
}
Expand Down
28 changes: 25 additions & 3 deletions Common/world_simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace darwin {
characters_ = characters;
time_ = time;
started_ = true;
last_server_update_time_ =
std::chrono::duration<double>(
std::chrono::system_clock::now().time_since_epoch()).count();
}

std::vector<proto::Element> WorldSimulator::GetGForceElementsLocked() {
Expand Down Expand Up @@ -125,7 +128,8 @@ namespace darwin {
}

UniformEnum WorldSimulator::GetCloseUniforms(
const proto::Vector3& normal) const
const proto::Vector3& normal,
double delta_time) const
{
std::lock_guard l(mutex_);
UniformEnum uniform_enum;
Expand All @@ -141,7 +145,14 @@ namespace darwin {
for (const auto& character : characters_) {
if (character.status_enum() == proto::STATUS_DEAD) continue;
if (IsClose(normal, Normalize(character.physic().position()))) {
uniform_enum.spheres.push_back(GetSphere(character.physic()));
if (character.name() == GetUserName()) {
uniform_enum.spheres.push_back(
GetSphere(character.physic()));
}
else {
uniform_enum.spheres.push_back(
GetSphereUdpate(character.physic(), delta_time));
}
uniform_enum.colors.push_back(GetColor(character));
}
}
Expand All @@ -156,6 +167,17 @@ namespace darwin {
physic.radius());
}

glm::vec4 WorldSimulator::GetSphereUdpate(
const proto::Physic& physic,
double delta_time) const
{
return glm::vec4(
physic.position().x() + physic.position_dt().x() * delta_time,
physic.position().y() + physic.position_dt().y() * delta_time,
physic.position().z() + physic.position_dt().z() * delta_time,
physic.radius());
}

glm::vec4 WorldSimulator::GetColor(const proto::Element& element) const {
return glm::vec4(
element.color().x(),
Expand All @@ -171,7 +193,7 @@ namespace darwin {
character.color().x(),
character.color().y(),
character.color().z(),
1.0);
2.0);
}

proto::Character WorldSimulator::GetCharacterByName(
Expand Down
12 changes: 11 additions & 1 deletion Common/world_simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ namespace darwin {
double time);
void UpdateTime();
UniformEnum GetUniforms() const;
UniformEnum GetCloseUniforms(const proto::Vector3& normal) const;
UniformEnum GetCloseUniforms(
const proto::Vector3& normal,
double delta_time) const;
proto::Character GetCharacterByName(const std::string& name) const;
void SetCharacter(const proto::Character& character);
std::string GetPotentialHit(const proto::Character& character) const;
proto::Physic GetPlanet() const;
bool HasCharacter(const std::string& name) const;

public:
double GetLastServerUpdateTime() const {
std::lock_guard l(mutex_);
return last_server_update_time_;
}
void SetPlayerParameter(const proto::PlayerParameter& parameter) {
std::lock_guard l(mutex_);
player_parameter_ = parameter;
Expand Down Expand Up @@ -64,6 +70,9 @@ namespace darwin {
const proto::Vector3& normal,
const proto::Vector3& position) const;
glm::vec4 GetSphere(const proto::Physic& physic) const;
glm::vec4 GetSphereUdpate(
const proto::Physic& physic,
double delta_time) const;
glm::vec4 GetColor(const proto::Element& element) const;
glm::vec4 GetColor(const proto::Character& character) const;
std::vector<proto::Element> GetGForceElementsLocked();
Expand All @@ -78,6 +87,7 @@ namespace darwin {
std::vector<proto::Element> elements_;
std::vector<proto::Character> characters_;
double time_;
double last_server_update_time_;
std::chrono::time_point<std::chrono::high_resolution_clock> last_time_;
proto::PlayerParameter player_parameter_;
};
Expand Down

0 comments on commit 7e6f87d

Please sign in to comment.