Bouncing back on the edges - android

I am trying to make a small game where you can fling a coin on the screen. while i was searching for that kind of animation i found this page
http://mobile.tutsplus.com/tutorials/android/android-gesture/
I managed to customise the events according to my aim, but i couldn't manage to set boundaries on the screen so that the coin(bitmap) will bounce back when it comes to the edges.
I tried several calculations but in the end the coin moves weirdly based on the contact points on the edges.
Can someone help me about it, according to the working code on the website

Below is a sample code from my pong game. Each object has position (x,y) and its speed. Movement is speed applied to position. One extra info is that the screen coordinate system starts from the left top corner. Y axis go down but increasing. Since screen coordinates start from 0, the boundaries become screen.width -1 and screen.height -1
#Override
public void onDraw(Canvas c){
p.setStyle(Style.FILL_AND_STROKE);
p.setColor(0xff00ff00);
// If ball's current x location plus the ball diameter is greater than screen width - 1, then make speed on x axis negative. Because you hit the right side and will bounce back to left.
if((Px+ballDiameter) > width - 1){
Vx = -Vx;
}
// If ball's current y location plus the ball diameter is greater than screen height -1, then make speed on y axis negative. Because you hit the bottom and you will go back up.
if((Py + ballDiameter) > height - 1){
Vy = -Vy;
}
// If current x location of the ball minus ball diameter is less than 1, then make the speed on x axis nagative. Because you hit to the left side of the screen and the ball would bounce back to the right side
if((Px - ballDiameter) < 1){
Vx = -Vx;
}
// If current y location of the ball minus ball diameter is less than 1, then make the speed on y axis negative. Because the ball hit the top of the screen and it should go down.
if((Py - ballDiameter ) <1){
Dy = 1;
Vy = -Vy;
}
Px += Vx; // increase x position by speed
Py += Vy; // increase y position by speed
c.drawCircle(Px, Py, ballDiameter, p);
invalidate();
}

Related

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.

Android barrier detection of accelerating object

my app contains an object moving on a surfaceview. I am able to move it around via accelerometer.
Here's the movement code of the player object:
if(x + mx*speed > 0 && x + mx*speed < GameView.WIDTH) {
x += mx*speed;
}
if(y+ my*speed > 0 && y+ my*speed < GameView.HEIGHT) {
y+=my*speed;
}
x and y are the player's coordinates
mx is the value the player gets from the accelerometer, for example: when tilting to the left, mx is -2, when tilting more, mx is -4, -5, -6 etc. --> my is the same for the y-axis
the speed is a variable to modify and play around when i want to have a faster movement.
as you can see I tried to limit the movement to only move when the player is inside of the view.
Now my problem is: when tilting the device intensively to the right, mx turns to something like 6. speed is set to 5. This means, when the player's position + 6 * 5 is bigger than the game view it should not move any more. But this results in the player stopping pixels in front of the right side of the view... when tilting lightly to the right, the object stops perfectly at the border of the view...
Now how should i change the code to achieve an object that stops it's movement perfectly at borders of the screen?
On this picture you can see the circle not stopping quite at the bottom, as there are some pixels between the circle and the bottom border. when going slightly back with the accelerometer, the circle aligns itself to the bottom of the screen:
But now, i can only reach the screen borders when moving slowly, which means with a low mx or my.
the screenshots you can see the mY values. On the first picture my = ca. 8 and on the second ca. 6.
Any ideas?
Thanks in advance
Try to instead cap the value to the border like so
x = Math.max(Math.min(x + mx*speed, GameView.WIDTH), 0.0f));
y = Math.max(Math.min(y + my*speed, GameView.HEIGHT, 0.0f));

How to get right values from Views touch event

I have a problem with implementing touch events on GLSurfaceView. Views size is 1280x696, because of android (tablet) status bar at bottom with soft keys, time etc.., (screen resolution is 1280x800), but OnTouchListener is receiving touch events with coords [646.0,739.0], and thus my gluunproject method fails to return correct values
is there any way to return events that respect these boundaries? or how should I recalculate the position?
In general to transition between such coordinate systems: If you transition from system A to B and you have points
AOrigin (left upper corner of your receiver view)
AEnd (right bottom corner of your receiver view (origin + size))
BOrigin (left upper corner of your GL view)
BEnd (right bottom corner of your GL view (origin + size))
where AOrigin and BOrigin represent the same location in view and same goes for AEnd and BEnd
then for point P in the receiver view:
X = B.Origin.x + ((P.x - AOrigin.x)/(AEnd.x - AOrigin.x)) * (BEnd.x - B.Origin.x)
Y = B.Origin.y + ((P.y - AOrigin.y)/(AEnd.y - AOrigin.y)) * (BEnd.y - B.Origin.y)
For your case (I'm not sure because of lack of information) AOrigin is at (0,0), AEnd is at (646, 739), BOrigin is at (0,0) and BEnd is at (1280, 800):
X = P.x/646 * 1280
Y = P.y/739 * 800
You can also use this to transition to/from "GL" coordinates. A common case is having a view with upper left corner at (0, 0) and lower bottom at (1280, 800) and your "GL" coordinates are from (-1, 1) to (1, -1):
X = -1 + (P.x/1280)*2
Y = 1 + (P.y/800)*(-2)
Also note that you may use any 2 pairs of points that represent same location on screen as long as (origin-end).x != 0 AND (origin-end).y != 0

centered zoom in custom view - calculating canvas coordinates

I am working on my first "real" Android application, a graphical workflow editor. The drawing is done in a custom class, that is a subclass of View.At the moment my elements are rectangles, which are drawn on a canvas. To detect actions on elements I compare the coordinates and check for elements on the touch location.
To implement a zoom gesture I tried http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
With the 4 argument canvas.scale(...) function the centered zooming works well, but I lose the ability to calculate the canvas coordinates using the offset with mPosX and mPosY to detect if the touch after a zoom is on an element.
I tried to change the example in the blogpost above to center the canvas on the zoom gesture with:
canvas.save();
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor, mScalePivotX, mScalePivotY);
//drawing ....
canvas.restore();
I did not find any examples on how this could be done without losing the reference offset to calculate the coordinates. Is there an easy workaround? I tried to calculate the offset with the gesture center and the scaling factor, but failed :/
I have seen that other examples which use an ImageView often use a Matrix to transform the image. Could this be done with a custom View and a Canvas? If yes, how can I get the x and y offset to check the coordinates?
Also, if my ideas are completely wrong, I would be very happy to see some examples on how this is done properly.
Thx! ;)
Perhaps the following code will help you to calculate coordinates with the gesture center and the scaling factor. I use this method in my class representing opengl-sprite.
void zoom(float scale, PointF midPoint) {
if (zoomFactor == MAX_ZOOM_FACTOR && scale > 1) return;
if (zoomFactor == MIN_ZOOM_FACTOR && scale < 1) return;
zoomFactor *= scale;
x = (x - midPoint.x) * scale + midPoint.x;
y = (y - height + midPoint.y) * scale + height - midPoint.y;
if (zoomFactor >= MAX_ZOOM_FACTOR) {
zoomFactor = MAX_ZOOM_FACTOR;
} else if (zoomFactor < MIN_ZOOM_FACTOR) {
zoomFactor = MIN_ZOOM_FACTOR;
x = 0;
y = 0;
}
}
X and Y coordinates are processed in different ways, because of distinction between directions of opengl coordinate system (right and up) and midPoint's coordinate system (right and down). midPoint is taken from MotionEvents coordinates.
All other operations are understandable, i think.
Hope it will help you.

Calculating points of a circle dynamically in an Android game

If my X co-ordinates increment every time a frame is drawn, what formula can I employ from the math library in order to have the Y co-ordinate moving around the circumference of a circle frame by frame, creating the illusion of an orbiting object around a continuously moving central point?
I have seen it may involve sin or cos but am not sure how to adjust the variables accordingly per frame.
Many thanks for your help
You can't make a complete circle if your X coordinate increments every time, because half the time your X coordinate has to be decrementing.
What you want is polar coordinates: theta for angle and r for radius. Your r will remain constant, and your theta will increment continuously. Then your x and y are:
x = r * cos(theta)
y = r * sin(theta)
let ox,oy be the origin of your circle, and px,py be a point on the edge of the circle, with a radius of r
given: (px-ox)^2 + (py-oy)^2 = r^2 definition of circle
solve for py:
(py-oy)^2 = r^2 - (px-ox)^2
(py-oy) = sqrt(r^2 - (px-ox)^2)
py = sqrt(r^2 - (px-ox)^2) + oy <---
So as you increment px with your frames, you can find the appropriate py by recalculating the above formula.

Categories

Resources