core vector3df ChunkManager getTerraPos core line3d f32 line smgr- get

 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
core::vector3df ChunkManager::getTerraPos() {
core::line3d<f32> line = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(
device->getCursorControl()->getPosition(), smgr->getActiveCamera ());
core::vector3df v1 = smgr->getActiveCamera ()->getAbsolutePosition(), v2 = smgr->getActiveCamera ()->getTarget();
//Переходим в систему отсчета камеры
core::vector3df diff = v2 - v1;
f32 dx = v2.X - v1.X;
f32 dz = v2.Z - v1.Z;
f32 x0 = v1.X;
f32 z0 = v1.Z;
f32 l = sqrt(dx*dx + dz*dz);
f32 angle;
if(dz == 0) {
if(dx>0)
angle = core::PI/2;
else
angle = -core::PI/2;
}
else
angle = atan(dx/dz);
std::cout<<"In loop x0:"<<x0<<" z0: "<<z0<< " angle "<< angle<<std::endl;
std::cout<<"In loop l:"<<l<<std::endl;
for(s32 i = 0; i<=l; i+=5) {
int x = i*cos(angle)+x0;
int z = i*sin(angle)+z0;
int xx = x%chunkSize;
int zz = z%chunkSize;
if(xx<0) xx+=chunkSize;
if(zz<0) zz+=chunkSize;
Chunk *c = getChunkByIJ(getIJPairByCoords( core::vector3df(x, 0, z)));
if(c==NULL) continue;
ShTlTerrainSceneNode* terra = c->getTerra();
if(terra==NULL) continue;
core::vector3df currentPoint = core::vector3df(x, terra->getHeight(xx, zz),z);
core::vector3df nearestPoint = line.getClosestPoint(currentPoint);
if( nearestPoint.getDistanceFrom(currentPoint) <= 5.0 )
return currentPoint;
}
std::cout<<"Return"<<std::endl;
return core::vector3df(0,0,0);
}