Skip to content

Instantly share code, notes, and snippets.

@dev001hajipro
Created June 25, 2017 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dev001hajipro/f6f86818bbdce1cad226d05885e33422 to your computer and use it in GitHub Desktop.
Save dev001hajipro/f6f86818bbdce1cad226d05885e33422 to your computer and use it in GitHub Desktop.
Vehicles implement by OpenSiv3D from The Nature of Code.
#include "stdafx.h"
inline void _limit(Vec2& v, double a)
{
double b = v.lengthSq();
if (b > a*a) {
v /= sqrt((float)b);
}
v *= a;
}
inline double heading(Vec2 v)
{
return atan2(v.y, v.x);
}
inline double _Radians(double d)
{
// 180d = PI
// 360 = 2*PI
return 2 * M_PI / 360 * d;
}
struct Target {
Target()
:
position(Vec2(Random(0, Window::Width()), Random(0, Window::Height()))),
velocity(Vec2(Random(-2,2), Random(-2,2))),
acceleration(Vec2::Zero())
{
}
Target(double x, double y)
: position(Vec2(x, y)),
velocity(Vec2(Random(-2, 2), Random(-2, 2))),
acceleration(Vec2::Zero())
{
}
void update()
{
velocity += acceleration;
position += velocity;
acceleration *= 0.0;
if (position.x < 0) {
position.x = Window::Width();
}
else if (position.x > Window::Width()) {
position.x = 0;
}
if (position.y < 0) {
position.y = Window::Height();
}
else if (position.y > Window::Height()) {
position.y = 0;
}
}
void display()
{
Circle(position, r).draw(Color(170, 170, 170)).drawFrame(1, Palette::Black);
}
void run()
{
update();
display();
}
Vec2 position;
Vec2 velocity;
Vec2 acceleration;
double r = 10;
};
struct Vehicle {
Vehicle()
:
position(Vec2(Random(0, Window::Width()), Random(0, Window::Height()))),
velocity(Vec2::Zero()),
acceleration({ Random(-3,3),Random(-3,3) }),
mass(mass),
maxspeed(7.0),
maxforce(0.09)
{
}
Vehicle(double x, double y, double mass = 1)
:
position({ x, y }),
velocity(Vec2::Zero()),
acceleration({Random(-1,1),Random(-1,1)}),
mass(mass),
maxspeed(1.0),
maxforce(0.005)
{
}
Vec2 position;
Vec2 velocity;
Vec2 acceleration;
double maxspeed;
double maxforce;
double mass;
// F=AM, A=F/M
void applyForce(const Vec2& force)
{
auto v = force.xy();
acceleration += v;
}
void seek(Target target)
{
// 望む速度
Vec2 desired = target.position - position;
desired.normalize();
desired *= maxspeed;
// 操舵力=望む速度-現在の速度
Vec2 steer = desired - velocity;
// steer.limit
_limit(steer, maxforce);
applyForce(steer);
}
void update()
{
velocity += acceleration;
position += velocity;
acceleration *= 0;
if (position.x < 0) {
position.x = Window::Width();
}
else if (position.x > Window::Width()) {
position.x = 0;
}
if (position.y < 0) {
position.y = Window::Height();
}
else if (position.y > Window::Height()) {
position.y = 0;
}
}
void display()
{
auto theta = heading(velocity) + M_PI/2;
Triangle(position, 10).rotated(theta).draw().drawFrame(1.0, Palette::Black);
}
void run(Target target) {
seek(target);
update();
display();
}
};
void Main()
{
Window::Resize(640, 480);
Graphics::SetBackground(Color(240, 240, 240, 50));
const Font font(30);
Target target(100, 100);
std::vector<Vehicle> vs(50);
while (System::Update())
{
font(L"💖OpenSiv3D!🐣").drawAt(Vec2(150, 20), Palette::Black);
for (auto& v : vs) {
v.run(target);
}
target.run();
//font(Cursor::Pos()).draw(20, 400);
//Circle(Cursor::Pos(), 60).draw(ColorF(1, 0, 0, 0.5));
/*
Line(Vec2(0, 200), Vec2(400, 200)).draw(Palette::Black);
Line(Vec2(200, 0), Vec2(200, 400)).draw(Palette::Black);
Triangle(200, 200, 20)
//.setCentroid(Vec2(200, 200))
.rotated(_Radians(45))
.draw(Palette::Black);// .drawFrame(1.0, Palette::Black);
Line(Vec2(200, 200), Vec2(400, 0)).draw(Palette::Blue);
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment