简体   繁体   中英

Creating InputProcessor for PerspectiveCamera on Android Libgdx

I am trying to create an InputProcessor that will do many things. I have a touchpad set up that I am trying to move the camera along the x and y axes.

tBounds = new Sprite(Assets.touchpadBounds);
tBounds.setSize(tBounds.getWidth() * scale, tBounds.getHeight() * scale);
tKnob = new Sprite(Assets.touchpad);
tKnob.setSize(tKnob.getWidth() * scale, tKnob.getHeight() * scale);
touchpad = new Skin();
touchpad.add("boundary", tBounds);
touchpad.add("circle", tKnob);
touchpadStyle = new TouchpadStyle();
bounds = touchpad.getDrawable("boundary");
knob = touchpad.getDrawable("circle");
touchpadStyle.background = bounds;
touchpadStyle.knob = knob;
pad = new Touchpad(10, touchpadStyle);
stage.addActor(pad);
//Setting Bounds
pad.setBounds(width / 14, height / 10, tBounds.getHeight(),
tBounds.getWidth());

private void setKnobAction() {
    camera.position.set(camera.position.x + (pad.getKnobPercentX() * 0.1f),
            camera.position.y + 0,
            camera.position.z - (pad.getKnobPercentY() * 0.1f));
    camera.update();
}

The problem I am having with the setKnobAction() is that it moves the camera based on the initial direction it is facing. I am wanting it to move in the direction that it is currently facing.

The problem is, that you move the camera relativ to the worlds axis.
If you rotate your camera, the worlds axis stay the same, only the cameras direction vector changes.
Therefor you need to move your camera relativ to its direction vector.

I guess before playing arround with PerspectiveCamera and 3D in general you should read a few tutorials.
Here is a tutorial, which describes a FirstPersonCamera .

Basicly you need to store a normalized direction Vector for the camera:

Vector3 direction = new Vector3(1, 0, 0); //You are looking in x-Direction

If you rotate, you need to rotate this Vector arround the worlds Y-Axis or the camera s Up-Axis (depends on the situation, explained in the linked tutorial):

direction.rotate(Vector3.Y, degrees).nor(); // Not sure if "nor()" is needed, it limits the length to 1, as it only gives the direction, not the speed.  

To go foreward you simply need to add the direction Vector scaled by the speed to the position.

camera.position.add(direction.scl(speed))

After that you need to reset the direction s length by calling nor() again.

Hope it helps.

I have figured it out. All I needed to do for forward and backward motion was to add or subtract the direction vector scaled by 1 to the position vector.

// To move forward
camera.position.add(camera.direction.scl(1f));
camera.update();

// To move backwards
camera.position.sub(camera.direction.scl(1f));
camera.update();

// To move to the right
Vector3 right = camera.direction.cpy().crs(Vector3.Y).nor();
camera.position.add(right.scl(1));
camera.update();

// To move to the left
Vector3 right = camera.direction.cpy().crs(Vector3.Y).nor();
camera.position.sub(right.scl(1));
camera.update();

To strafe (move to the left or right while the camera is facing forward) I needed to add or subtract the cross-product between the camera's direction vector and the camera's up vector.

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