Quad placement on different resolutions

So, my problem is, I’ve got a xml file which I’ve parsed. I’ve gotten x,y, width, and height for these objects (based on their upper left point) from the xml. Now, I want to scale these objects to any resolution when attaching them to the guiNode. I’m using quads to represent them.

If I default to 640x640, which is the resolution I used in Tiled to make the map, it looks perfect:

But if I try and do my scaling, it comes out like this:

So basically, I think I need help with a) my math and b) my understanding of quads.

Here is the code that places the images in:

[java]for(int i = 0; i < objects.size(); i++){
Geometry q = ((Geometry)((Object[])objects.get(i))[0]);
float x = ((Integer)((Object[])objects.get(i))[1]);
float y = ((Integer)((Object[])objects.get(i))[2]);
//System.out.println(x + " " + y);

        q.setLocalScale(app.settings.getWidth()/(32f*20f),app.settings.getHeight()/(32f*20f),0);    //scale width and height by factor
        q.setLocalTranslation((x/(20f*32f)) * app.settings.getWidth(),(((32f*20)-y)/(32f*20f))*app.settings.getHeight()-32,0);
        //System.out.println(q.getLocalTranslation() + "  " + q.getLocalScale().y);
        guiNode.attachChild(q);
    }[/java]

And here is the code/math that places the red quads of arbitrary size in (with the defined x,y,width,height from top left corner):

[java]for(int i = 0;i < collide.size(); i++){
if(((TiledObjectGroup)collide.get(i)).getName().equalsIgnoreCase(“collision”)){
CollisionGroup cg = (CollisionGroup)collide.get(i);
for(int j = 0; j<cg.getGroup().size();j++){
float x = ((ObjectDetails)cg.getGroup().get(j)).getX();
float y = ((ObjectDetails)cg.getGroup().get(j)).getY();
float w = ((ObjectDetails)cg.getGroup().get(j)).getWidth();
float h = ((ObjectDetails)cg.getGroup().get(j)).getHeight();

                x = (x/(20f*32f)) * app.settings.getWidth();     //Divide x by original width by original width, then multiply with new width
                y = ((1- (y/(32f*20f)))*app.settings.getHeight())-h; // Divide 1 - y/original height (to flip y),   then mulitply by new height
                w = w * app.settings.getWidth()/(32f*20f);     //Scale width
                h = h * app.settings.getHeight()/(32f*20f);     //Scale height
                 
                System.out.println(w + " " + h + " " + x + " " + y);
                
                
                Quad b = new Quad(w,h); // create cube shape
                Geometry geom = new Geometry("Box", b);  // create cube geometry from the shape
                Material mat = new Material(assetManager,
                  "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
                mat.setColor("Color", ColorRGBA.Red);   // set color of material to blue
                geom.setMaterial(mat); 
               // geom.addControl(rbc);
                Node box = new Node("box");
                //geom.scale(w,h,0);
                box.attachChild(geom);
                
                //bas.getPhysicsSpace().add(rbc);
                
                guiNode.attachChild(box);
                box.setLocalTranslation(x,y,0);
                
            }
         }
    }

[/java]

Anyone know what I’m doing wrong?

I’m not sure what’s wrong between the two images.

Though you can perhaps simplify your life a lot by creating one node that will be your parent (attach that to the guiNode) and scale it like you want. Then just attach the other objects using their native coordinates and they will all be scaled appropriately. No math involved.

Well if u look at the bottom picture, the tiles are cutoff and some of the bigger red boxes are off.

Now with your suggestion. I don’t understand. You can scale a node? And are u saying I should be able to attach everything to that node ( and the normal x,y,width,height) and then I can scale everythings width and height? Oh wait, is setLocalScale a method of a spatial (in between classes now, no computer to look at)?

Wondering… if you can get to the forum then can you not also see the javadocs in the upper left of the forum?

http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#setLocalScale(float)

Also, in the upper right: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

Don’t be insulted by the name… but it goes over a lot if the topics that are sort of crucial for a scene graph, like local coordinate spaces (including scaling, translation, rotation) and so on.

Well i didnt try, but u are right, i can access the javadocs. My bad.

Also, Im not one to met my pride to get in the way of progress. I’ll look at that again.

So, is this thinking right? Get each quad. Attach it to the parent node at the original x,y and w,h. Then, when all quads collected, scale to settings width and height

@machspeeds said: Well i didnt try, but u are right, i can access the javadocs. My bad.

Also, Im not one to met my pride to get in the way of progress. I’ll look at that again.

So, is this thinking right? Get each quad. Attach it to the parent node at the original x,y and w,h. Then, when all quads collected, scale to settings width and height

If they are attached to a common parent node and that parent node is scaled then it’s like creating a new “space” where coordinates and size mean different things. So, I think the answer is yes. If I understood the question.

Perfect. I’ll try this out in about 3 hours and report back. Thanks for the help so far ( so far IF it doesn’t work , which I’m sure won’t be the case)

Alright, reporting back.

It worked. After a little playing around with the pixel space underneath the title bar for perfect alignment, I’ve got a nice parser that takes in a file from Tiled, and creates a map that scales to any resolution:

The red things are “collision” objects I defined.

Now, I know this is a question for a different thread, but are rigidbodies possible in the guiNode? I would like to define all those red areas actually as rigidbodies, but I don’t know if that can be done, because it may involve mixing wu and pixels.

While probably possible bullet is a 3d engine, meaning you will get all kinds of problems (eg rotations/velocity on two more axes) that are not fitting for 2d.

I suggest to take a look into http://www.jbox2d.org/
As it might be a better alternative for this.

Hmm, have you used jbox2d before? How easy is it too use? I’m scanning the quickstart right now, but I’m kind of reluctant to include a whole new engine when all my game will consist of is running and jumping.

@machspeeds said: Hmm, have you used jbox2d before? How easy is it too use? I'm scanning the quickstart right now, but I'm kind of reluctant to include a whole new engine when all my game will consist of is running and jumping.

If you don’t have real physics and only character-platform physics then any physics engine may be slightly overkill.

After all, the basic physics equations are really simple. It’s realistic collision detection and collision response that is tricky. If you don’t want objects bouncing and rotating and stacking realistically then just:
velocity = velocity.addLocal(acceleration.mult(tpf));
pos = pos.addLocal(velocity.mult(tpf));

…or whatever… and set velocity to zero when you collide with something under your feet. (I’m oversimplifying just slightly.) Otherwise, better to use a 2D physics engine for 2D physics… a) you won’t be constantly fighting it, and b) it will perform 100x better because it’s dealing with a simpler problem space. (simpler by an order of magnitude)

Alright, thanks for the info. I was prepared for that, though. I was seeing what I could do for the sake of writing of Tiled parser for JME3. I may just do it in Quintus (HTML5/JS library) and then try and recreate the game in JME3, for the sake of learning :slight_smile: