|
|
|
#define OLC_PGE_APPLICATION
|
|
|
|
#include "pixelGameEngine.h"
|
|
|
|
|
|
|
|
#if defined(__EMSCRIPTEN__)
|
|
|
|
#include <emscripten.h>
|
|
|
|
#define FILE_RESOLVE(url, file) emscripten_wget(url, file); emscripten_sleep(0)
|
|
|
|
#else
|
|
|
|
#define FILE_RESOLVE(url, file)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace olc;
|
|
|
|
|
|
|
|
float angle_difference(float angle_1, float angle_2)
|
|
|
|
{
|
|
|
|
angle_1=(angle_1<0)?angle_1+360:fmod(angle_1,360);
|
|
|
|
angle_2=(angle_2<0)?angle_2+360:fmod(angle_2,360);
|
|
|
|
float angle_diff = angle_1 - angle_2;
|
|
|
|
|
|
|
|
if (angle_diff > 180)
|
|
|
|
angle_diff -= 360;
|
|
|
|
else if (angle_diff < -180)
|
|
|
|
angle_diff += 360;
|
|
|
|
|
|
|
|
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{
|
|
|
|
public:
|
|
|
|
float facing_dir;
|
|
|
|
float rotation_spd;
|
|
|
|
vd2d pos;
|
|
|
|
vd2d size;
|
|
|
|
Object(vd2d pos, vd2d size, float rotation_spd=100*(3.14159f/180.f), float facing_dir=0)
|
|
|
|
:pos(pos),size(size),rotation_spd(rotation_spd),facing_dir(facing_dir) {}
|
|
|
|
void draw(PixelGameEngine*pge) {
|
|
|
|
pge->DrawCircle(pos,size.x);
|
|
|
|
pge->DrawLine(pos,{(int)(pos.x+4*cos(facing_dir)),(int)(pos.y+4*sin(facing_dir))});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Override base class with your custom functionality
|
|
|
|
class Example : public olc::PixelGameEngine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Example()
|
|
|
|
{
|
|
|
|
// Name your application
|
|
|
|
sAppName = "Rotation";
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
Object*testRotationObj;
|
|
|
|
bool OnUserCreate() override
|
|
|
|
{
|
|
|
|
testRotationObj = new Object({(double)ScreenWidth()/2,(double)ScreenHeight()/2},{8,8});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override
|
|
|
|
{
|
|
|
|
Clear(BLACK);
|
|
|
|
float diffAng = atan2fmod(testRotationObj->pos,GetMousePos());
|
|
|
|
float rotAngle=angle_difference((180.f/3.14159f)*testRotationObj->facing_dir,(180.f/3.14159f)*diffAng);
|
|
|
|
//std::cout<<diffAng<<std::endl;
|
|
|
|
//std::cout<<" "<<rotAngle<<std::endl;
|
|
|
|
if (abs(rotAngle)>testRotationObj->rotation_spd*fElapsedTime*(180.f/3.14159f)) {
|
|
|
|
if (rotAngle>0) {
|
|
|
|
testRotationObj->facing_dir+=testRotationObj->rotation_spd*fElapsedTime;
|
|
|
|
} else {
|
|
|
|
testRotationObj->facing_dir-=testRotationObj->rotation_spd*fElapsedTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testRotationObj->draw(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
Example demo;
|
|
|
|
if (demo.Construct(256, 240, 2, 2))
|
|
|
|
demo.Start();
|
|
|
|
return 0;
|
|
|
|
}
|