aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/LookControl.cpp
blob: 1e0d5258433c0f844f4930141a07eed37a4d0f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "stdafx.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.phys.h"
#include "LookControl.h"

LookControl::LookControl(Mob *mob)
{
	yMax = xMax = 0.0f;
	hasWanted = false;
	wantedX = wantedY = wantedZ = 0.0;

	this->mob = mob;
}

void LookControl::setLookAt(shared_ptr<Entity> target, float yMax, float xMax)
{
	wantedX = target->x;
	if ( target->instanceof(eTYPE_LIVINGENTITY) ) wantedY = target->y + dynamic_pointer_cast<LivingEntity>(target)->getHeadHeight();
	else wantedY = (target->bb->y0 + target->bb->y1) / 2;
	wantedZ = target->z;
	this->yMax = yMax;
	this->xMax = xMax;
	hasWanted = true;
}

void LookControl::setLookAt(double x, double y, double z, float yMax, float xMax)
{
	wantedX = x;
	wantedY = y;
	wantedZ = z;
	this->yMax = yMax;
	this->xMax = xMax;
	hasWanted = true;
}

void LookControl::tick()
{
	mob->xRot = 0;

	if (hasWanted)
	{
		hasWanted = false;

		double xd = wantedX - mob->x;
		double yd = wantedY - (mob->y + mob->getHeadHeight());
		double zd = wantedZ - mob->z;
		double sd = sqrt(xd * xd + zd * zd);

		float yRotD = (float) (atan2(zd, xd) * 180 / PI) - 90;
		float xRotD = (float) -(atan2(yd, sd) * 180 / PI);
		mob->xRot = rotlerp(mob->xRot, xRotD, xMax);
		mob->yHeadRot = rotlerp(mob->yHeadRot, yRotD, yMax);
	}
	else
	{		
		mob->yHeadRot = rotlerp(mob->yHeadRot, mob->yBodyRot, 10);
	}

	float headDiffBody = Mth::wrapDegrees(mob->yHeadRot - mob->yBodyRot);

	if (!mob->getNavigation()->isDone())
	{
		// head clamped to body
		if (headDiffBody < -75) mob->yHeadRot = mob->yBodyRot - 75;
		if (headDiffBody > 75) mob->yHeadRot = mob->yBodyRot + 75;
	}
}

float LookControl::rotlerp(float a, float b, float max)
{
	float diff = b - a;
	while (diff < -180)
		diff += 360;
	while (diff >= 180)
		diff -= 360;
	if (diff > max)
	{
		diff = max;
	}
	if (diff < -max)
	{
		diff = -max;
	}
	return a + diff;
}

bool LookControl::isHasWanted()
{
	return hasWanted;
}

float LookControl::getYMax()
{
	return yMax;
}

float LookControl::getXMax()
{
	return xMax;
}

double LookControl::getWantedX()
{
	return wantedX;
}

double LookControl::getWantedY()
{
	return wantedY;
}

double LookControl::getWantedZ()
{
	return wantedZ;
}