org.joml.Vector2d Java Examples

The following examples show how to use org.joml.Vector2d. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MouseBoxSelectionDetector.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public boolean selectGameItem(GameItem[] gameItems, Window window, Vector2d mousePos, Camera camera) {
    // Transform mouse coordinates into normalized spaze [-1, 1]
    int wdwWitdh = window.getWidth();
    int wdwHeight = window.getHeight();
    
    float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;
    float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;
    float z = -1.0f;

    invProjectionMatrix.set(window.getProjectionMatrix());
    invProjectionMatrix.invert();
    
    tmpVec.set(x, y, z, 1.0f);
    tmpVec.mul(invProjectionMatrix);
    tmpVec.z = -1.0f;
    tmpVec.w = 0.0f;
    
    Matrix4f viewMatrix = camera.getViewMatrix();
    invViewMatrix.set(viewMatrix);
    invViewMatrix.invert();
    tmpVec.mul(invViewMatrix);
    
    mouseDir.set(tmpVec.x, tmpVec.y, tmpVec.z);

    return selectGameItem(gameItems, camera.getPosition(), mouseDir);
}
 
Example #2
Source File: MouseBoxSelectionDetector.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public void selectGameItem(GameItem[] gameItems, Window window, Vector2d mousePos, Camera camera) {
    // Transform mouse coordinates into normalized spaze [-1, 1]
    int wdwWitdh = window.getWidth();
    int wdwHeight = window.getHeight();
    
    float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;
    float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;
    float z = -1.0f;

    invProjectionMatrix.set(window.getProjectionMatrix());
    invProjectionMatrix.invert();
    
    tmpVec.set(x, y, z, 1.0f);
    tmpVec.mul(invProjectionMatrix);
    tmpVec.z = -1.0f;
    tmpVec.w = 0.0f;
    
    Matrix4f viewMatrix = camera.getViewMatrix();
    invViewMatrix.set(viewMatrix);
    invViewMatrix.invert();
    tmpVec.mul(invViewMatrix);
    
    mouseDir.set(tmpVec.x, tmpVec.y, tmpVec.z);

    selectGameItem(gameItems, camera.getPosition(), mouseDir);
}
 
Example #3
Source File: MouseBoxSelectionDetector.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public boolean selectGameItem(GameItem[] gameItems, Window window, Vector2d mousePos, Camera camera) {
    // Transform mouse coordinates into normalized spaze [-1, 1]
    int wdwWitdh = window.getWidth();
    int wdwHeight = window.getHeight();
    
    float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;
    float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;
    float z = -1.0f;

    invProjectionMatrix.set(window.getProjectionMatrix());
    invProjectionMatrix.invert();
    
    tmpVec.set(x, y, z, 1.0f);
    tmpVec.mul(invProjectionMatrix);
    tmpVec.z = -1.0f;
    tmpVec.w = 0.0f;
    
    Matrix4f viewMatrix = camera.getViewMatrix();
    invViewMatrix.set(viewMatrix);
    invViewMatrix.invert();
    tmpVec.mul(invViewMatrix);
    
    mouseDir.set(tmpVec.x, tmpVec.y, tmpVec.z);

    return selectGameItem(gameItems, camera.getPosition(), mouseDir);
}
 
Example #4
Source File: Pin.java    From BlockMap with MIT License 6 votes vote down vote up
@Override
protected PopOver initInfo() {
	PopOver info = super.initInfo();
	GridPane content = new GridPane();
	content.getStyleClass().add("grid");

	content.add(new Label("Player position:"), 0, 2);

	Vector3dc position = player.getPosition();
	Button jumpButton = new Button(position.toString());
	jumpButton.setTooltip(new Tooltip("Click to go there"));
	content.add(jumpButton, 1, 2);
	jumpButton.setOnAction(e -> {
		Vector2d spawnpoint = new Vector2d(position.x(), position.z());
		AABBd frustum = viewport.frustumProperty.get();
		viewport.translationProperty.set(spawnpoint.negate().add((frustum.maxX - frustum.minX) / 2, (frustum.maxY - frustum.minY) / 2));
		info.hide();
	});

	info.setContentNode(content);
	return info;
}
 
Example #5
Source File: MouseBoxSelectionDetector.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public boolean selectGameItem(GameItem[] gameItems, Window window, Vector2d mousePos, Camera camera) {
    // Transform mouse coordinates into normalized spaze [-1, 1]
    int wdwWitdh = window.getWidth();
    int wdwHeight = window.getHeight();
    
    float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;
    float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;
    float z = -1.0f;

    invProjectionMatrix.set(window.getProjectionMatrix());
    invProjectionMatrix.invert();
    
    tmpVec.set(x, y, z, 1.0f);
    tmpVec.mul(invProjectionMatrix);
    tmpVec.z = -1.0f;
    tmpVec.w = 0.0f;
    
    Matrix4f viewMatrix = camera.getViewMatrix();
    invViewMatrix.set(viewMatrix);
    invViewMatrix.invert();
    tmpVec.mul(invViewMatrix);
    
    mouseDir.set(tmpVec.x, tmpVec.y, tmpVec.z);

    return selectGameItem(gameItems, camera.getPosition(), mouseDir);
}
 
Example #6
Source File: MouseBoxSelectionDetector.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public boolean selectGameItem(GameItem[] gameItems, Window window, Vector2d mousePos, Camera camera) {
    // Transform mouse coordinates into normalized spaze [-1, 1]
    int wdwWitdh = window.getWidth();
    int wdwHeight = window.getHeight();
    
    float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;
    float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;
    float z = -1.0f;

    invProjectionMatrix.set(window.getProjectionMatrix());
    invProjectionMatrix.invert();
    
    tmpVec.set(x, y, z, 1.0f);
    tmpVec.mul(invProjectionMatrix);
    tmpVec.z = -1.0f;
    tmpVec.w = 0.0f;
    
    Matrix4f viewMatrix = camera.getViewMatrix();
    invViewMatrix.set(viewMatrix);
    invViewMatrix.invert();
    tmpVec.mul(invViewMatrix);
    
    mouseDir.set(tmpVec.x, tmpVec.y, tmpVec.z);

    return selectGameItem(gameItems, camera.getPosition(), mouseDir);
}
 
Example #7
Source File: MouseBoxSelectionDetector.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public boolean selectGameItem(GameItem[] gameItems, Window window, Vector2d mousePos, Camera camera) {
    // Transform mouse coordinates into normalized spaze [-1, 1]
    int wdwWitdh = window.getWidth();
    int wdwHeight = window.getHeight();
    
    float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;
    float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;
    float z = -1.0f;

    invProjectionMatrix.set(window.getProjectionMatrix());
    invProjectionMatrix.invert();
    
    tmpVec.set(x, y, z, 1.0f);
    tmpVec.mul(invProjectionMatrix);
    tmpVec.z = -1.0f;
    tmpVec.w = 0.0f;
    
    Matrix4f viewMatrix = camera.getViewMatrix();
    invViewMatrix.set(viewMatrix);
    invViewMatrix.invert();
    tmpVec.mul(invViewMatrix);
    
    mouseDir.set(tmpVec.x, tmpVec.y, tmpVec.z);

    return selectGameItem(gameItems, camera.getPosition(), mouseDir);
}
 
Example #8
Source File: MouseBoxSelectionDetector.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public boolean selectGameItem(GameItem[] gameItems, Window window, Vector2d mousePos, Camera camera) {
    // Transform mouse coordinates into normalized spaze [-1, 1]
    int wdwWitdh = window.getWidth();
    int wdwHeight = window.getHeight();
    
    float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;
    float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;
    float z = -1.0f;

    invProjectionMatrix.set(window.getProjectionMatrix());
    invProjectionMatrix.invert();
    
    tmpVec.set(x, y, z, 1.0f);
    tmpVec.mul(invProjectionMatrix);
    tmpVec.z = -1.0f;
    tmpVec.w = 0.0f;
    
    Matrix4f viewMatrix = camera.getViewMatrix();
    invViewMatrix.set(viewMatrix);
    invViewMatrix.invert();
    tmpVec.mul(invViewMatrix);
    
    mouseDir.set(tmpVec.x, tmpVec.y, tmpVec.z);

    return selectGameItem(gameItems, camera.getPosition(), mouseDir);
}
 
Example #9
Source File: MouseBoxSelectionDetector.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public boolean selectGameItem(GameItem[] gameItems, Window window, Vector2d mousePos, Camera camera) {
    // Transform mouse coordinates into normalized spaze [-1, 1]
    int wdwWitdh = window.getWidth();
    int wdwHeight = window.getHeight();
    
    float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;
    float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;
    float z = -1.0f;

    invProjectionMatrix.set(window.getProjectionMatrix());
    invProjectionMatrix.invert();
    
    tmpVec.set(x, y, z, 1.0f);
    tmpVec.mul(invProjectionMatrix);
    tmpVec.z = -1.0f;
    tmpVec.w = 0.0f;
    
    Matrix4f viewMatrix = camera.getViewMatrix();
    invViewMatrix.set(viewMatrix);
    invViewMatrix.invert();
    tmpVec.mul(invViewMatrix);
    
    mouseDir.set(tmpVec.x, tmpVec.y, tmpVec.z);

    return selectGameItem(gameItems, camera.getPosition(), mouseDir);
}
 
Example #10
Source File: RectangledTest.java    From JOML with MIT License 5 votes vote down vote up
public void testRectangleContainsPoints() {
    Rectangled rect = new Rectangled(0, 0, 3, 3);

    Assert.assertTrue(rect.isValid());
    Assert.assertTrue(rect.containsPoint(new Vector2d(0, 0)));
    Assert.assertFalse(rect.containsPoint(new Vector2d(-1, -1)));
    Assert.assertFalse(rect.containsPoint(new Vector2d(4, 4)));
}
 
Example #11
Source File: Node.java    From LWJGUI with MIT License 5 votes vote down vote up
protected void resize() {
	
	// Size up to pref size
	if ( size.x < this.getPrefWidth() )
		size.x = this.getPrefWidth();
	if ( size.y < this.getPrefHeight() )
		size.y = this.getPrefHeight();

	// Get available size
	Vector2d available = this.getAvailableSize();
	double availableWidth = available.x;
	double availableHeight = available.y;

	// Cap size to available size
	if ( size.x > availableWidth )
		size.x = availableWidth;
	if ( size.y > availableHeight )
		size.y = availableHeight;

	// Cap size to min size
	if ( size.x < this.getMinWidth() )
		size.x = this.getMinWidth();
	if ( size.y < this.getMinHeight() )
		size.y = this.getMinHeight();

	// Cap size to max size
	if ( size.x > this.getMaxWidth() )
		size.x = this.getMaxWidth();
	if ( size.y > this.getMaxHeight() )
		size.y = this.getMaxHeight();
}
 
Example #12
Source File: DragScrollDecoration.java    From BlockMap with MIT License 5 votes vote down vote up
/**
 * @param dragButton
 *            The button that must be pressed to activate dragging. <code>null</code> will disable dragging.
 * @param scrollFactor
 *            Higher values will increase the zoomed amount per scroll. Zero deactivates scrolling. Negative values invert the scroll
 *            direction.
 */
public DragScrollDecoration(DisplayViewport frustum, MouseButton dragButton, double scrollFactor) {
	setOnMouseMoved(e -> frustum.mousePosProperty.set(new Vector2d(e.getX(), e.getY())));
	setOnMouseDragged(e -> {
		Vector2dc old = frustum.mousePosProperty.get();
		Vector2d current = new Vector2d(e.getX(), e.getY());
		if (cooldown <= 0) {
			if (dragButton != null && e.getButton() == dragButton) {
				frustum.pan(current.x() - old.x(), current.y() - old.y());
				double width = getWidth();
				double height = getHeight();
				double dx = 0, dy = 0;
				if (current.x() < 1)
					dx = width - 3;
				if (current.x() > width - 2)
					dx = -width + 4;
				if (current.y() < 1)
					dy = height - 3;
				if (current.y() > height - 2)
					dy = -height + 4;
				if (dx != 0 || dy != 0) {
					current.add(dx, dy);
					/* Lazy initialization */
					if (robot == null)
						robot = new Robot();
					robot.mouseMove((int) (e.getScreenX() + dx), (int) (e.getScreenY() + dy));
					cooldown = 3;
				}
			}
		} else if (cooldown > 0)
			cooldown--;
		frustum.mousePosProperty.set(current);
	});

	if (scrollFactor != 0)
		setOnScroll(e -> frustum.mouseScroll(e.getDeltaY() * scrollFactor));
}
 
Example #13
Source File: PinDecoration.java    From BlockMap with MIT License 5 votes vote down vote up
PinGroup(PinGroup a, PinGroup b) {
	this.pins = new ArrayList<>();
	pins.addAll(a.pins);
	pins.addAll(b.pins);
	int sizeLeft = a.pins.size(), sizeRight = b.pins.size();
	center = new Vector2d(
			a.center.x() * sizeLeft + b.center.x() * sizeRight,
			a.center.y() * sizeLeft + b.center.y() * sizeRight)
					.mul(1.0 / (sizeLeft + sizeRight));
}
 
Example #14
Source File: Pin.java    From BlockMap with MIT License 5 votes vote down vote up
/** Convert a {@link LevelMetadata} object containing information retrieved directly from the world to (static) pins. */
public static Set<Pin> convertStatic(LevelMetadata pin, ScheduledExecutorService backgroundThread, DisplayViewport viewport) {
	Set<Pin> pins = new HashSet<>();
	for (LevelMetadata.PlayerPin player : pin.getPlayers().orElse(Collections.emptyList())) {
		pins.add(new PlayerPin(player, backgroundThread, viewport));
		if (player.getSpawnpoint().isPresent())
			pins.add(new PlayerSpawnpointPin(player, viewport));
	}
	for (LevelMetadata.VillageObjectPin villageObject : pin.getVillageObjects().orElse(Collections.emptyList())) {
		if (PinType.VILLAGE_MAPPING.containsKey(villageObject.getType())) {
			pins.add(new VillageObjectPin(villageObject, viewport));
		} else {
			log.warn("Invalid type for village pin: '" + villageObject.getType() + "' at position " + villageObject.getPosition()
					+ ". Allowed values: " + PinType.VILLAGE_MAPPING.keySet());
		}
	}

	/* Cluster maps at identical position to merge their pins. */
	pins.addAll(pin.getMaps().map(List::stream).orElse(Stream.empty())
			.collect(Collectors.groupingBy(map -> map.getPosition()))
			.entrySet()
			.stream()
			.map(e -> new MapPin(new Vector2d(e.getKey().x(), e.getKey().y()), e.getValue(), viewport))
			.collect(Collectors.toList()));
	/* All banner pins of the maps */
	pins.addAll(pin.getMaps().map(List::stream).orElse(Stream.empty())
			.flatMap(map -> map.getBanners().map(List::stream).orElse(Stream.empty()))
			.map(banner -> new Pin(new Vector2d(banner.getPosition().x(), banner.getPosition().y()), PinType.MAP_BANNER, viewport))
			.collect(Collectors.toList()));

	pin.getWorldSpawn().map(spawn -> new WorldSpawnPin(spawn.getSpawnpoint(), viewport)).ifPresent(pins::add);

	return pins;
}
 
Example #15
Source File: Pin.java    From BlockMap with MIT License 5 votes vote down vote up
@Override
protected PopOver initInfo() {
	PopOver info = super.initInfo();
	GridPane content = new GridPane();
	content.getStyleClass().add("grid");

	content.add(new Label("Name:"), 0, 2);
	Label playerName = new Label("loading...");
	playerName.textProperty().bind(this.playerName);
	content.add(playerName, 1, 2);

	player.getSpawnpoint().ifPresent(spawn -> {
		content.add(new Label("Spawnpoint: "), 0, 3);
		Button jumpButton = new Button(spawn.toString());
		jumpButton.setTooltip(new Tooltip("Click to go there"));
		content.add(jumpButton, 1, 3);
		jumpButton.setOnAction(e -> {
			Vector2d spawnpoint = new Vector2d(spawn.x(), spawn.z());
			AABBd frustum = viewport.frustumProperty.get();
			viewport.translationProperty.set(spawnpoint.negate().add((frustum.maxX - frustum.minX) / 2, (frustum.maxY - frustum.minY) / 2));
			info.hide();
		});
	});

	info.setContentNode(content);
	return info;
}
 
Example #16
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #17
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #18
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public Vector2d getCurrentPos() {
    return currentPos;        
}
 
Example #19
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #20
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #21
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #22
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public Vector2d getCurrentPos() {
    return currentPos;        
}
 
Example #23
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #24
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #25
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #26
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #27
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public Vector2d getCurrentPos() {
    return currentPos;        
}
 
Example #28
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #29
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}
 
Example #30
Source File: MouseInput.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
public MouseInput() {
    previousPos = new Vector2d(-1, -1);
    currentPos = new Vector2d(0, 0);
    displVec = new Vector2f();
}