So I am making an android application where I have a set of circles. When the user touches a point in one of the circles I need to calculate a point according to where the user touched.
Somewhat similar to an archer's target.
how do I achieve this?
I have searched about touch interfaces but I haven't been able to figure out how i can do this
You should use TouchListener with following code to get to know that whether your touch event occur inside circle or not.
Formula
(x - center_x)^2 + (y - center_y)^2 < radius^2
code
int x = view.getX();
int y = view.getY();
if((xTouch - (x + radius)) * (xTouch - (x + radius)) + (yTouch - (y + radius)) * (yTouch - (y + radius)) <= (radius * radius)){
...
}
If this satisfies left side equation is less then right side one, your touch event is inside circle else outside circle.
Just set onTouchListener and make your logic with event.getX()/getY()
Related
I'm making a game where the player follows the mouse and a bullet gets shot out the players back end at the opposite direction that it is moving towards the mouse. Its a bit hard to explain in words so i quickly made a rough draft in paint.net to demonstrate.
Grey ball is the player. yellow cursor is where the player is touching on the touch screen. And the black balls are the bullets.
In example 1 and 2, the player is moving towards the cursor and bullets are behind shot in the opposite direction. What I want to o is like in example 3 where there is more than one bullet coming out the back and they are also spread out like a shotgun shot.
image:
http://i.stack.imgur.com/Q3Q18.png
My goal would be to allow the player to upgrade to be able to shoot more than one bullet at a time. All I want is for the player to be able to shoot up to 5 at a time. Thanks in advance!
The angles of the other bullets would use the same formula you are using, but the angles would be some constant offset relative to the center bullet. For instance, if you are using the following formula for the center bullet:
x += Math.cos(angle * Math.PI/180) * Status.bulletSpeed;
y += Math.sin(angle * Math.PI/180) * Status.bulletSpeed;
then your other bullets would be:
x1 += Math.cos((angle + 10) * Math.PI/180) * Status.bulletSpeed;
y1 += Math.sin((angle + 10) * Math.PI/180) * Status.bulletSpeed;
x2 += Math.cos((angle - 10) * Math.PI/180) * Status.bulletSpeed;
y2 += Math.sin((angle - 10) * Math.PI/180) * Status.bulletSpeed;
You can choose some other value other than 10 if you so choose. If you wanted to shoot 5 at a time, you would also use:
x3 += Math.cos((angle + 20) * Math.PI/180) * Status.bulletSpeed;
y3 += Math.sin((angle + 20) * Math.PI/180) * Status.bulletSpeed;
x4 += Math.cos((angle - 20) * Math.PI/180) * Status.bulletSpeed;
y4 += Math.sin((angle - 20) * Math.PI/180) * Status.bulletSpeed;
Here, the number you use should be twice the value you choose for the first set.
suppose take my example as PIANO image..on every key press on its keyboard i want to perform a different event..now i am not able to get where to start this..
how to perform this in android..
take below image:
As you can see in the image i have an piano image..and drawn red and blue lines in some shape..now i want to perform different event for each shape on this image..
how can i do this in android programming..
This seems like a basic Collision Detection problem.
Basically what you want to do is of course listen to the screen touches and receive the X & Y of the touch location. (You can of course use multi-touch, just remember to do this for every touch)
When a touch appears you will calculate a bunch of containsRectanglePoint for each key on the image.
So basically you will split the image into a lot of rectangles like so.
(source: gyazo.com)
Then you check if the point contains any of the rectangles.
If the touch X & Y is inside either 1 or 2 then perform the event for that key.
If the touch X & Y is inside either 3, 4 or 5 the perform the event for that key.
If the touch X & Y is inside either 6 then perform the event for that key.
If the touch X & Y is inside either 7 then perform the event for that key.
You will of course do that for all the keys.
So thereby when a collision happens you look through all that.
Simple Rectangle vs Point Collision Detection
The following code checks for collision detection between a rectangle and a point. If the point is within the rectangles bounds then the method will return true. If not it returns false.
public static boolean containsRectanglePoint(double x, double y, double w, double h, double px, double py)
{
if (px < x) { return false; }
if (py < y) { return false; }
if (px > (x + w)) { return false; }
if (py > (y + h)) { return false; }
return true;
}
x = Rectangle X (or AABB Minimum X)
y = Rectangle Y (or AABB Minimum Y)
w = Rectangle Width (or AABB Maximum X - AABB Minimum X)
h = Rectangle Height (or AABB Maximum Y - AABB Minimum Y)
px = Point X
py = Point Y
In your case px & py is the location of the touch.
You could also use Java's standard Rectangle2D class, to both store and calculate the collisions, but that requires creating a lot of instances of the class, and it will be a lot cheaper, when we are talking about memory, to just store the coordinates and then use the function I provided to you.
So this is my second question today, I might be pushing my luck
In short making a 3D first Person, where you can move about and look around.
In My OnDrawFrame I am using
Matrix.setLookAtM(mViewMatrix, 0, eyeX , eyeY, eyeZ , lookX , lookY , lookZ , upX, upY, upZ);
To move back, forth, sidestep left etc I use something like this(forward code listed)
float v[] = {mRenderer.lookX - mRenderer.eyeX,mRenderer.lookY - mRenderer.eyeY, mRenderer.lookZ - mRenderer.eyeZ};
mRenderer.eyeX += v[0] * SPEED_MOVE;
mRenderer.eyeZ += v[2] * SPEED_MOVE;
mRenderer.lookX += v[0] * SPEED_MOVE;
mRenderer.lookZ += v[2] * SPEED_MOVE;
This works
Now I want to look around and I tried to port my iPhone openGL 1.0 code. This is left/right
float v[] = {mRenderer.lookX - mRenderer.eyeX,mRenderer.lookY - mRenderer.eyeY, mRenderer.lookZ - mRenderer.eyeZ};
if (x > mPreviousX )
{
mRenderer.lookX += ((Math.cos(SPEED_TURN / 2) * v[0]) - (Math.sin(SPEED_TURN / 2) * v[2]));
mRenderer.lookZ += ((Math.sin(SPEED_TURN / 2) * v[0]) + (Math.cos(SPEED_TURN / 2) * v[2]));
}
else
{
mRenderer.lookX -= (Math.cos(SPEED_TURN / 2) *v[0] - Math.sin(SPEED_TURN / 2) * v[2]);
mRenderer.lookZ -= (Math.sin(SPEED_TURN / 2) *v[0] + Math.cos(SPEED_TURN / 2) * v[2]);
}
This works for like 35 degrees and then goes mental?
Any ideas?
First of all I would suggest not to trace the look vector but rather forward vector, then in lookAt method use eye+forward to generate look vector. This way you can loose the update on the look completely when moving, and you don't need to compute the v vector (mRenderer.eyeX += forward.x * SPEED_MOVE;...)
To make things more simple I suggest that you normalize the vectors forward and up whenever you change them (and I will consider as you did in following methods).
Now as for rotation there are 2 ways. Either use right and up vectors to move the forward (and up) which is great for small turning (I'd say about up to 10 degrees and is capped at 90 degrees) or compute the current angle, add any angle you want and recreate the vectors.
The first mentioned method on rotating is quite simple:
vector forward = forward
vector up = up
vector right = cross(forward, up) //this one might be the other way around as up, forward :)
//going left or right:
forward = normalized(forward + right*rotationSpeedX)
//going up or down:
forward = normalized(forward + up*rotationSpeedY)
vector right = cross(forward, up) //this one might be the other way around
vector up = normalized(cross(forward, right)) //this one might be the other way around
//tilt left or right:
up = normalized(up + right*rotationZ)
The second method needs a bit trigonometry:
Normally to compute an angle you could just call atan(forward.z/forward.x) and add some if statements since the produced result is only in 180 degrees angle (I am sure you will be able to find some answers on the web to get rotation from vector though). The same goes with up vector for getting the vertical rotation. Then after you get the angles you can easily just add some degrees to the angles and recreate the vectors with sin and cos. There is a catch though, if you rotate the camera in such way, that forward faces straight up(0,1,0) you need to get the first rotation from up vector and the second from forward vector but you can avoid all that if you cap the maximum vertical angle to something like +- 85 degrees (and there are many games that actually do that). The second thing is if you use this approach your environment must support +-infinitive or this atan(forward.z/forward.x) will brake if forward.x == 0.
And some addition about the first approach. Since I see you are trying to move around the 2D space your forward vector to use with movement speed should be normalized(forward.x, 0, forward.z), it is important to normalize it or you will be moving slower if camera tilts up or down more.
Second thing is when you rotate left/right you might want to force up vector to (0,1,0) + normalize right vector and lastly recreate the up vector from forward and right. Again you then should cap the vertical rotation (up.z should be larger then some small value like .01)
It turned out my rotation code was wrong
if (x > mPreviousX )
{
mRenderer.lookX = (float) (mRenderer.eyeX + ((Math.cos(SPEED_TURN / 2) * v[0]) - (Math.sin(SPEED_TURN / 2) * v[2])));
mRenderer.lookZ = (float) (mRenderer.eyeZ + ((Math.sin(SPEED_TURN / 2) * v[0]) + (Math.cos(SPEED_TURN / 2) * v[2])));
}
else
{
mRenderer.lookX = (float) (mRenderer.eyeX + ((Math.cos(-SPEED_TURN / 2) * v[0]) - (Math.sin(-SPEED_TURN / 2) * v[2])));
mRenderer.lookZ = (float) (mRenderer.eyeZ + ((Math.sin(-SPEED_TURN / 2) * v[0]) + (Math.cos(-SPEED_TURN / 2) * v[2])));
}
My goal is to code a headtracker for my head mounted display using my android phone. The movement is transferred to mouse movement, which in turn is used inside pc games. I want to mount my android phone to the back of the head-tracker, so it will be fixed to the back of my head. The y-axis is facing upwards, the x-axis is facing to the right when looking straight ahead, that means when you look at my back head, you see the mobile phone display.
Due to mechanics of this setup there is an angle between the y-axis and the gravity-axis, so the mobile phone is tilted slightly away from the back of the head when looking straight forward. Also it is slightly rotated around the z-axis and might be rotating while moving around the z-axis within an angle of about 5 to 10°.
My phone does not have a gyroscope. I used
public void onSensorChanged(SensorEvent event)
{
// alpha is calculated as t / (t + dT)
// with t, the low-pass filter's time-constant
// and dT, the event delivery rate
final float alpha = 0.8;
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];
}
which works better than just using raw data from the accelerometer. I just used 0.8 as alpha, do I need to adjust it according to t / (t + dT)? So how to get t and dT then?
Using this setup, tilting the head up and down works - with some refinement here and there - well.
But moving left and right turns out to be an issue. Also moving diagonal from left-down to right-up and mirrored. Or moving in circles.
Coming from the basic mathematics and not my error description, which code do you suggest to make this work?
I'm looking for a tutorial that allows me to make a simple line tracing app, no other fancy stuff such as collision. If I can get an object to follow a line drawn on the screen at the end of this week that would be wonderful.
After getting familiar with android dev, creating a few apps (calculators, converters), I think I'm ready to step it up a bit with a game containing a main loop.
I think this is exactly what I'm looking for: http://www.rengelbert.com/tutorial.php?id=182
Here is the demo: http://www.rengelbert.com/swf/LineDrawing.html
Your question is actually quite vague and it would help if you actually supplied some code snippets, variables, formulas, to help us understand your scenario. I'm going to make the following assumptions to help me guide an answer:
I have a line segment defined by (x1, y1) - (x2, y2)
I want to make an animation of an object that follows the line segment
The object needs to be orientated the correct direction
Lets assume the object moves at a speed of 1 pixel per second
Okay, now we have established the parameters, we can provide some Java code:
// Define the line segment.
double x1 = /* ... insert value here */;
double y1 = /* ... insert value here */;;
double x2 = /* ... insert value here */;;
double y2 = /* ... insert value here */;;
// Determine both the direction and the length of the line segment.
double dx = x2 - x1;
double dy = y2 - y1;
double length = Math.sqrt(dx * dx + dy * dy); // length of the line segment
double orientation = Math.atan2(dy, dx);
// Now for any time 't' between 0 and length, let's calculate the object position.
double x = x1 + t * dx / length;
double y = y1 + t * dy / length;
showObjectAt(x, y, orientation);
As to following a tutorial on building a game loop for your application, I highly recommend you follow the series on http://www.mybringback.com/ particularly Travis' Android tutorial on working with the SurfaceView object at http://www.mybringback.com/tutorial-series/3266/android-the-basics-28-introduction-to-the-surfaceview/