I am trying to set up scaling of my Libgdx Actors which are also using Spine functionality. Each Actor has a Skeleton and a SkeletonRenderer to be drawn during runtime. Having done it like this, the Actor itself needs to have a position, a width and a height to be drawn. I thus need to do some scaling to get stuff straight for input detection and other detections. See attached image for an example of a simple bomb. What I am doing now is:
Set a bounding box attachment on the bomb, called actor_bb. I want this bounding box to work as the area where input can be detected and thus also the position, width and height of the actor.
I set the bounds of the Actor (the Bomb) using the vertices of the bounding box:
/**Sets the bounds of the Actor based on the vertices of the actor_bb */ private void setActorBounds(BoundingBoxAttachment actor_bb){ float[] vertices = actor_bb.getVertices(); float minX = Float.MAX_VALUE; float maxX = Float.MIN_VALUE; float minY = Float.MAX_VALUE; float maxY = Float.MIN_VALUE; /*Iterates vertices to find the bounds. The vertices are ordered by x, y, x, y ...*/ for( int i = 0; i < vertices.length; i++ ){ if ( i % 2 == 0 ){ // i is even, coordinate is an x coordinate if ( vertices[i] <= minX ) minX = vertices[i]; if ( vertices[i] >= maxX ) maxX = vertices[i]; } else{ // i is odd, coordinate is a y coordinate if ( vertices[i] <= minY ) minY = vertices[i]; if ( vertices[i] >= maxY ) maxY = vertices[i]; } } setBounds(minX, minY, maxX - minX, maxY - minY); }
I calculate and set a scaling factor based on this bounding box and a physical height (i.e. height in meters):
/**Scales the ActorBaseClass in accordance with its physical height (in meters) and the height of the screen (in meters).*/ @Override public void animatableScale() { float heightInPixels = GameParameters.ViewportParameters.PPM * parameters.height; float scalingFactor = heightInPixels / this.getHeight(); this.setScale(scalingFactor); rootBone.setScaleX(scalingFactor); rootBone.setScaleY(scalingFactor); skeleton.updateWorldTransform(); }
This works to some extent and the scaling factor is based on the height of the bounding box for the Actor. However, the bounds of the Actor seems to start at the center point of the Bomb and stretches to the right of it, as if the bounds are moved "50 %" to the right. Maybe it has something to do with Actor coordinates versus Skeleton coordinates?
Are there any better ways of doing this? It seems a bit silly to mix Actor and Spine stuff like this, but I cannot readily see how to do it otherwise. Please tear this approach apart end enlighten me on better ways to do this! Any comments are very appreciated