www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: AGP on June 21, 2018, 06:24:00 am

Title: Asymmetrical Scaling
Post by: AGP on June 21, 2018, 06:24:00 am
I'm trying to create a flickering glow. To that end, I wrote the following thickening method in a vertex controller. The problem happens when the base object, from which the glow object is created, is rotated because the glow appears to be modified in worldspace as opposed to in objectspace. What should I do to calculate the x/z distortion in objectspace?

Code: [Select]
     public void thicken(final float deltaTime) {
SimpleVector[] vertices = this.getSourceMesh();
SimpleVector[] destination = this.getDestinationMesh();
for (int i = 0; i < vertices.length; i++) {
     SimpleVector v = vertices[i];
     v.x *= (1f+deltaTime);
     v.z *= (1f+deltaTime);
     destination[i] = v;
}
this.updateMesh();
     }
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 21, 2018, 09:07:26 am
The vertex controller works in object space, not in world space. What exactly is the problem?
Title: Re: Asymmetrical Scaling
Post by: AGP on June 21, 2018, 06:04:45 pm
Obviously, that it's scaling in the wrong axis. But only in my real-world use. The test in which I didn't rotate the object worked fine.
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 21, 2018, 10:04:01 pm
Must be something else then. The controller works in object space. I don't see how this code snippet alone can cause wrong scaling unless the object is somehow rotated around Z and your Z-scaling should actually be Y.
Title: Re: Asymmetrical Scaling
Post by: AGP on June 21, 2018, 10:06:48 pm
That's weird. I actually have two different, possibly related, problems. The second one is that the glow object, though both parallel and near the original object, is not in the exact same space as the original object. Yet I initialize it like so:

Code: [Select]
glow = new Object3D(obj, false);

Do you have to call build() on the glow object? Also, does getTransformedCenter() always consider the translations of all parents? Because I can't place the glow over the glower even with:

Code: [Select]
SimpleVector to = obj.getTransformedCenter();
SimpleVector o = glow.getTransformedCenter();
glow.translate(to.x-o.x, to.y-o.y, to.z-o.z);
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 22, 2018, 08:27:26 am
Yes, you have to call build and yes, getTransformedCenter() should apply all parent transformations.
Title: Re: Asymmetrical Scaling
Post by: AGP on June 22, 2018, 10:11:29 am
Then why doesn't that snippet place the glow over the glower? Can you think of a possible solution?

Edit: I should mention that the original object was created with ExtendedPrimitives.createCylinder(...).
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 24, 2018, 04:52:18 pm
Maybe because the transformation as a whole is different (some additional rotation involved)? Try to offset the glow by a scaled version of the results from get?Axis() instead.
Title: Re: Asymmetrical Scaling
Post by: AGP on June 24, 2018, 09:27:10 pm
There's nothing. This is how glow is created. I even added the setRotationMatrix() call to see if that would help, but nothing changed.

Code: [Select]
if (deltaGrowth > 1.00f) {
     obj.removeChild(glow);
     theWorld.removeObject(glow);
     glow = new Object3D(obj, false);
     glow.setTexture("Glow");
     glow.setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
     glow.build();
     glow.setRotationMatrix(obj.getRotationMatrix());
     moveTo(glow, obj.getTransformedCenter());
     glow.setTransparency(100);
     deltaGrowth = 0.00f;
     obj.addChild(glow);
     theWorld.addObject(glow);
     vController = new VertexController(glow);
}
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 25, 2018, 08:38:12 am
Have you checked which vealue you are actually getting from getTransformedcenter() before and after doing the additional translation. Something has to be there.
Title: Re: Asymmetrical Scaling
Post by: AGP on June 25, 2018, 08:32:46 pm
This is what I get:

Quote
Glow before build(): (0.20492004,0.06320784,0.28592888)
Glow after build(): (0.20492004,0.06320784,0.28592888)
Glow after translation: (0.31077987,0.6419327,1.6769919) blade: (0.31077987,0.6419327,1.6769919)
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 26, 2018, 09:04:23 am
Looking at your code snippet again, I noticed that you set the glow's rotation matrix to the one of the object. I'm not sure if that's a good idea, because it sets the exact some instance. I might be better to make one the child of the other or do a cloneMatrix() on that rotation matrix before setting it. Maybe that helps.
Title: Re: Asymmetrical Scaling
Post by: AGP on June 26, 2018, 09:21:29 am
No, that was my failed attempt to solve the problem. It doesn't work with or without it.
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 26, 2018, 10:29:19 am
Do you have a screen shot? Looking at the log output, it's pretty hard for me to imagine the actual problem.
Title: Re: Asymmetrical Scaling
Post by: AGP on June 26, 2018, 07:33:34 pm
https://www.dropbox.com/s/0lfiemqtu7um5bh/GlowDisplacement.jpg?dl=0 (https://www.dropbox.com/s/0lfiemqtu7um5bh/GlowDisplacement.jpg?dl=0)
Title: Re: Asymmetrical Scaling
Post by: AGP on June 27, 2018, 04:25:34 am
Have a look at this. The further ahead I move Luke, the further up the glow goes. I've tried, unsuccessfully, rotating getTransformedCenter() of the blade, but it's still wrong.

https://www.dropbox.com/s/ouy2edwxaaxuyfr/TheFurtherAheadTheFurtherUp.webm?dl=0 (https://www.dropbox.com/s/ouy2edwxaaxuyfr/TheFurtherAheadTheFurtherUp.webm?dl=0)
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 27, 2018, 12:55:37 pm
Maybe the location of the glow in object space is off? What are the bouding box values for the glow and the blade?
Title: Re: Asymmetrical Scaling
Post by: AGP on June 27, 2018, 07:04:58 pm
It would've been crazy if they weren't in the same place.

Quote
******Blade Bounds:
0.18368883, 0.22368883
-1.0867921, -0.08679211
0.9159291, 0.95592904
******

******Glow Bounds:
0.18368883, 0.22368883
-1.0867921, -0.08679211
0.9159291, 0.95592904
******
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 28, 2018, 09:10:12 am
Soo...the bounding boxes are in the same location, the transformed center is...yet the visual outcome doesn't match!? I'm not sure what's wrong here. Have you tried to replace these objects by some primitives just to see if it's really not something with the objects themselves?
Title: Re: Asymmetrical Scaling
Post by: AGP on June 28, 2018, 10:05:23 am
The original object, blade (obj in the Glow class) is an ExtendedPrimitive. Glow itself is a copy of blade (new Object3D(obj, false)).
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 28, 2018, 01:06:26 pm
I really don't know then. In this screen shot, which object is the child of which?
Title: Re: Asymmetrical Scaling
Post by: AGP on June 28, 2018, 06:38:11 pm
The glow object is the child of blade (obj in Glow class).
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 28, 2018, 11:25:45 pm
What happens if you call clearRotation() and clearTranslation() on the glow object?
Title: Re: Asymmetrical Scaling
Post by: AGP on June 29, 2018, 07:49:09 am
ClearRotation() doesn't do anything (and it shouldn't, since I interpolate the matrices of the lightsaber and the skeletal structure's hand in the loop). ClearTranslation fixed its initial position, thanks very much.

Now, I'm having a hard time keeping glow in the same place. I've tried every permutation of the following method of which I could think:

Code: [Select]
     public void thicken(final float deltaTime) {
SimpleVector[] vertices = this.getSourceMesh();
SimpleVector[] destination = this.getDestinationMesh();
final SimpleVector c = new SimpleVector(obj.getTransformedCenter());
float minX = Float.MAX_VALUE, minZ = Float.MAX_VALUE, maxX = Float.MIN_VALUE, maxZ = Float.MIN_VALUE;
for (int i = 0; i < vertices.length; i++) {
     SimpleVector v = new SimpleVector(vertices[i]);
     v.x *= (1f+deltaTime);
//      v.x -= deltaTime*.5f;
     v.z *= (1f+deltaTime);
//      v.z -= deltaTime*.5f;
     destination[i] = v;
     if (minX > v.x)
minX = v.x;
     if (maxX < v.x)
maxX = v.x;
     if (minZ > v.z)
minZ = v.z;
     if (maxZ < v.z)
maxZ = v.z;
}
this.updateMesh();
SimpleVector d = new SimpleVector((minX-maxX), 0f, (minZ-maxZ));
obj.translate(d);
// obj.clearTranslation();
// obj.calcCenter();
// Glow.moveTo(obj, c);
// Glow.moveTo(obj, d.calcSub(c));
     }
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on June 30, 2018, 04:38:59 pm
I'm not sure, if I understand...if the glow is a child of the saber...then why doesn't it follow it properly?
Title: Re: Asymmetrical Scaling
Post by: AGP on July 01, 2018, 01:41:03 am
Because I'm altering its vertices with the VertexController.
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on July 02, 2018, 07:44:16 pm
Ok...so it works without that vertex manipulation but with it (to make the glower larger), it doesn't in some way. Is that the problem? I somehow lost track here...
Title: Re: Asymmetrical Scaling
Post by: AGP on July 03, 2018, 01:50:16 am
It works both ways, but it's getting displaced as it gets wider. Read that last version of thicken() and you'll see my many attempts to keep the center in the same place.
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on July 03, 2018, 06:25:03 pm
Maybe because your calculations assume that the center is at the origin? I would try this: Take the vertex, translate it by the negated center, scale it and translate it back by the center.
Title: Re: Asymmetrical Scaling
Post by: AGP on July 04, 2018, 02:56:55 am
This still displaces it, but in a different direction:

Code: [Select]
     public void thicken(final float deltaTime) {
SimpleVector[] vertices = this.getSourceMesh();
SimpleVector[] destination = this.getDestinationMesh();
final SimpleVector c = new SimpleVector(obj.getTransformedCenter());
for (int i = 0; i < vertices.length; i++) {
     SimpleVector v = new SimpleVector(vertices[i]);
     v.x -= c.x;
     v.y -= c.y;
     v.z -= c.z;
     v.x *= (1f+deltaTime);
     v.z *= (1f+deltaTime);
     v.x += c.x;
     v.y += c.y;
     v.z += c.z;
     destination[i] = v;
}
this.updateMesh();
     }
Title: Re: Asymmetrical Scaling
Post by: AGP on July 04, 2018, 03:06:45 am
On the other hand, this time it may well be a Java bug:
Quote
Why does array[idx++]+=“a” increase idx once in Java 8 but twice in Java 9 and 10?
https://blog.takipi.com/stack-overflow-answers-the-top-java-questions-we-didnt-know-we-had/ (https://blog.takipi.com/stack-overflow-answers-the-top-java-questions-we-didnt-know-we-had/)
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on July 04, 2018, 10:24:33 am
Not the transformed center, just the center.
Title: Re: Asymmetrical Scaling
Post by: AGP on July 05, 2018, 08:50:40 pm
That works, thank you. But it begs the question: why would an Extended Primitives cylinder object space not be at the origin?
Title: Re: Asymmetrical Scaling
Post by: EgonOlsen on July 05, 2018, 09:59:07 pm
Not sure. I think it's created with the origin as the base line, not the center.