I have a class to move a cat which is the type Pawn. It seems to go way off in the Y direction and I don’t understand why.
Here is the source:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Cat_Alt.h"
// Sets default values
ACat_Alt::ACat_Alt()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
catSleep = false;
canMove = true;
minx = -500;
maxx = 500;
miny = -500;
maxy = 500;
bminx = -4700;
bmaxx = 4700;
bminy = -4700;
bmaxy = 4700;
catRotation = 270;
dirx[0] = 1;
dirx[1] = 0;
dirx[2] = -1;
dirx[3] = 0;
diry[0] = 0;
diry[1] = 1;
diry[2] = 0;
diry[3] = -1;
notMoved = true;
shouldSleep = false;
angchan = 20;
moveSpeed = 50;
catPos = FVector(-550, 0, 8);
}
// Called when the game starts or when spawned
void ACat_Alt::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACat_Alt::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
cycle.hours = hours;
cycle.minutes = minutes;
cycle.seconds = seconds;
if (wakeTime > 0) {
wakeTime -= DeltaTime;
if (wakeTime <= 0) {
wakeTime = 0;
canMove = true;
}
}
if (cycle.callEvent) {
if (cycle.wakeUp) {
canMove = false;
wakeTime = 3;
}
if (cycle.shouldSleep) shouldSleep = true;
if (cycle.mustSleep) catSleep = true;
cycle.callEvent = false;
}
if (canMove && !catSleep) {
move(DeltaTime);
}
}
// Called to bind functionality to input
void ACat_Alt::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void ACat_Alt::move(float delta) {
if (catRotation != toAngle) {
if (catRotation > toAngle) {
catRotation -= delta * angchan;
if (catRotation < toAngle) catRotation = toAngle;
}
else {
catRotation += delta * angchan;
if (catRotation > toAngle) catRotation = toAngle;
}
}
if (notMoved) setMovement();
float xmov = cos(catRotation * 0.01745329251994329576923690768489) * moveSpeed * delta;
float ymov = sin(catRotation * 0.01745329251994329576923690768489) * moveSpeed * delta;
catPos += FVector(xmov, ymov, 0);
if (sign(tox - catPos.X) != dsx || sign(toy - catPos.Y) != dsy) notMoved = true;
}
float ACat_Alt::getToRotation(float x, float y) {
notMoved = false;
dsx = sign(tox - catPos.X);
dsy = sign(toy - catPos.Y);
int i;
if (x == 0 || y == 0) {
for (i = 0; i < 4; i++) {
if (x != 0 && dsx == dirx[i]) break;
if (y != 0 && dsy == diry[i]) break;
}
return 90 * i;
}
int rot1 = 0;
int rot2 = 0;
for (i = 0; i < 4; i++) {
if (x != 0 && dsx == dirx[i]) rot1 = i;
if (y != 0 && dsy == diry[i]) rot2 = i;
}
int rota;
if (rot1 > rot2) {
rota = rot2;
}
else {
rota = rot1;
}
float hypot = pow(x * x + y * y, .5);
float relx = x / hypot;
float degMul = 57.295779513082320876798154814105;
float angle = 0;
switch (rota) {
case 0:
angle = asin(relx) * degMul;
break;
case 1:
angle = 180 - asin(relx) * degMul;
break;
case 2:
angle = 180 - asin(relx) * degMul;
break;
case 3:
angle = 360 + asin(relx) * degMul;
break;
}
return angle;
}
void ACat_Alt::setMovement() {
float dist = distFrom(catPos.X, catPos.Y);
float catx = catPos.X;
float caty = catPos.Y;
if (shouldSleep) {
}
else {
ranloc dest;
if (cycle.isHunting) {
}
else {
dest = getALocation();
toAngle = getToRotation(dest.locx - catx, dest.locy - caty);
}
}
}
bool ACat_Alt::inBounds(float x, float y) {
return (((x > bminx && x < bminx) || (x > maxx && x < bmaxx)) && ((x > bminy && x < bminy) || (x > maxy && x < bmaxy)));
}
float ACat_Alt::distFrom(float x, float y) {
float hypot = pow(x * x + y * y, .5);
float abx = abs(x / hypot);
float aby = abs(y / hypot);
if (abx == aby) {
return hypot - maxx * 1.4142135623730950488016887242097;
}
float lesval;
float grval;
if (abx > aby) {
lesval = aby;
grval = abx;
}
else {
lesval = abx;
grval = aby;
}
float relval = lesval / grval;
float adv = 0;
float ang = asin(relval);
for (float i = 2; i > pow(2, -24); i /= 2) if (asin((adv + i) / pow((adv + i) * (adv + 1) + 1, .5)) <= ang) adv += i;
return maxx * adv;
}
int ACat_Alt::sign(float val) {
if (val == 0) {
return 0;
}
else {
if (val > 0) {
return 1;
}
else {
return -1;
}
}
}
ranloc ACat_Alt::getALocation() {
ranloc retv;
bool notTested = true;
float posx, posy;
float catx = catPos.X;
float caty = catPos.Y;
bool signCh = false;
int tsign = 1;
float tx[4] = { -500, 500, -500, 500 };
float ty[4] = { -500, -500, 500, 500 };
while (notTested) {
posx = rand.generate() * 2000 - 1000;
posy = rand.generate() * 2000 - 1000;
if (!(posx > minx && posx < maxx && posy > miny && posy < maxy)) {
// (y2-y1)*x + (x1-x2)*y + (x2*y1-x1*y2)
for (int i = 0; i < 4; i++) {
if ((posy - caty) * tx[i] + (posx - catx) * ty[i] + (posx * caty - catx * posy) == 0) {
signCh = true;
break;
}
else {
if (sign((posy - caty) * tx[i] + (posx - catx) * ty[i] + (posx * caty - catx * posy)) != tsign && i > 0) {
signCh = true;
break;
}
else {
tsign = sign((posy - caty) * tx[i] + (posx - catx) * ty[i] + (posx * caty - catx * posy));
}
}
if (signCh) {
if ((posx > 500 && catx > 500) || (posx < -500 && catx < -500) || (posy > 500 && caty > 500) || (posy < -500 && caty < -500)) signCh = false;
}
if (signCh) notTested = false;
}
}
}
tox = posx;
toy = posy;
return retv;
}