简体   繁体   中英

convert java lwjgl mouse rotation to c# opentk

im making a model viewer in C# and im having trouble getting the camera to move around a point...

in java all i had to do was

        if (Mouse.isButtonDown(0) && !Mouse.isButtonDown(1)) {
            yaw -= (float) Mouse.getDX();
            pitch -= (float) Mouse.getDY();
        }
        if (Mouse.isButtonDown(0) && Mouse.isButtonDown(1)) {
            offset_z += (float) Mouse.getDY();
        }
        float wheel = (float) Mouse.getDWheel() / 960.0F;
        if (wheel > 1.0F)
            wheel = 1.0F;
        else if (wheel < -1.0F)
            wheel = -1.0F;
        scale -= scale * wheel;
        if (scale < 0.01F)
            scale = 0.01F;

sorry, im new to c# I have searched for this for about 30 minutes before i posted here so...

The code you posted maps 1-1 to C# / OpenTK. In idiomatic C#, it would look something like this:

using OpenTK;
using OpenTK.Input;

class MouseControls
{
    public float Yaw { get; set; }
    public float Pitch { get; set; }
    public float Offset { get; set; }
    public float Scale { get; set; }

    MouseState mouse_previous;

    public void Update()
    {
        var mouse = Mouse.GetState();
        var delta = new Vector3(
            mouse.X - mouse_previous.X,
            mouse.Y - mouse_previous.Y,
            mouse.WheelPrecise - mouse_previous.WheelPrecise);

        if (mouse[MouseButton.Left] && !mouse[MouseButton.Right])
        {
            Yaw -= delta.X;
            Pitch -= delta.Y;
        }

        if (mouse[MouseButton.Left] && mouse[MouseButton.Right])
        {
            Offset += delta.Y;
        }

        Scale -= Scale * MathHelper.Clamp(delta.Z, -1.0f, 1.0f);
        Scale = Scale < 0.01f ? 0.01f : Scale;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM