Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[Irrlicht] Zwracanie klikniętego pola na mapie

Ostatnio zmodyfikowano 2024-02-22 20:11
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
» 2024-02-20 20:07:04
https://irrlicht.sourceforge.io/docu/example007.html
no właśnie nic z tego nie rozumiem, dlatego piszę tutaj na forum.
Nie rozumiem dlaczego scene::ISceneNode* selectedSceneNode jest zawsze nullptr

C/C++
position2d < s32 > mousePos = device->getCursorControl()->getPosition();
cout << "mouse position: " << mousePos.X << ", " << mousePos.Y << endl;

core::line3d < f32 > ray = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates( mousePos, cam );
cout << "ray start: " << ray.start.X << ", " << ray.start.Y << ", " << ray.start.Z << endl;
cout << "ray end: " << ray.end.X << ", " << ray.end.Y << ", " << ray.end.Z << endl;

core::vector3df intersection;
core::triangle3df hitTriangle;
scene::ISceneCollisionManager * collMan = smgr->getSceneCollisionManager();

if( collMan != nullptr )
   
 cout << "collMan ok " << endl;
else
   
 cout << "collMaan is nullptr " << endl;


scene::ISceneNode * selectedSceneNode = collMan->getSceneNodeAndCollisionPointFromRay( ray, intersection, hitTriangle );

if( selectedSceneNode != nullptr )
   
 cout << "collision node position: " << selectedSceneNode->getPosition().X << ", " << selectedSceneNode->getPosition().Y << endl;
else
   
 cout << "collision node is null\n";

cout << endl;
P-180858
tBane
Temat założony przez niniejszego użytkownika
» 2024-02-21 17:26:47
Być może to promień źle obliczam, bo sprawdziłem współrzędne promienia i wyszły takie jak poniżej. Wydaje mi się, że współrzędne końca promienia są niepoprawne - współrzędne są za wielkie, poza tym X jest ujemna( jakim cudem? )


mouse position: 1, 271
ray start: 0, 100, -40
ray end: -3263.76, -2028.19, 2142.52
collMan ok
collision node is null

mouse position: 3, 271
ray start: 0, 100, -40
ray end: -3251.65, -2028.19, 2142.52
collMan ok
collision node is null

gdy podmieniłem współrzędne końca na zerowe i tak nie wykryło żadnej kolizji, co może świadczyć o tym, że w sumie wszystko źle zaprogramowałem.

mouse position: 629, 304
ray start: 0, 100, -40
ray end: 0, 0, 0
collMan ok
collision node is null

mouse position: 629, 304
ray start: 0, 100, -40
ray end: 0, 0, 0
collMan ok
collision node is null
P-180859
tBane
Temat założony przez niniejszego użytkownika
» 2024-02-22 20:11:49
należało utworzyć Mesh dla każdego z kafelków oraz sprawdzać w update współrzędne kolizji z meshem

C/C++
class HexTile
    : public scene::ISceneNode
{
   
SMesh * mesh;
   
SMeshBuffer * meshBuffer;
   
public:
   
terrainType ttype;
   
float x, z;
   
   
HexTile( ISceneNode * parent, ISceneManager * smgr, s32 id, float, float );
   
virtual void OnRegisterSceneNode();
   
virtual void render();
   
virtual const aabbox3d < f32 > & getBoundingBox() const;
   
virtual u32 getMaterialCount() const;
   
virtual SMaterial & getMaterial( u32 i );
   
void setTexture( ITexture * );
   
void setTerrainType( terrainType );
   
SMesh * getMesh();
};

HexTile::HexTile( ISceneNode * parent, ISceneManager * smgr, s32 id, float x, float z )
    :
ISceneNode( parent, smgr, id )
{
   
ttype = terrainType::grass;
   
this->x = x;
   
this->z = z;
   
   
meshBuffer = new SMeshBuffer();
   
   
// SET THE INDICES
   
   
meshBuffer->Indices.set_used( 18 );
   
for( u32 i = 0; i < 18; ++i )
       
 meshBuffer->Indices[ i ] = hexIndices[ i ];
   
   
// SET THE VERTICES
   
SColor color = SColor( 255, 255, 255, 255 );
   
   
meshBuffer->Vertices.push_back( S3DVertex( 0, 0, 0, 0, 0, 0, color, 0, 0 ) );
   
for( int i = 0; i < 7; i++ )
   
{
       
//meshBuffer->Vertices.push_back(S3DVertex(hexVertices[2 * i] * 0.9f, 0.0f, hexVertices[2 * i + 1] * 0.9f, 0, 0, 0, color, texture_uv[2 * i], texture_uv[2 * i + 1]));
       
meshBuffer->Vertices.push_back( S3DVertex( hexVertices[ 2 * i ], 0.0f, hexVertices[ 2 * i + 1 ], 0, 0, 0, color, texture_uv[ 2 * i ], texture_uv[ 2 * i + 1 ] ) );
       
   
}
   
   
// SET THE BOUNDING BOX
   
meshBuffer->BoundingBox.reset( vector3df( hexVertices[ 0 ], 0, hexVertices[ 1 ] ) );
   
for( s32 i = 1; i < 7; ++i )
       
 meshBuffer->BoundingBox.addInternalPoint( vector3df( hexVertices[ 2 * i ], 0, hexVertices[ 2 * i + 1 ] ) );
   
   
   
// SET THE MATERIAL
   
meshBuffer->Material.Wireframe = false;
   
meshBuffer->Material.Lighting = false;
   
meshBuffer->Material.setTexture( 0, getTexture( ttype ) );
   
   
mesh = new SMesh();
   
mesh->addMeshBuffer( meshBuffer );
   
mesh->recalculateBoundingBox();
   
}

void HexTile::OnRegisterSceneNode()
{
   
if( IsVisible )
       
 SceneManager->registerNodeForRendering( this );
   
   
ISceneNode::OnRegisterSceneNode();
}

void HexTile::render()
{
   
IVideoDriver * driver = smgr->getVideoDriver();
   
driver->setMaterial( meshBuffer->Material );
   
driver->setTransform( video::ETS_WORLD, AbsoluteTransformation );
   
driver->drawMeshBuffer( meshBuffer );
   
}

const aabbox3d < f32 > & HexTile::getBoundingBox() const
{
   
return meshBuffer->BoundingBox;
}

u32 HexTile::getMaterialCount() const
{
   
return 1;
}

SMaterial & HexTile::getMaterial( u32 i )
{
   
return meshBuffer->Material;
}

void HexTile::setTexture( ITexture * tex )
{
   
meshBuffer->Material.setTexture( 0, tex );
}

void HexTile::setTerrainType( terrainType ttype )
{
   
this->ttype = ttype;
   
setTexture( getTexture( ttype ) );
}

SMesh * HexTile::getMesh()
{
   
return mesh;
}

C/C++
// GET THE MOUSE POSITION
position2d < s32 > mousePos = device->getCursorControl()->getPosition();
cout << "mouse position: " << mousePos.X << ", " << mousePos.Y << endl;

// CALCULATE THE RAY TO COLLISION
core::line3d < f32 > ray = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates( mousePos, cam );
cout << "ray start: " << ray.start.X << ", " << ray.start.Y << ", " << ray.start.Z << endl;
cout << "ray end: " << ray.end.X << ", " << ray.end.Y << ", " << ray.end.Z << endl;

// COLLISION
core::vector3df intersection;
core::triangle3df hitTriangle;
scene::ISceneCollisionManager * collMan = smgr->getSceneCollisionManager();

if( collMan != nullptr )
   
 cout << "collMan ok " << endl;
else
   
 cout << "collMaan is nullptr " << endl;


scene::ISceneNode * selectedSceneNode = collMan->getSceneNodeAndCollisionPointFromRay( ray, intersection, hitTriangle );

if( selectedSceneNode != nullptr )
{
   
cout << "collision node position: " << selectedSceneNode->getPosition().X << ", " << selectedSceneNode->getPosition().Z << endl;
}
else
   
 cout << "collision node is null\n";

cout << endl;
P-180863
1 « 2 »
Poprzednia strona Strona 2 z 2