Question

Please bear with me here - I will eventually get to a question...

I'm building a 3D space game and currently adding space stations (Orbitals) built up out of multiple meshes. The problem I seem to be having is that different sections seem to be overlapping, as if they are not offset away from each other correctly.

I have a super class, OrbitalSection, which is subclassed; Each subclass is defined with a different number of OrbitalJunctions, which each contain a Matrix3D object that determines the junction's position and orientation.

In the process of game initialisation, an initialising Orbital selects a first OrbitalSection and chooses which of that section's OrbitalJunctions will be used. To determine the orientation of this OrbitalJunction, I'm using...

chosenJunction.matrix.append(chosenSection.matrix);

(Both OrbitalSection and OrbitalJunction have a 'matrix' method that returns its internal Matrix3D object). I pass the above returned Matrix3D orientation into a 'connectToJunction' method on a new, randomly selected, OrbitalSection.

So, here, finally, is my question:
Is the following code, in the OrbitalSection Class, logical?

public function connectToJunction(p_sourceOrientation:Matrix3D):void {
    // _junctions is an Array of OrbitalJunctions...
    var chosenJunction:OrbitalJunction = _junctions[Math.floor(Math.random() * _junctions.length)];
    var invertedMatrix:Matrix3D = new Matrix3D();
    invertedMatrix.copyFrom(chosenJunction.matrix);
    invertedMatrix.invert();
    // the following lines re-position the inverted, rotated matrix back to the junction's original position...
    invertedMatrix.position.x = chosenJunction.matrix.position.x;
    invertedMatrix.position.y = chosenJunction.matrix.position.y;
    invertedMatrix.position.z = chosenJunction.matrix.position.z;

    _offsetMatrix.copyFrom(invertedMatrix); // << this is this OrbitalSection's Matrix3D object that the 'matrix' method returns
    _offsetMatrix.append(p_sourceOrientation); // the 'external' orientation is appended to this object's _offsetMatrix because the passed parameter in the Matrix3D .append method is the left-hand-side Matrix3D
}

My logic is that, for the 2 junctions to 'plug' into each other, they've got to match up in the same direction; so surely ONE of them has to be inverted.

Ideally the new OrbitalSection's _offsetMatrix (and any other added OrbitalSections) should be set so that it describes a position and rotation RELATIVE to the Orbital's 0, 0, 0 position and 0, 0, 0 rotation.

So, does inverting the chosen junction's _offsetMatrix in this way make sense?

And, by the way, I mark each OrbitalJunction as being 'used' when it's chosen; 'used' junctions are excluded from the list of available junctions in the build process, so the 'overlapping' problem isn't simply that more than one OrbitalSection is being added to the same junction - even though that's slightly how it looks in-game. Cheers.

Was it helpful?

Solution

I have figured out the problem. I needed to 'flip' the Matrix3D of the OrbitalJunction of the first OrbitalSection (by 180 degrees) and translate it to its negative coordinates - that would both re-orient it so another junction could 'plug' into it AND reposition it so that its position WITHIN the OrbitalJunction was offset by the correct amount. Then I needed to .inverse() the Matrix3D of the second OrbitalJunction and apply this to the Matrix3D of the second OrbitalSection. This is necessary, for the the OrbitalJunction to be matched up to 0, 0, 0 rotation, its containing OrbitalSection needs to be rotated, and moved in the opposite directions. Just in case this is useful for anyone wanting to position 3D objects relative to each other...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top