ANDEngine GLES 2.0 : Sprite X and Y not updated after Scaling - android

The issue is when i up or downscale the sprite at its centre, sprite X and Y are not updated, although its drawn correctly on the screen . You can test this issue with the following code:
Rectangle rect=new Rectangle(CAMERA_WIDTH/2,CAMERA_HEIGHT/2,100,100,this.getVertexBufferObjectManager());
rect.setScaleCenter(rect.getWidth()/2, rect.getHeight()/2);
rect.setScale(1.5f);
The new X and Y should match the scene coordinates, but this doesnot happen

The X and Y postion of a sprite should NEVER update in response to a scale change. The x and y are measured from the transform center of the sprite, essentially its local 0,0. So no scale multiplier will ever change that transform root to anything other than 0,0.
You may see the object appear to move left or right in response to a scale up or scale down, but that is because the bounding box of the sprite is enlarging or shrinking. But the point from which that transform is changing does not change, so X and Y stay the same.

Related

Y axes has unexpected value when drawing rectangle

I am drawing a rectangle using the drawRect method. I use x, y to give initial values for the rectangle. And these values are x = 150, y=300.
So I've got this:
Now I made the printing of coordinates in the log when touching the screen.
The problem is when I am touching the up left corner of rect (150, 300) I'm getting next values in the log:
D/TOUCH:: x=153, y=370
It is the correct value for X but incorrect for Y because as said I put initial Y==300. My question is why it is so?

How to pinch zoom within finger point?

Right now I am using this code as custom layout in android studio. But the large problem is that when pinch-to-zoom, it will zoom into the point 0,0 ,instead of zooming into the finger point.
https://gist.github.com/anorth/9845602
EDIT: Forked the original gist and updated it, view it here. It was a little more complex than I thought. The default pivot point for scaling a view is the center of the view. In order to make this work I had to set the view pivot to (0,0) and the adjust the translation for the pivot from the user's gesture.
I noticed in your onScale() override you are calling scaleDetector.getScaleFactor() but you are not calling scaleDetector.getFocusX() and scaleDetector.getFocusy(). These methods tell you what the focal point of the pinch-zoom is. You will have to call these methods and get this focal point (x,y) then do some calculations to figure out the translation required to keep that focal point. You will have to work this into your dx and dy values so that the translation accounts for the focal point.
To calculate the translation, you would take the focal point and multiply the x and y values by the change in scale factor. This tells you how far the point would move given focal point (0,0). So you would subtract x(updated) from x(original) and y(updated) from y(original) to get that move vector. Your translation values are the negated vector values. So:
scale(delta) = scaleDector.getScaleFactor() -- just the change, not the new scale factor
translation x = x(focal) - x(focal) * scale(delta)
translation y = y(focal) - y(focal) * scale(delta)
So obviously you would add these values to your dx and dy values for translation.

Quadrant 3 positions in a quadrant 4 imageview

I'm putting together a demo of indoor positioning technology for an Android device. The problem is that I am getting quadrant 3 positions from the system I'm using, where 0,0 origin is in the upper right. The map is placed in an imageview that has the origin in the upper left (quadrant 4).
Naturally, when I try to put another image on top to move around according to the position I am getting, it is backwards on the X-axis. What is the best way to display the right coordinates? Can I transform the points as I get them, or is there a way to somehow flip the imageview so that the origin is in the upper right?
You will have to transform the coordinates yourself. It is not complex - mapping the values from one to the other, in general. If the image has a width of "Wo" and you have Vo (View Origin) and Mo (Map Origin) then an x coordinate from the Map is Mx and is transformed as Vx = Mx + Wo. (Mx are map x coordinates that have negative values, no greater than Wo and no less than 0). To transform from the view to the map, do the opposite: Mx = Vx - Wo. (The Vx x-coordinates will not have values less than 0, and at its greatest width, it should result in a "0" on the map.)
That is not CPU intensive, so it should be fast.

Hairline graph after matrix transformation

I'm testing the drawing of an XY graph on my Android tablet. It's a Samsung Galaxy Tab 2 (7") running ICS.
I've created a View subtype with an overridden onDraw method. Its job is to simply plot an array of (x,y) coordinates as a series of connected line segments. I've got a float array representing the y values, and the x values are the array indices. The y values extend from -1 to 1 and there are about 10 values. Pretty simple.
The target canvas is a square on the screen, say about 480 by 480 pixels, with +1 intended to be at the top of the screen and -1 at the bottom, and the 0th value at the extreme left and the Nth value at the extreme right.
Thus, the transformation from "world coordinates" to "screen coordinates" along the X and Y axes is not uniform. In my onDraw method, I apply a translate, a scale, and then another translate operation to the Canvas object, and then I proceed to draw the line segments using a Paint pen having a hairline stroke width of 0.
The result is a graph that's not hairline in width. Obviously, my scale operation is thickening the line segments so that gently sloping lines appear thicker than steep ones. When I change my world-coordinates extents so that they're equal along both axes (to match the square canvas), then this problem disappears.
Interestingly, this problem occurs on the tablet, but not on the Android ICS emulator.
Any thoughts on this would be appreciated. My preference is to have a hairline graph no matter what the transformation is.
The obvious work-around to this issue is to leave the canvas's matrix untouched and perform the translate/scale/translate operations myself (converting from world to screen coordinates), and then use the screen coordinates for drawing, utilizing a Paint pen with a width of 0.

finding specifically where an object is for collision detection purposes

I have a square that rotates to a random angle and then travels in a straight line in the direction it is pointing. It does this by using a variable as its x axis and then calling
Variable++
Each frame.
unfortunatley i cannot work out how to return the exact position of the square because the square can be travelling at any angle and therefore doesn't rigidly follow the world coordinte grid. This means that the x variable is not the shapes x coordinate.
How do i return the shapes exact coordinates and how do i do it in such a way that i can have two squares drawn from the same class behaving differently.
So you've got a measure of distance from where the object started along its internal sideways axis and a measure of the angle between that axis and the horizontal?
If so then the formula you want is simple trigonometry. Assuming the object started at (x, y) and has travelled 'distance' units along an axis at an angle of 'angle' with the horizontal then the current position (x', y') is:
x' = x + distance * cos(angle)
y' = y + distance * sin(angle)
If you have the origin in the lower left of the screen and axes arranged graph paper style with x increasing to the right and y increasing as you go upward, that assumes that the angle is measured anticlockwise and that the object is heading along positive x when angle is zero.
If you'll permit a hand waving explanation, the formula works because one definition of sine and cosine is that they're the (x, y) coordinates of the point on the outside of a unit circle at the angle specified. It also matches with the very first thing most people learn about trigonometry, that sine is 'opposite over hypotenuse', and cosine is 'adjacent over hypotenuse'. In this case your hypotenuse has length 'distance' and and you want to get the 'opposite' and 'adjacent' lengths of a right angled triangle that coincides with the axes.
Assuming Android follows J2SE in this area, the one thing to watch out for is that Math.sin and Math.cos take an angle in radians, whereas OpenGL's rotatef takes an argument in degrees. Math.toDegrees and Math.toRadians can do the conversion for you.
When you made the shape you should have already specified its X & Y coordinates. Im not too sure what you mean when you say you cant find the coordinates?
Also make sure you do fame independent movement; currently you are adding one to your variable on every loop of your program. This means if it runs a 60 Frame Per Second(FPS) it will move 60 units, but if it runs at 30FPS it will move at half the speed

Categories

Resources