Author Topic: Object3D.getTransformedCenter() of Current Mesh  (Read 5445 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Object3D.getTransformedCenter() of Current Mesh
« on: September 10, 2009, 11:09:14 pm »
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?

Code: [Select]
   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*/
    }

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D.getTransformedCenter() of Current Mesh
« Reply #1 on: September 11, 2009, 02:01:04 pm »
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...

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Object3D.getTransformedCenter() of Current Mesh
« Reply #2 on: September 11, 2009, 05:48:39 pm »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D.getTransformedCenter() of Current Mesh
« Reply #3 on: September 11, 2009, 06:18:23 pm »
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.
« Last Edit: September 11, 2009, 06:21:29 pm by EgonOlsen »

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Object3D.getTransformedCenter() of Current Mesh
« Reply #4 on: September 11, 2009, 06:50:05 pm »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Object3D.getTransformedCenter() of Current Mesh
« Reply #5 on: September 11, 2009, 07:02:42 pm »
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.

Code: [Select]
    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*/
    }

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Object3D.getTransformedCenter() of Current Mesh
« Reply #6 on: September 11, 2009, 07:21:50 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D.getTransformedCenter() of Current Mesh
« Reply #7 on: September 11, 2009, 07:29:28 pm »
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.


Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Object3D.getTransformedCenter() of Current Mesh
« Reply #8 on: September 11, 2009, 07:35:37 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Object3D.getTransformedCenter() of Current Mesh
« Reply #9 on: September 11, 2009, 07:38:17 pm »
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...