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?
Related
I created a rectangle using x and y integers which change values when I touch the rectangle and swipe in a certain direction (x increases when I swipe right, y increases when I swipe up, etc.). This rectangle is also being rendered onto a canvas. I have a game loop which calls an update() method and render() method. The update() method updates the x and y values and creates a collision detection rectangle, the render() method then uses these values to render an updated rectangle to my canvas.
Say I use the method canvas.translate(0, -50) before I draw this rectangle in my game loop. The rectangle appears 50 pixels above where it used to be, however my x and y values remain the same so I have to swipe below my rendered rectangle for it to actually do something. What's a simple and efficient way of updating the x and y so that my rectangle is actually located where it is rendered and not below it? I need the collision detection box to be located where the rectangle is drawn.
I taught myself how to program, so I am not really sure if there is an easier way, but if I were you whenever you call canvas.translate() just change the x and y values to adjust. For example, this is what I would do for the sample you gave:
canvas.translate(0, -50)
x = x+0
y = y+(-50)
If you are too lazy to add the extra two lines you could put it all in to a function and just replace canvas.translate() with whatever function you made.
In fact I will make that function right now. Why not?
public void translate(double XMove, double YMove) {
canvas.translate(XMove, YMove);
x = x+XMove;
y = y+YMove;
}
Ohh.. damm Math !! once again stuck. It seems to be easy but i think its not that easy,
Problem Statement: I want to rotate the 3 fixed points which lies on a fixed circle.
1.when 1 point is selected remaining 2 points should be static mode and only selected point should move/rotate on circumference of circle.
2.And all 3 points are connected via 3 lines as shown in images..when we select a point and rotate it,connected lines also increase and decrease..
I already tried to solve this problem finding angle at each instant after touch.but its not quite working as per my need..
something like this
I hope the following explanation enable you to put the steps into your coding language.
Presumption is that the vertex to be moved has already selected and so the calculation of (xcnd,ycnd) as defined below is used to set the selected vertex of the triangle.
Let the constraining circle have centre at (cx,cy) and radius r.
Let the coordinates of where the screen is touched be (xtch,ytch)
Let the coordinates of where the screen is touched relative to the centre be (xrel,yrel)
then xrel = xtch - cx and yrel = ytch - cy
Let the coordinates of the point on the constraining circle when the screen is touched at (xtch,ytch) be (xcnd,ycnd).
xcndrel = xcnd - cx, and ycndrel = ycnd - cy give the coordinates on the constraining circle relative to its centre,
Note that
xrel and xcndrel will have the same signs (ie both positive or both negative)
and yrel and ycndrel will also have the same signs.
the function abs(x) = x if x>=0 and -x if x<0 should be available in whatever language you are using
the function sign(x) may or may not be available, sign(x) =1 if x>0 and -1 if x<0 and undefined for x=0
If not available then sign(x)=x/abs(x)
Check if xrel=0
if xrel=0 xcndrel=0, ycndrel=r*sign(yrel)
Otherwise work in first quadrant ie where x>0 and y>0 using abs(xrel) and abs(yrel)
find angle where screen is touched relative to centre of circle using
theta=arctan(abs(yrel)/abs(xrel))
find the coordinates (xcndrel, ycndrel) by using theta in the first quadrant and then placing in the correct quadrant using the signs of xrel and yrel
xcndrel = sign(xrel)*r*COS(theta)
ycndrel = sign(yrel)*r*SIN(theta)
Screen coordinates can now be found
xcnd = xcndrel +cx
ycnd = ycndrel + cy
I have a custom drawn rectangle which i want to move in a circular path based on touch events.
It follows the direction of the touch for clockwise or anticlockwise movement but basically move in circular motion, as if moving on the edge of the circle.
My current thought process is as follows:
Based on the users current and previous x,y i shall find the angle in degrees and then move this rectangle by the same angle by re-drawing in the new position, just making sure that it moves on the edge of a circle.
But this leads to some confusion on the following:
1. how do i decide whether angle movement is clockwise or anti-clockwise.
2. I am not being able to figure out the math for this properly.
Would this be the best approach or is there a better idea for doing this?
Also, if this is the best approach, could someone please tell me the formula for calculating the angle by which i should move it while taking care of the clocking and anticlockwise ?
could someone please help?
please let me know if any more details are required.
Thanks
Steps
Here are a few steps in order to move your rectangle along a circle's rim when the user taps and holds to the side of the circle:
1. Obtain direction desired.
2. Obtain angle from current x and y coordinates.
3. Add direction (+1 if counterclockwise, -1 if clockwise) to angle.
4. Calculate new x and y coordinates.
5. Update/display rectangle.
Details
1. In pseudocode, direction = sign(Rectangle1.x - UsersFingerPosition.x). Here sign is a function returning -1 if the number was negative, 0 if it is 0, and 1 if it is positive. Note that sign(0) will only result when the user is on the exact x and y of your rectangle's location. In that case, the rectangle would not move (which should be good). In Java, the sign function is Math.signum().
2. To obtain the current angle use the following java code:
double angle = Math.toDegrees(Math.atan2(Circle.y-Rectangle1.y, Rectangle1.x-Circle.x));
Note the order of Circle.y-Rectangle.y and Rectangle.x...Circle.x. This is a result of the coordinate (0, 0) being in the top left corner instead of the center of the screen.
3. Simple enough, just add direction to angle. If desired, do something like
angle += direction*2; //So it will move more quickly
4. To get the new x and y coordinates of your rectangle, use the trigonometric functions sine and cosine:
Rectangle1.x = Math.cos(Math.toRadians(angle))*Circle.radius + Circle.x - Rectangle1.width;
Rectangle1.y = Math.sin(Math.toRadians(angle))*Circle.radius + Circle.y - Rectangle1.height;
(where Circle.x and Circle.y are the coordinates of the center of your circle and Circle.radius is naturally it's radius).
5. This one you'll have to take care of (or have already) :)!
Hope this helps you!
Steps
Here are a few steps in order to move your rectangle along a circle's rim:
1. Obtain finger position/Check that it's still dragging the rectangle.
2. Obtain angle from current x and y coordinates.
3. Calculate new x and y coordinates.
4. Update/display rectangle.
Details
1. This one is probably specific to your code, however, make sure that when the user starts dragging the rectangle, you set a variable like rectangleDragging to true. Before you run the next steps (in the code), check that rectangleDragging == true. Set it to false once the user lets go.
2. To obtain the current angle use the following java code:
double angle = Math.toDegrees(Math.atan2(Circle.y-Finger.y, Finger.x-Circle.x));
Note the order of Circle.y-Finger.y and Finger.x...Circle.x. This is a result of the coordinate (0, 0) being in the top left corner instead of the center of the screen.
3. To get the new x and y coordinates of your rectangle, use the trigonometric functions sine and cosine:
Rectangle1.x = Math.cos(Math.toRadians(angle))*Circle.radius + Circle.x - Rectangle1.width;
Rectangle1.y = Math.sin(Math.toRadians(angle))*Circle.radius + Circle.y - Rectangle1.height;
(where Circle.x and Circle.y are the coordinates of the center of your circle and Circle.radius is naturally it's radius). Subtracting the width and height of the rectangle should center it on the circle's border instead of placing the left, upper corner on the circle.
4. This one you'll have to take care of (or have already) :)!
Hope this helps you!
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.
How would I draw a line using canvas if i touch the image using onTouchevent .here i am using imageview as a background and position it based on x/Y co-ords? Also, is it possible to check if a line is drawn at those co-ords? Actually if i draw a line ,it would be overlapped the image view.the line should draw image itself inside the layer.how to calculate image x,y position
Thanks,
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)(event.getX());
int Y = (int)(event.getY());
switch (eventaction ) {
case MotionEvent.ACTION_DOWN:
canvas.drawLine(X,Y,someOtherX, someOtherY, paint):
break;
<snip>
}
If you want to check if a line is already drawn at coordinates X,Y, then you have to keep
a list or Array of the lines you've already drawn with their start/stop coords or slope/intercept and do the algebra. You've got your current line from X,Y to SomeOtherX,Y.
It would be a simple process of going through each line and finding where, if at all, the two lines intersect and if they intersect on screen.
is merely a shorthad ywa of saying there is more code here, but not important to the point you asked.
How to solve for intersection of two lines, say, these two, in slope-intercept form
y = 3x-3
y = 2.3x+4
At the point of intersection they will both have the same y-coordinate value, so we set the equations equal to each other:
3x-3 = 2.3x+4
This gives us an equation in one unknown (x) which we can solve:
Re-arrange to get x terms on left 3x - 2.3x = 4+3
Combining like terms 0.7x = 7
Giving x = 10
To find y, simply set x equal to 10 in the equation of either line and solve for y:
Equation for a line y = 3x - 3 (Either line will do)
Set x equal to 10 y = 30 - 3
Giving y = 27
We now have both x and y, so the intersection point is (10, 27)