Placing a model relative to another - libgdx

I was wondering if it's possible to position a model relative to another model? For example, I have a height map model that I generated. I would like to place a box in the center of the map, how would I do that?

Something like:
boxInstance.transform.set(mapInstance.transform).translate(x, y, z);
Where x, y, z is the location relative to the parent model instance.

Related

Finding the coordinates of a rotated object

I have 2 objects of interest for this problem.
Obj1 is the parent object, a circle sprite
Obj2 is a nested child object, a square sprite
Obj1 has a child called objHolder, inside objHolder is Obj2.
objHolder has its pivot point set to the middle of Obj1, and Obj2 is placed near the circumference of Obj1. The objective is to rotate objHolder so that Obj2 looks like it's hovering around the circumference of Obj1.
Every frame, objHolder would have a new rotation value base on some other input values from my interface.
My question is, how do I obtain the x,y coordinates of Obj2 (relative to Obj1, not the stage) every frame?
If I use localToGlobal() it would not take into account the rotation value. Is there an easier way?
Well, it should take rotation into account. You describe that you have a nested structure like this: Obj1 <- objHolder <- Obj2. Then, objHolder is located at center of visible Obj1, and Obj2 is offset from the center of objHolder. Now, if you give objHolder some rotation, you should see Obj2 rotate and travel a circle. Does it do this? If no, then your display list is not how you describe. If Obj2 does rotate but doesn't travel, then you have Obj2's pivot point at center of objHolder, move it away.
Anyway, the answer is to use both translations, first from source to stage, then from stage to target. If you want coordinates of one object in the system of another object, do this:
p=target.globalToLocal(source.localToGlobal(new Point()));
In your case, source is Obj2 and target is Obj1. And, new Point() is a point with coordinates (0,0) in the source object's coordinate system, aka the pivot point of source.

Libgdx - How to move object forward by direction?

Example: I have 3 object with 3 rotation so how can i move object forward by object's direction like that.
http://i.stack.imgur.com/pTWDf.png
If you are using sprites to hold to values of each object, set the x/y += direction you want the objects to travel. Otherwise, if you are drawing the image through a spritebatch you can use batch.draw(texture, x, y) and set the x and y coordinates that way.

How do I get the global point of a rotated object?

I'm trying to get the global point (x,y) of an object. This object is a child of a rotated parent, MovieClip. Without rotation of the parent, it's easy to find the global point of the child, parent.localToGlobal. However, when I'm rotating the parent, it seems that localToGlobal returns the incorrect x,y.
localToGlobal deals with position not transformation. You are simply using the wrong tool for the job. I'm guessing you don't want to deal with matrices but you gonna have to.
mydisplayobject.transform.pixelBounds;//global space occupied by object
mydisplayobject.transform.concatenatedMatrix//the matrix representing all combined transofrmations

Pan to specific X and Y coordinates and center image svg-pan-zoom

I am using the ariutta svg-pan-zoom library(https://github.com/ariutta/svg-pan-zoom).
I am trying to pan to a specific shape at (x:1000, y:20) when the user clicks a button. I would also like the image to centre at this point.
The Problem I am facing is the point I am trying to pan and centre at goes off the screen.
I am currently using:
panZoom.pan({x:1000, y:20});
but this does not work.
Please see the jsFiddle for more info: http://jsfiddle.net/arjSharma/n6r55dp1/2/
Can anyone help with my problem?
Thanks
I managed to answer my question:
I first pan to point 0,0 using the method:
panZoom.pan({x:0,y:0});
I then get the 'real Zoom' value by:
var realZoom= panZoom.getSizes().realZoom;
To pan and centre at specific x and y, use the pan() method and pass x and y coordinates like in the example below:
panZoom.pan
({
x: -(755*realZoom)+(panZoom.getSizes().width/2),
y: -(240*realZoom)+(panZoom.getSizes().height/2)
});
The '+(panZoom.getSizes().width/2)' and '+(panZoom.getSizes().height/2)' are responsible for adding the offset to the x and y coordinates which ensures the image is centred.
Feel free to ask any further questions regarding my answer.

localToGlobal/globalToLocal AS3 confusion

I want to move a display object from one container to another, but have it appear in the same place on screen.
I thought I'd understood this years ago, but the following does not work:
function moveToNewContainer(obj:DisplayObject, newParent:DisplayObjectContainer):void {
var pos:Point = new Point(obj.x, obj.y);
var currentParent:DisplayObjectContainer = obj.parent;
pos = currentParent.localToGlobal(pos);
currentParent.removeChild(obj);
newParent.addChild(obj);
pos = newParent.globalToLocal(pos);
obj.x = pos.x;
obj.y = pos.y;
}
This doesn't position the object in the same place as I would have expected.
Does anyone know what I am doing wrong, please?
Thanks,
James
Using localToGlobal/globalToLocal and setting the x and y properties like you showed calculates the correct position for the object in its new parent, but does not adjust for other aspects of the transformation such as scaling or rotation. In other words, the object's registration point will indeed remain in the same place, but the object may be rotated, scaled, or sheared differently.
The solution to your problem will need to take into account the transform.concatenatedMatrix properties of the old and new parents--you'll need to multiply the object's transformation matrix by one and then by the inverse of the other, or something along those lines. Leave a comment if you need help working out the math.
There is nothing wrong with your code, provided that both containers have no transformations applied. If your clips are scaled, rotated, etc.. you need to handle that in addition to the coordinate space transformations that localToGlobal and globalToLocal do.
You have to check if your containers are actually placed on stage. If your new container isn't added as a child to stage, function globalToLocal fails, just because it doesnt know how to correctly calculate that data.