今回は、ホーミング機能と同じ、タップしたところにプレイヤーが移動するように
タップした位置と、現在の位置の角度をもとめたいのですが、うまくいきません。
どこの計算がマズいでしょうか?
よろしくお願いします。
#include "DxLib.h"
#include <stdio.h>
#include "common.h"
int s_iPlayerImage = 0;
int s_iBackImage = 0;
static double s_iPlayerPosX = 500;
static double s_iPlayerPosY = 500;
void Player1Input();
void Player1Draw()
{
DrawGraph(s_iPlayerPosX, s_iPlayerPosY, s_iPlayerImage, TRUE );
}
double GetAngle(int x, int y, int x2, int y2)
{
double angle = 0;
double a, b, c;
a = b = c = 0.0;
a = abs(x2 - x);
b = abs(y2 - y);
c = sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
double si, co;
si = co = 0.0;
si = b / c;
co = a / c;
angle = 180 / (si * 3.1415);
DrawLine(x, y, x2, y2, GetColor(255, 255, 255));
DrawFormatString(100, 20, GetColor(255, 255, 255), "a:%f", a);
DrawFormatString(100, 40, GetColor(255, 255, 255), "b:%f", b);
DrawFormatString(100, 60, GetColor(255, 255, 255), "co:%f", c);
DrawFormatString(100, 80, GetColor(255, 255, 255), "si:%f", si);
DrawFormatString(100, 100, GetColor(255, 255, 255), "co:%f", co);
DrawFormatString(100, 120, GetColor(255, 255, 255), "angle:%f", angle);
return angle;
}
void Player1Input()
{
int PosX, PosY;
PosX = PosY = 0;
GetTouchInput(0, &PosX, &PosY, NULL, NULL);
if ((PosX > 0) && (PosY > 0))
{
double angle;
angle = GetAngle(s_iPlayerPosX, s_iPlayerPosY, PosX, PosY);
double X, Y, speed;
X = Y = 0;
speed = 1.0;
X = speed * cos(angle);
Y = speed * sin(angle);
s_iPlayerPosX += X;
s_iPlayerPosY += Y;
DrawFormatString(100, 140, GetColor(255, 255, 255), "X:%f", X);
DrawFormatString(100, 160, GetColor(255, 255, 255), "Y:%f", Y);
}
}
void Stage1Init()
{
s_iPlayerImage = LoadGraph("player.png");
s_iBackImage = LoadGraph("cosmo.jpg");
}
void Stage1MainLoop()
{
DrawGraph(0, 0, s_iBackImage, true);
Player1Input();
Player1Draw();
}