#Calculating a Vector2 aiming direction based on the mouse position in orthogonal isometric 3D

1 messages · Page 1 of 1 (latest)

torpid lotus
#

Given an orthogonal camera set to 45 degrees to display an isometric perspective, I need to calculate the shooting direction based on where the mouse position is relative to the player.

The end results is a Vector2D because I only care about the direction on a 2D plane, that direction should point at the mouse position, it's important that vector will make the resulting bullet will pass under the mouse pointer, if it's not moved.

I have absolutely no idea where to start on this

high vessel
#

were you able to figure it out?

torpid lotus
#

Nope

reef hinge
#

is there Actually a rotated 3d camera involved or is this a question about 2d?

#

in 3d, you could raycast from the camera to a plane that represents the floor

#

and then do the vector math in that respect

torpid lotus
#

The camera is looking down at 45 degrees

reef hinge
#

the 3d camera is looking down at 45 degrees?

#

(I think that's what you mean, but it's good to get specific early 😄 )

torpid lotus
#

Yep it's a 3D camera, oerographic

#

Oerographic

#

Ortogrwpgi

#

Ortographic

#

Sorry phone

reef hinge
#

one of those.

#

Anyway!

#

camera.project_ray_normal and project_ray_origin let you place a ray shooting from the camera into the world

torpid lotus
#

There's other geometry in the way though

reef hinge
#

so that figures out where the mouse would be in the plane, without accounting for anything it'd hit on the way there

#

(which is I think what you want? You can do an actual raycast if you want to account for collision bodies in between)

#

this lets you get the point_this_made-point_player_at vector the bullet should traverse

#

(it's a vector3d, but you can do Vector2(that.x, that.z) if you really want :-D)

torpid lotus
#

Hmm I'm not sure I get it

reef hinge
#

which step lost you?

torpid lotus
#

Wouldn't the raycast also hit other level geometry?

reef hinge
#

no, look at the method I suggest you use

#

it's just a mathematical intersection ray<->plane

#

(where tbc the plane is the Plane with normal=Vector3.UP at d=point_player_at.y, equiv to point=point_player_at)

torpid lotus
#

I'm completely lost

reef hinge
#

haha! I'm still here!

torpid lotus
#

And it's not helping that it's late at night

#

So I cannot really concentrate on this

reef hinge
#

ok. But it is just math (, and code), and so it is pretty solvable when you get a chance!

torpid lotus
#

I'll try to read it up in the morning and ping you if I have issues

reef hinge
#

I'm happy to try to expand on any of the steps!

torpid lotus
#

Thanks for the help

torpid lotus
#
public partial class MouseAimProvider3D : Node3D, IMouseAimProvider
{
    private Viewport _viewport;

    public override void _Ready()
    {
        _viewport = GetViewport();
    }
    
    public Vector2 GetMouseAimInput()
    {
        var mouseWorldPos = GetMouseWorldPosition(this.GlobalPosition.Y);
        
        var direction = (mouseWorldPos - this.GlobalPosition).Normalized();
        
        return new Vector2(direction.X, direction.Z);
    }

    public Vector3 GetMouseWorldPosition(float planeHeight = 0)
    {
        var mousePos = _viewport.GetMousePosition();
        var rayOrigin = CameraController3D.Instance.ProjectRayOrigin(mousePos);
        var rayDirection = CameraController3D.Instance.ProjectRayNormal(mousePos);

        var plane = new Plane(Vector3.Up, planeHeight);
        
        var intersection = plane.IntersectsRay(rayOrigin, rayDirection);

        return intersection ?? Vector3.Zero;
    }
}