simulation update

This commit is contained in:
Anselme 2015-09-22 10:46:28 +02:00
parent 7cd9805539
commit d956b8f2b3

View File

@ -3,9 +3,12 @@
#include <ctime>
#include <cstdlib>
#define PI 3.1416
#define MAX_ANGULAR_SPEED 1
#define MAX_SPEED 3
#define PI 3.1416f
#define MAX_ANGULAR_SPEED 0.05f
#define MAX_FORWARD_SPEED 1.f
#define MAX_REVERSE_SPEED 0.5f
#define MAX_ACCELERATION 0.02f
#define MAX_DECELERATION (MAX_ACCELERATION*2)
Simulation::Simulation()
{
@ -23,9 +26,9 @@ std::vector<InitClientPacket>* Simulation::getWorldData()
void Simulation::addClient()
{
InitClientPacket info;
info.x = std::rand()%50;
info.y = std::rand()%50;
info.angle = ((float)(std::rand()%360)) / PI;
info.x = std::rand()%30;
info.y = std::rand()%30;
info.angle = ((float)(std::rand()%360))*PI/180.f;
info.vforward = 0;
info.vangle = 0;
worldData.push_back(info);
@ -43,18 +46,40 @@ void Simulation::removeClient(int i)
void Simulation::clientInput(int i, const DeltaClientPacket &delta)
{
InitClientPacket *info = &(worldData[i]);
info->vangle += delta.dvangle;
info->vforward += delta.dvforward;
if(info->vforward>3)
info->vforward = MAX_SPEED;
if(info->vforward<0)
info->vforward = 0;
float speed = info->vforward;
float maxAngularSpeed = speed <= 1 ? speed*MAX_ANGULAR_SPEED : MAX_ANGULAR_SPEED/speed;
float dvforward;
// handling speed
if(delta.acceleration > 0.01f) // if accelerating
{
if(fabs(delta.acceleration) < 1)
dvforward = delta.acceleration*MAX_ACCELERATION;
else
dvforward = info->vforward >= 0 ? MAX_ACCELERATION : -MAX_ACCELERATION;
}
else if(delta.acceleration < -0.01f)
{
if(fabs(delta.acceleration) < 1)
dvforward = delta.acceleration*MAX_DECELERATION;
else
dvforward = info->vforward >= 0 ? -MAX_DECELERATION : MAX_DECELERATION;
}
else // not accelerating
dvforward = -info->vforward/8;
info->vforward += dvforward;
if(info->vforward>MAX_FORWARD_SPEED)
info->vforward = MAX_FORWARD_SPEED;
if(info->vforward<-MAX_REVERSE_SPEED)
info->vforward = -MAX_REVERSE_SPEED;
// handling angle
info->vangle = delta.turning;
if(abs(info->vangle) > 0.01f)
{
float x = 2*PI*fabs(info->vforward)/MAX_FORWARD_SPEED;
float maxAngularSpeed = (x/5 + 1 + cos(PI + x))*MAX_ANGULAR_SPEED;
if(info->vangle > maxAngularSpeed)
info->vangle = maxAngularSpeed;
if(info->vangle < -maxAngularSpeed)
info->vangle = -maxAngularSpeed;
}
}
void Simulation::update()