This is a mobile optimized page that loads fast, if you want to load the real page, click this text.

Вопрос Получение точки в мире, на которую смотрит камера

E-Exception

Участник портала
3 Июл 2022
104
17
59
23
Пишу на с#, подскажите как получить точку в мире (или направление куда смотрит камера)
 

mippoosedev

Гуру
BackEnd developer
2 Мар 2021
292
127
100
Направление куда смотрит камера = heading. Позицию в мире(куда именно смотрит прицел) - можно определить через raycasting. Этот принцип подходит для любого 3д мира, в рейдже есть нативки для использования рейкастов
 

JJIGolem

Старожил
High developer
BackEnd developer
19 Окт 2020
239
288
142
JavaScript:
function pointingAt(distance) {
    const camera = mp.cameras.new("gameplay"); // gets the current gameplay camera

    let position = camera.getCoord(); // grab the position of the gameplay camera as Vector3

    let direction = camera.getDirection(); // get the forwarding vector of the direction you aim with the gameplay camera as Vector3

    let farAway = new mp.Vector3((direction.x * distance) + (position.x), (direction.y * distance) + (position.y), (direction.z * distance) + (position.z)); // calculate a random point, drawn on a invisible line between camera position and direction (* distance)

    let result = mp.raycasting.testPointToPoint(position, farAway, null, 17); // now test point to point - intersects with map and objects [1 + 16]

    return result; // and return the result ( undefined, if no hit )
}

Источник
 

E-Exception

Участник портала
3 Июл 2022
104
17
59
23
Да вот проблема, через raycast можно, да. Но вот куда конкретно луч пускать не ясно. У cam.GetRotation() очень странные выходные данные, почему-то. Так что не получилось у меня сделать используя поворот камеры..
 

E-Exception

Участник портала
3 Июл 2022
104
17
59
23
На c# у камеры нет метода getDirection, только getRotation, в этом и проблема. А как я уже объяснил выше, через rotation сделать не получилось. Мб я слишком туп , не понятно крч.
p.s я брал вектор с GetRotation и нормализовывал его, получая направление камеры, затем умножал его на 40 (к примеру), чтобы получить точку в этом направлении, а затем сюда прибавлял GetCoord (актуальную позицию камеры). Вроде как такое должно было работать, но точки на выходе были не то чтобы несоответствующие реальности, они были тупо неадекватные, будто даже закономерности никакой не было.
 

E-Exception

Участник портала
3 Июл 2022
104
17
59
23
Помочь в этой ситуации ещё мог метод screen2dtoworld3d, но хоть убейте нет его в c#. Искал много, результатов ноль.
 

XDeveluxe

⚡️BackEnd Developer
Команда форума
Moderator
High developer
BackEnd developer
30 Авг 2021
2,771
1,577
211
28
В C# на клиенте есть
C#:
Vector3 GetGameplayCamCoord()
Vector3 GetGameplayCamRot(int rotationOrder)
Может они дадут то, что тебе нужно?
 
Последнее редактирование:

DaVilka

Старожил
BackEnd developer
16 Сен 2020
761
276
128
C#:
using RAGE;
using System;

namespace HardLife.Utils
{
    internal static class Direction
    {
        internal static Vector3 GetDirection(Vector3 camCoord, Vector3 camRot, float distance = 10.3f)
        {
            Vector3 lengthVector = multiply(rot_to_direction(camRot), distance);
            Vector3 posLookAt = add(camCoord, lengthVector);
            return posLookAt;
        }
        private static Vector3 multiply(Vector3 vector, float x)
        {
            Vector3 result = new Vector3(vector.X, vector.Y, vector.Z);
            result.X *= x;
            result.Y *= x;
            result.Z *= x;
            return result;
        }
        private static Vector3 add(Vector3 vectorA, Vector3 vectorB)
        {
            Vector3 result = new Vector3(vectorA.X, vectorA.Y, vectorA.Z);
            result.X += vectorB.X;
            result.Y += vectorB.Y;
            result.Z += vectorB.Z;
            return result;
        }
        private static Vector3 rot_to_direction(Vector3 rot)
        {
            float radiansZ = rot.Z * 0.0174532924f;
            float radiansX = rot.X * 0.0174532924f;
            float num = MathF.Abs((float)MathF.Cos((float)radiansX));
            Vector3 dir = new Vector3();
            dir.X = (float)((float)((float)(-(float)MathF.Sin((float)radiansZ))) * (float)num);
            dir.Y = (float)((float)((float)MathF.Cos((float)radiansZ)) * (float)num);
            dir.Z = (float)MathF.Sin((float)radiansX);
            return dir;
        }
    }
}
 

FireFeed

⚡️Frontend Developer
Команда форума
Moderator
17 Дек 2020
200
66
127
20
Я свечку за тебя поставил в церкви, спасибо
 
Реакции: DaVilka