mirror of
https://github.com/devZoGok/PlanetFleet.git
synced 2026-08-26 19:43:29 +00:00
screen to space position transformation
This commit is contained in:
+12
-62
@@ -3,6 +3,7 @@
|
||||
#include <model.h>
|
||||
#include <material.h>
|
||||
#include <quad.h>
|
||||
#include <ray.h>
|
||||
|
||||
#include <stateManager.h>
|
||||
|
||||
@@ -475,56 +476,16 @@ namespace battleship{
|
||||
}
|
||||
|
||||
void ActiveGameState::addPos() {
|
||||
/*
|
||||
Vector3 camPos = cam->getPosition();
|
||||
Vector3 topLeft=cam->getViewFrustum()->getNearLeftUp();
|
||||
Vector3 topRight=cam->getViewFrustum()->getNearRightUp();
|
||||
Vector3 bottomLeft=cam->getViewFrustum()->getNearLeftDown();
|
||||
Camera *cam = Root::getSingleton()->getCamera();
|
||||
Vector3 camPos = cam->getPosition();
|
||||
Vector3 endPos = screenToSpace(getCursorPos());
|
||||
|
||||
GameManager *gm = GameManager::getSingleton();
|
||||
Vector2 mousePos = gm->getDevice()->getCursorControl()->getPosition();
|
||||
Vector3 p = topLeft;
|
||||
p+=(topRight-topLeft)*((float)mousePos.X / gm->getWidth());
|
||||
p+=(bottomLeft-topLeft)*((float)mousePos.Y / gm->getHeight());
|
||||
vector3df orderDir=(p-camPos).normalize();
|
||||
|
||||
ISceneManager *smgr = gm->getDevice()->getSceneManager();
|
||||
ISceneCollisionManager *collMan = smgr->getSceneCollisionManager();
|
||||
line3d<float> ray;
|
||||
ray.start = camPos;
|
||||
ray.end = camPos + orderDir * map->getSize().X * 1000;
|
||||
triangle3df t;
|
||||
vector3df collPoint;
|
||||
ISceneNode *collNode = collMan->getSceneNodeAndCollisionPointFromRay(ray, collPoint, t, 0, 0);
|
||||
vector<Ray::CollisionResult> results;
|
||||
Ray::retrieveCollisions(camPos, (endPos - camPos).norm(), map->getWaterNode(), results);
|
||||
Ray::sortResults(results);
|
||||
|
||||
if (collNode) {
|
||||
for (Player *p : players)
|
||||
for (int i = 0; i < p->getNumberOfUnits(); i++)
|
||||
if (collNode == p->getUnit(i)->getNode()) {
|
||||
orderPos.push_back(p->getUnit(i)->getPosPtr());
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(orderDir.Y>0)
|
||||
orderDir.Y*=-1;
|
||||
|
||||
float angle = getAngleBetween(vector3df(0, -1, 0), orderDir);
|
||||
float hyp = camPos.Y / cos(angle);
|
||||
vector3df *v=new vector3df(orderDir * hyp + camPos);
|
||||
(*v).Y=0;
|
||||
orderPos.push_back(v);
|
||||
}
|
||||
if (!selectingPatrolPoints) {
|
||||
Order::TYPE t = Order::TYPE::MOVE;
|
||||
|
||||
if (selectingGuidedMissileTarget)
|
||||
t = Order::TYPE::LAUNCH;
|
||||
else if (controlPressed)
|
||||
t = Order::TYPE::ATTACK;
|
||||
|
||||
issueOrder(t, orderPos, false);
|
||||
}
|
||||
*/
|
||||
if(!results.empty())
|
||||
issueOrder(Order::TYPE::MOVE, vector<Order::Target>{Order::Target(false, new Vector3(results[0].pos))}, false);
|
||||
}
|
||||
|
||||
void ActiveGameState::onAction(int bind, bool isPressed) {
|
||||
@@ -538,26 +499,15 @@ namespace battleship{
|
||||
if (isPressed) {
|
||||
clickPoint = getCursorPos();
|
||||
|
||||
if (!selectedUnits.empty()){
|
||||
Vector3 *p = new Vector3;
|
||||
*p = Vector3(10, 0, 10);
|
||||
|
||||
Order::Target t;
|
||||
t.unit = false;
|
||||
t.pos = p;
|
||||
|
||||
issueOrder(Order::TYPE::MOVE, vector<Order::Target>{t}, false);
|
||||
|
||||
addPos();
|
||||
}
|
||||
if (!selectedUnits.empty())
|
||||
addPos();
|
||||
}
|
||||
|
||||
break;
|
||||
case Bind::DESELECT:
|
||||
if (isPressed) {
|
||||
if (isPressed)
|
||||
while(!selectedUnits.empty())
|
||||
selectedUnits.pop_back();
|
||||
}
|
||||
break;
|
||||
case Bind::TOGGLE_SUB:
|
||||
if (isPressed) {
|
||||
|
||||
@@ -24,10 +24,7 @@ namespace battleship{
|
||||
if (orders.size() == 0) {
|
||||
Order o;
|
||||
o.type = Order::TYPE::MOVE;
|
||||
Order::Target t;
|
||||
t.unit = false;
|
||||
t.pos = new Vector3(lp);
|
||||
o.targets.push_back(t);
|
||||
o.targets.push_back(Order::Target(false, new Vector3(lp)));
|
||||
setOrder(o);
|
||||
}
|
||||
|
||||
|
||||
@@ -44,10 +44,10 @@ namespace battleship{
|
||||
Texture *t = new Texture(fr, 1, false);
|
||||
mat->addTexUniform("textures[0]", t, true);
|
||||
quad->setMaterial(mat);
|
||||
Node *water = new Node();
|
||||
water->attachMesh(quad);
|
||||
root->getRootNode()->attachChild(water);
|
||||
water->setOrientation(Quaternion(-1.57, Vector3::VEC_I));
|
||||
waterNode = new Node();
|
||||
waterNode->attachMesh(quad);
|
||||
root->getRootNode()->attachChild(waterNode);
|
||||
waterNode->setOrientation(Quaternion(-1.57, Vector3::VEC_I));
|
||||
|
||||
Camera *cam = root->getCamera();
|
||||
cam->setPosition(Vector3(1, 1, 1) * 10);
|
||||
|
||||
@@ -13,13 +13,10 @@ namespace battleship{
|
||||
void load();
|
||||
void unload();
|
||||
inline vb01::Vector2 getSize(){return size;}
|
||||
inline vb01::Node* getWaterNode(){return waterNode;}
|
||||
private:
|
||||
vb01::Vector2 size;
|
||||
/*
|
||||
irr::scene::ILightSceneNode *sunLight;
|
||||
irr::scene::IAnimatedMesh *waterMesh;
|
||||
irr::scene::ISceneNode *waterNode;
|
||||
*/
|
||||
vb01::Node *waterNode = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,16 @@ namespace battleship{
|
||||
|
||||
struct Order {
|
||||
enum class TYPE {ATTACK, MOVE, PATROL, LAUNCH};
|
||||
struct Target{bool unit; vb01::Vector3 *pos = nullptr;};
|
||||
struct Target{
|
||||
bool unit;
|
||||
vb01::Vector3 *pos = nullptr;
|
||||
|
||||
Target(bool unit, vb01::Vector3 *pos){
|
||||
this->unit = unit;
|
||||
this->pos = pos;
|
||||
}
|
||||
};
|
||||
|
||||
TYPE type;
|
||||
vb01::LineRenderer::Line line;
|
||||
std::vector<Target> targets;
|
||||
|
||||
@@ -206,8 +206,6 @@ namespace battleship{
|
||||
}
|
||||
|
||||
Vector2 spaceToScreen(Vector3 pos){
|
||||
mat4 model = translate(mat4(1.f), vec3(pos.x, pos.y, pos.z));
|
||||
|
||||
Root *root = Root::getSingleton();
|
||||
Camera *cam = root->getCamera();
|
||||
Vector3 dir = cam->getDirection(), up = cam->getUp();
|
||||
@@ -217,14 +215,13 @@ namespace battleship{
|
||||
float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(), nearPlane = cam->getNearPlane(), farPlane = cam->getFarPlane();
|
||||
mat4 proj = perspective(radians(fov), width / height, nearPlane, farPlane);
|
||||
|
||||
vec4 ndcPos = proj * view * model * vec4(0, 0, 0, 1);
|
||||
vec4 ndcPos = proj * view * vec4(pos.x, pos.y, pos.z, 1);
|
||||
ndcPos.x /= ndcPos.w;
|
||||
ndcPos.y /= ndcPos.w;
|
||||
return Vector2(0.5 * width * (1 + ndcPos.x), 0.5 * height * (1 - ndcPos.y));
|
||||
}
|
||||
|
||||
Vector3 screenToSpace(Vector2 pos){
|
||||
/*
|
||||
Root *root = Root::getSingleton();
|
||||
Camera *cam = root->getCamera();
|
||||
Vector3 dir = cam->getDirection(), up = cam->getUp();
|
||||
@@ -234,12 +231,12 @@ namespace battleship{
|
||||
float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(), nearPlane = cam->getNearPlane(), farPlane = cam->getFarPlane();
|
||||
mat4 proj = perspective(radians(fov), width / height, nearPlane, farPlane);
|
||||
|
||||
vec4 ndcPos = proj * view * model * vec4(0, 0, 0, 1);
|
||||
ndcPos.x /= ndcPos.w;
|
||||
ndcPos.y /= ndcPos.w;
|
||||
screenPos = Vector2(0.5 * width * (1 + ndcPos.x), 0.5 * height * (1 - ndcPos.y));
|
||||
return screenPos;
|
||||
*/
|
||||
mat4 mat = proj * view;
|
||||
float w = (mat * vec4(0, 0, 0, 1)).w;
|
||||
vec4 ndcPos = vec4(pos.x * 2.0 / width - 1.0, pos.y * -(2.0 / height) + 1.0, -1, 1);
|
||||
vec4 spacePos = inverse(mat) * ndcPos;
|
||||
spacePos = vec4(spacePos.x / spacePos.w, spacePos.y / spacePos.w, spacePos.z / spacePos.w, spacePos.w);
|
||||
return Vector3(spacePos.x, spacePos.y, spacePos.z);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user