Object3D.getTransformedCenter() of Current Mesh

Started by AGP, September 10, 2009, 11:09:14 PM

Previous topic - Next topic

AGP

One of my models unfortunately moves forward on his own (as opposed to simply moving its legs) so that, obviously, it moves back when the animation loops. The following method doesn't work because the center is the same for both the start and end of the animation (I'm guessing it's right in the middle of the extremes). Is the answer along the lines of a VertexController or is there a simpler way to get different centers for both the first and last frames?


   private void advance() {//THEY WALK FORWARD BY THEMSELVES, THEN, WHEN ANIMATION LOOPS, GO BACK TO THEIR STARTING POINT...
model.calcCenter();
SimpleVector currentPosition = model.getTransformedCenter();
model.animate(animationPosition, currentAnimation);
model.calcCenter();
moveTowards(model, currentPosition, 1f);/**THIS WOULD WORK IF model.getTransformedCenter() DIDN'T RETURN THE SAME VALUE AS currentPosition*/
   }

EgonOlsen

Maybe the center of the bounding box would be sufficient? In that case, you could query the mesh for the bb and calculate the center from the returned bb by yourself (pretty easy, just average the min and max values) and transform this value into world space. However, calculating the bb isn't cheap...it's not very expensive either, but it should be avoided if possible.
If this is not sufficient, you may try to call calcCenter() instead every time you need the value, but that's even more expensive.
Or use the PolygonManager to calculate the average of all transformed vertices. Maybe that's the best option...

AGP

I did call calcCenter(). It didn't help. You're saying the PolygonManager would be the fastest option? And if I'm only going to do this once every two or three seconds (advance() only gets called when the animation loops), does it matter?

EgonOlsen

#3
I don't see a reason why calcCenter() shouldn't help. It works directly on the object's mesh data, which is modified during the animation. I just did a quick test and the center changes as expected during the animation. It may not be the center you want though because it's just an average of all vertices.

AGP

That's very odd because I've printed the results of model.getTransformedCenter() after animate() and calcCenter() and before animate() and they're the same for my model. I'm going to play around with it (remove strip() for one thing) and tell you what I get.

AGP

My methods are really very simple and I even added some redundant things to make sure I'm on the right frames and the centers are still always the same. I'm e-mailing you the model so you can see for yourself.


    public void animate() {
if (animationPosition < 1f) {
    animationPosition += animationIncrement;
    model.animate(animationPosition, currentAnimation);
}
else if (keepLooping) {
    animationPosition = 0.00f;
    advance();
}
else model.setVisibility(false);
    }

    private void advance() {
//ZOMBIES WALK FORWARD BY THEMSELVES, THEN, WHEN THE ANIMATION LOOPS, GO BACK TO THEIR STARTING POINT...
model.calcCenter();
model.animate(1, currentAnimation);//THIS IS REDUNDANT AND SHOULD BE REMOVED
SimpleVector currentPosition = model.getTransformedCenter();
System.out.println("\nCurrent Position: "+currentPosition);
model.animate(animationPosition, currentAnimation);
model.calcCenter();
System.out.println("Current Position: "+model.getTransformedCenter());
moveTowards(model, currentPosition, 1f);/**THIS WOULD WORK IF model.getTransformedCenter() DIDN'T RETURN THE SAME VALUE AS currentPosition*/
    }

AGP

You know what I think it is? QTip, my MD2 exporter, is wrapping the animation so that the last frame is actually one of the first ones.

EgonOlsen

Maybe...i've tried the animation and there seems to be a strange frame in it somehow that is visible for a frame or two. However, the center itself changes nicely. I've simply dropped the model into the advanced example from the wiki: http://www.jpct.net/wiki/index.php/Advanced_example

Maybe that helps. Something is fishy with the model anyway...i can't load it in my md2viewer, it crashes the viewer application.


AGP

Works for me and I wrote a bunch of jPCT viewers myself. Could yours crash because I didn't send you the texture? I'm checking the frame list on the model now, I'll post if I find that it's the model, and will have a look at your example.

EgonOlsen

It's not a jPCT based viewer. It's a native application written in C by somebody else. I use it to verify that my own stuff is correct. In this case, my own stuff can at least load the model without crashing...