Radians instead. And fixes.

master
sigonasr2 2 years ago
parent f47d7703a1
commit 71f10d012a
  1. 34
      main.cpp

@ -10,30 +10,22 @@
using namespace olc; using namespace olc;
const float PI = 3.14159f;
float angle_difference(float angle_1, float angle_2) float angle_difference(float angle_1, float angle_2)
{ {
angle_1=(angle_1<0)?angle_1+360:fmod(angle_1,360); angle_1=fmod(angle_1,2*PI);
angle_2=(angle_2<0)?angle_2+360:fmod(angle_2,360); angle_2=fmod(angle_2,2*PI);
float angle_diff = angle_1 - angle_2; float angle_diff = angle_1 - angle_2;
if (angle_diff > 180) if (angle_diff > PI)
angle_diff -= 360; angle_diff -= 2*PI;
else if (angle_diff < -180) else if (angle_diff < -PI)
angle_diff += 360; angle_diff += 2*PI;
return -angle_diff; return -angle_diff;
} }
//Points an angle from point1 to point2, returning an angle between 0-2*PI.
// (atan2 returns -PI to PI)
float atan2fmod(vd2d point1,vd2d point2){
float angle = atan2f(point2.y-point1.y,point2.x-point1.x);
if (angle<0) {
angle+=2*3.14159f;
}
return angle;
}
class Object{ class Object{
public: public:
float facing_dir; float facing_dir;
@ -69,17 +61,17 @@ public:
bool OnUserUpdate(float fElapsedTime) override bool OnUserUpdate(float fElapsedTime) override
{ {
Clear(BLACK); Clear(BLACK);
float diffAng = atan2fmod(testRotationObj->pos,GetMousePos()); float diffAng = atan2f(GetMouseY()-testRotationObj->pos.y,GetMouseX()-testRotationObj->pos.x);
float rotAngle=angle_difference((180.f/3.14159f)*testRotationObj->facing_dir,(180.f/3.14159f)*diffAng); float rotAngle=angle_difference(testRotationObj->facing_dir,diffAng);
//std::cout<<diffAng<<std::endl;
//std::cout<<" "<<rotAngle<<std::endl; if (abs(rotAngle)>testRotationObj->rotation_spd*fElapsedTime) { //Prevents stuttering. Wait for an entire rotation speed amount to occur
if (abs(rotAngle)>testRotationObj->rotation_spd*fElapsedTime*(180.f/3.14159f)) {
if (rotAngle>0) { if (rotAngle>0) {
testRotationObj->facing_dir+=testRotationObj->rotation_spd*fElapsedTime; testRotationObj->facing_dir+=testRotationObj->rotation_spd*fElapsedTime;
} else { } else {
testRotationObj->facing_dir-=testRotationObj->rotation_spd*fElapsedTime; testRotationObj->facing_dir-=testRotationObj->rotation_spd*fElapsedTime;
} }
} }
testRotationObj->draw(this); testRotationObj->draw(this);
return true; return true;
} }

Loading…
Cancel
Save