Skip to content

Commit

Permalink
Eyes (closes #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
hammy275 committed Mar 9, 2022
1 parent 50446a8 commit 6654971
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/main/java/net/blf02/vrapi/api/data/IVRPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,23 @@ public interface IVRPlayer {
* @return IVRData representing the requested controller
*/
public IVRData getController(int controller);

/**
* Get Left Eye information
* @return IVRData representing the position and look direction of the left eye
*/
public IVRData getLeftEye();

/**
* Get Right Eye information
* @return IVRData representing the position and look direction of the right eye
*/
public IVRData getRightEye();

/**
* Get information for an eye.
* @param eye Eye number to get. 0 for left, 1 for right.
* @return IVRData representing the eye.
*/
public IVRData getEye(int eye);
}
16 changes: 15 additions & 1 deletion src/main/java/net/blf02/vrapi/client/VRDataGrabber.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class VRDataGrabber {
protected static Field VRData_hmd; // Type VRDevicePose
protected static Field VRData_c0; // Type VRDevicePose
protected static Field VRData_c1; // Type VRDevicePose
protected static Field VRData_eye0; // Type VRDevicePose (corresponds to left eye)
protected static Field VRData_eye1; // Type VRDevicePose (corresponds to right eye)

// VRDevicePose from Vivecraft
protected static Method VRDevicePose_getPosition; // Returns Vector3d (vanilla type)
Expand All @@ -45,6 +47,8 @@ public static void init() {
VRData_hmd = getField(Constants.VRDataRaw, "hmd");
VRData_c0 = getField(Constants.VRDataRaw, "c0");
VRData_c1 = getField(Constants.VRDataRaw, "c1");
VRData_eye0 = getField(Constants.VRDataRaw, "eye0");
VRData_eye1 = getField(Constants.VRDataRaw, "eye1");

VRDevicePose_getPosition = getMethod(Constants.VRDevicePoseRaw, "getPosition");
VRDevicePose_getDirection = getMethod(Constants.VRDevicePoseRaw, "getDirection");
Expand Down Expand Up @@ -73,6 +77,8 @@ public static VRPlayer getVRPlayer() {
Object hmdDevicePoseRaw = VRData_hmd.get(vrDataRaw); // Get the VRDevicePose for the HMD
Object c0DevicePoseRaw = VRData_c0.get(vrDataRaw);
Object c1DevicePoseRaw = VRData_c1.get(vrDataRaw);
Object eye0DevicePoseRaw = VRData_eye0.get(vrDataRaw);
Object eye1DevicePoseRaw = VRData_eye1.get(vrDataRaw);

Vector3d hmdPosition = (Vector3d) VRDevicePose_getPosition.invoke(hmdDevicePoseRaw); // Gets the position for the HMD in the world.
Vector3d hmdLookVec = (Vector3d) VRDevicePose_getDirection.invoke(hmdDevicePoseRaw);
Expand All @@ -82,8 +88,16 @@ public static VRPlayer getVRPlayer() {

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

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

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

return new VRPlayer(new VRData(hmdPosition, hmdLookVec),
new VRData(c0Position, c0LookVec), new VRData(c1Position, c1LookVec));
new VRData(c0Position, c0LookVec), new VRData(c1Position, c1LookVec),
new VRData(eye0Position, eye0LookVec), new VRData(eye1Position, eye1LookVec));
} 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
47 changes: 45 additions & 2 deletions src/main/java/net/blf02/vrapi/data/VRPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ public class VRPlayer implements IVRPlayer {
protected final VRData hmd;
protected final VRData controller0;
protected final VRData controller1;
protected final VRData leftEye;
protected final VRData rightEye;

public VRPlayer(VRData hmd, VRData controller0, VRData controller1) {
public VRPlayer(VRData hmd, VRData controller0, VRData controller1,
VRData leftEye, VRData rightEye) {
this.hmd = hmd;
this.controller0 = controller0;
this.controller1 = controller1;
this.leftEye = leftEye;
this.rightEye = rightEye;
}

/**
Expand Down Expand Up @@ -54,6 +59,40 @@ public VRData getController(int controller) {
return controller == 0 ? getController0() : getController1();
}

/**
* Get Left Eye information
* @return VRData representing the position and look direction of the left eye
*/
@Override
public VRData getLeftEye() {
return this.leftEye;
}

/**
* Get Right Eye information
* @return VRData representing the position and look direction of the right eye
*/
@Override
public VRData getRightEye() {
return this.rightEye;
}

/**
* Get information for an eye.
* @param eye Eye number to get. 0 for left, 1 for right.
* @return VRData representing the eye.
*/
@Override
public VRData getEye(int eye) {
if (eye == 0) {
return this.getLeftEye();
} else if (eye == 1) {
return this.getRightEye();
} else {
throw new IllegalArgumentException("Eye " + eye + " invalid! You can only select eyes 0 or 1.");
}
}

/**
* Encode VRPlayer into a buffer. Used internally
* @param player VRPlayer to encode
Expand All @@ -63,6 +102,8 @@ public static void encode(VRPlayer player, PacketBuffer buffer) {
VRData.encode(player.getHMD(), buffer);
VRData.encode(player.getController0(), buffer);
VRData.encode(player.getController1(), buffer);
VRData.encode(player.getLeftEye(), buffer);
VRData.encode(player.getRightEye(), buffer);
}

/**
Expand All @@ -74,7 +115,9 @@ public static VRPlayer decode(PacketBuffer buffer) {
VRData hmd = VRData.decode(buffer);
VRData c0 = VRData.decode(buffer);
VRData c1 = VRData.decode(buffer);
return new VRPlayer(hmd, c0, c1);
VRData leftEye = VRData.decode(buffer);
VRData rightEye = VRData.decode(buffer);
return new VRPlayer(hmd, c0, c1, leftEye, rightEye);
}

}
16 changes: 13 additions & 3 deletions src/main/java/net/blf02/vrapi/debug/DebugSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import net.blf02.vrapi.api.data.IVRPlayer;
import net.blf02.vrapi.event.VRPlayerTickEvent;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.particles.BasicParticleType;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand All @@ -16,8 +18,16 @@ public void onVRTick(VRPlayerTickEvent event) {
if (event.phase == TickEvent.Phase.START) return;
PlayerEntity player = event.player;
IVRPlayer vrPlayer = event.vrPlayer;
if (player.tickCount % 20 == 0 && !player.level.isClientSide) {
DebugPlugin.vrAPI.triggerHapticPulse(0, 0.5f, (ServerPlayerEntity) player);
if (player.tickCount % 20 == 0 && player.level.isClientSide) {
for (int eye = 0; eye <= 1; eye++) {
Vector3d pos = vrPlayer.getEye(eye).position();
System.out.println("Eye: " + eye + "\tPos: " + pos);
for (int i = 0; i < 10; i++) {
pos = pos.add(vrPlayer.getEye(eye).getLookAngle());
BasicParticleType type = eye == 0 ? ParticleTypes.DRIPPING_WATER : ParticleTypes.DRIPPING_LAVA;
player.level.addParticle(type, pos.x, pos.y, pos.z, 0, 0, 0);
}
}
}
}
}

0 comments on commit 6654971

Please sign in to comment.