Trouble getting world position of object

Hey all,

I’m currently trying to make a ‘dungeon generator’ - I have a set few Scene objects that each have an EntryNode, ExitNode and Room mesh inside of it. I’m having trouble setting the position of the Room object based on the previous’ room ExitNode.

The model is structured like so:

Scene
   L Entry_Node (instanceof THREE.Mesh) position: 0,0,0
   L Room (instaceof THREE.Group), positiom 0,0,23
      L (Several Children that are irrelevant)
   L Exit_Node position: 0,0,46

Rooms aren’t guaranteed to be uniform in size or always be the same size, otherwise this would be simple.

I’m currently creating a room like so:

_generateRoom(name) {
    let room = common.assets.model(name);		// Returns THREE.Scene with EntryNode, Room and ExitNode as children

	const entryNode = room.children.find(e => e.name.startsWith('Node_Entry'));
	const exitNode = room.children.find(e => e.name.startsWith('Node_Exit'));

	return [room, entryNode, exitNode];
}

And I’m trying to attach multiple of these rooms like so:

_preGenerate(count) {
	let rooms = [];
	for (let i = 0; i < count; i++) {
		const room = this._generateRoom('endless_straight3'); // Returns an array as [Room, EntryNode, ExitNode]
		this._stage.add(room[0]);

		room[0].updateMatrixWorld();
		if (rooms.length > 0) {
			this._exitNode.updateMatrixWorld(true);
			this._exitNode.getWorldPosition(room[0].position);
		} else {
				room[0].position.setScalar(0);
		}
		this._exitNode = room[2];

		console.log(room[0].position);
		rooms.push(room);
	}
}

However, the issue is that at this._exitNode.getWorldPosition(room[0].position); - it always returns (seemingly) the local postion of the exit node - THREE.Vector3(0, 0, 46)

I expect it to return the position of the exit node based on the world position - so 46 for the first room, 92 for the second room, 138 for the third, and so on.
Not sure what’s going wrong here, any help is appreciated!

cc. https://stackoverflow.com/questions/56129746/trouble-getting-world-position-of-object-in-three-js

There seems to be few misunderstandings here.

First of all, .getWorldPosition(room[0].position); is mapping exitNode world position to its parent position. Which does not seem like what you want at all. The argument inside .getWorldPosition is the target not the frame of reference, if you will.

In order to fix that, you just need to setup an empty target for your .getWorldPosition().

room[i].updateMatrixWorld();
//...
this._exitNode.updateMatrixWorld();

var target = new THREE.Vector3();
this._exitNode.getWorldPosition( target );
// target now contains exitNode world position

I’ve setup a JSFiddle that showcases what I meant. Hope it’s helpful.

Sorry, seems like my code wasn’t clear enough :slight_smile:
this._exitNode is actually the node of the previous room, the variable gets updated after it’s been used. Every room has it’s own entry & exit node. So I’m trying to align the position of room2 to the position of the exit node of room1 - two completely separate entities.
That’s what this._exitNode.getWorldPosition(room[0].position); was supposed to do - room[0] is the newly generated room, and this._exitNode is the exit node of the previous room. To my understanding, the resulting position of this._exitNode.getWorldPosition gets copied into the position of room[0], which sounds what I want… :thinking:

I see, your post is clear. I was the one misunderstanding your point, sorry about that.

If I understood everything correctly, your code logic is fine. The problem seems to be that you are not updating the previous room WorldMatrix. You update the exitNode on the next loop, but that has no effect, if you don’t update the room matrix.

const room = this._generateRoom('endless_straight3'); 
this._stage.add(room[0]);

if (rooms.length > 0) {
	this._exitNode.updateMatrixWorld(true);
	this._exitNode.getWorldPosition(room[0].position);
} else {
	room[0].position.setScalar(0);
}
room[0].updateMatrixWorld();
this._exitNode = room[2];

That should do the job, please let me know.

Wow, thanks! I’m glad I was on the right hutch :slight_smile: All works fine now. It was really just because was updating the worldMatrix before and not after. Thank you so much!

1 Like