Skip to content

Commit

Permalink
Roll Values
Browse files Browse the repository at this point in the history
  • Loading branch information
hammy275 committed Apr 25, 2022
1 parent 9cb1037 commit f340576
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '1.2.0'
version = '1.3.0'
group = 'net.blf02.vrapi' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'vrapi'

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/blf02/vrapi/api/data/IVRData.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,22 @@ public interface IVRData {
* @return A Vector3d representing the object's location in Minecraft
*/
public Vector3d position();

/**
* Returns the roll of the object in degrees.
* @return Object roll in degrees.
*/
public float getRoll();

/**
* Returns the pitch of the object in degrees.
* @return Object pitch in degrees.
*/
public float getPitch();

/**
* Returns the yaw of the object in degrees.
* @return Object yaw in degrees.
*/
public float getYaw();
}
13 changes: 10 additions & 3 deletions src/main/java/net/blf02/vrapi/client/VRDataGrabber.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class VRDataGrabber {
// VRDevicePose from Vivecraft
protected static Method VRDevicePose_getPosition; // Returns Vector3d (vanilla type)
protected static Method VRDevicePose_getDirection; // Returns Vector3d (vanilla type)
protected static Method VRDevicePose_getRoll; // Returns float

// MCVR from Vivecraft
public static Method MCVR_triggerHapticPulse; // Returns void
Expand All @@ -52,6 +53,7 @@ public static void init() {

VRDevicePose_getPosition = getMethod(Constants.VRDevicePoseRaw, "getPosition");
VRDevicePose_getDirection = getMethod(Constants.VRDevicePoseRaw, "getDirection");
VRDevicePose_getRoll = getMethod(Constants.VRDevicePoseRaw, "getRoll");

Minecraft_vr = getField(Minecraft.class, "vr");
Minecraft_vr_Instance = Minecraft_vr.get(Minecraft.getInstance());
Expand Down Expand Up @@ -82,22 +84,27 @@ public static VRPlayer getVRPlayer() {

Vector3d hmdPosition = (Vector3d) VRDevicePose_getPosition.invoke(hmdDevicePoseRaw); // Gets the position for the HMD in the world.
Vector3d hmdLookVec = (Vector3d) VRDevicePose_getDirection.invoke(hmdDevicePoseRaw);
float hmdRoll = (float) VRDevicePose_getRoll.invoke(hmdDevicePoseRaw);

Vector3d c0Position = (Vector3d) VRDevicePose_getPosition.invoke(c0DevicePoseRaw);
Vector3d c0LookVec = (Vector3d) VRDevicePose_getDirection.invoke(c0DevicePoseRaw);
float c0roll = (float) VRDevicePose_getRoll.invoke(c0DevicePoseRaw);

Vector3d c1Position = (Vector3d) VRDevicePose_getPosition.invoke(c1DevicePoseRaw);
Vector3d c1LookVec = (Vector3d) VRDevicePose_getDirection.invoke(c1DevicePoseRaw);
float c1roll = (float) VRDevicePose_getRoll.invoke(c1DevicePoseRaw);

Vector3d eye0Position = (Vector3d) VRDevicePose_getPosition.invoke(eye0DevicePoseRaw);
Vector3d eye0LookVec = (Vector3d) VRDevicePose_getDirection.invoke(eye0DevicePoseRaw);
float eye0roll = (float) VRDevicePose_getRoll.invoke(eye0DevicePoseRaw);

Vector3d eye1Position = (Vector3d) VRDevicePose_getPosition.invoke(eye1DevicePoseRaw);
Vector3d eye1LookVec = (Vector3d) VRDevicePose_getDirection.invoke(eye1DevicePoseRaw);
float eye1roll = (float) VRDevicePose_getRoll.invoke(eye1DevicePoseRaw);

return new VRPlayer(new VRData(hmdPosition, hmdLookVec),
new VRData(c0Position, c0LookVec), new VRData(c1Position, c1LookVec),
new VRData(eye0Position, eye0LookVec), new VRData(eye1Position, eye1LookVec));
return new VRPlayer(new VRData(hmdPosition, hmdLookVec, hmdRoll),
new VRData(c0Position, c0LookVec, c0roll), new VRData(c1Position, c1LookVec, c1roll),
new VRData(eye0Position, eye0LookVec, eye0roll), new VRData(eye1Position, eye1LookVec, eye1roll));
} catch (InvocationTargetException | IllegalAccessException e) {
// We shouldn't error here, as we know these fields and methods exist due to getField() and getMethod()
// combined with having access to Vivecraft's codebase on GitHub
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/blf02/vrapi/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Constants {

// Version {major, minor, patch}
public static final int[] version = new int[]{1, 2, 0};
public static final int[] version = new int[]{1, 3, 0};

// Debugging
public static final boolean doDebugging = false;
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/net/blf02/vrapi/data/VRData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public class VRData implements IVRData {

protected final Vector3d position;
protected final Vector3d lookVec;
protected final float roll;

public VRData(Vector3d position, Vector3d lookVec) {
public VRData(Vector3d position, Vector3d lookVec, float roll) {
this.position = position;
this.lookVec = lookVec;
this.roll = roll;
}

/**
Expand All @@ -27,6 +29,31 @@ public Vector3d position() {
return this.position;
}

/**
* Returns the roll of the controller in degrees.
* @return Controller roll in degrees.
*/
@Override
public float getRoll() {
return roll;
}

/**
* Returns the pitch of the object in degrees.
* @return Object pitch in degrees.
*/
public float getPitch() {
return (float) Math.toDegrees(Math.asin(this.lookVec.y / this.lookVec.length()));
}

/**
* Returns the yaw of the object in degrees.
* @return Object yaw in degrees.
*/
public float getYaw() {
return (float) Math.toDegrees(Math.atan2(-this.lookVec.x, this.lookVec.z));
}

/**
* Get the direction of the object
* @return A vector representing where the object is pointing on the x, y, and z axis.
Expand All @@ -43,6 +70,7 @@ public Vector3d getLookAngle() {
public static void encode(VRData data, PacketBuffer buffer) {
buffer.writeDouble(data.position.x).writeDouble(data.position.y).writeDouble(data.position.z);
buffer.writeDouble(data.lookVec.x).writeDouble(data.lookVec.y).writeDouble(data.lookVec.z);
buffer.writeFloat(data.roll);
}

/**
Expand All @@ -53,7 +81,8 @@ public static void encode(VRData data, PacketBuffer buffer) {
public static VRData decode(PacketBuffer buffer) {
Vector3d position = new Vector3d(buffer.readDouble(), buffer.readDouble(), buffer.readDouble());
Vector3d lookVec = new Vector3d(buffer.readDouble(), buffer.readDouble(), buffer.readDouble());
return new VRData(position, lookVec);
float roll = buffer.readFloat();
return new VRData(position, lookVec, roll);
}

}
6 changes: 3 additions & 3 deletions src/main/java/net/blf02/vrapi/debug/DebugSubscriber.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.blf02.vrapi.debug;

import net.blf02.vrapi.event.VRPlayerTickEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand All @@ -8,11 +9,10 @@
public class DebugSubscriber {

@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event) {
public void onPlayerTick(VRPlayerTickEvent event) {
if (event.phase != TickEvent.Phase.END) return;
if (event.player.tickCount % 20 == 0) {
String side = event.player.level.isClientSide ? "client" : "server";
System.out.println(side + ": Both sides have VR: " + DebugPlugin.vrAPI.apiActive(event.player));
System.out.println(event.vrPlayer.getController0().getRoll());
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ modId="vrapi" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
# ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata
# see the associated build.gradle script for how to populate this completely automatically during a build
version="1.1.0" #mandatory
version="1.3.0" #mandatory
# A display name for the mod
displayName="VR API" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/
Expand Down

0 comments on commit f340576

Please sign in to comment.