I am developing a new game for Android. In this I am using Android OpenGL ES
1.1. In this when I translate in Z axis to -2, the triangle moves backwards. Now when I translate in X axis and Y axis the triangle is visible till a limit of say -1.4 to 1.4 in Y axis and -0.8 to 0.8 in X axis.
Now If I move further down say about -4 in Z axis, I can move the triangle in
Y-axis to say about from -1.9 to 1.9 and X- axis from -1.2 to 1.2. The numbers provided are only approximate values. The point is that when I move back wards in Z axis, I am able to move triangle for a longer range in X and Y axis. This keeps increasing as I move backwards.
Now I would like to know calculate this visible range for a given value of Z. I mean If I know the Z value to be as -3 I would like to calculate in code what is the visible range of triangle to which i can translate in X and Y axis. I right now do it manually by trying trial and error with different values. Please let me know how to calculate it in code. Thank you very much in advance for your time and help.
This greatly depends on what matrices you put the triangle through, or you have loaded into opengl. Last time I checked, by default, the clip coordinates for opengl was -1 to 1 in the x, y, and z axis.
Related
I'm trying to make a small circle move in another bigger circle as a ball moving in circle relative to Android phone tilting. I'm doing this in Godot but I think the principle is the same in all game engines probably. I make a scene in Godot and add two sprites as the two circles as the following picture. I get the accelerometer 3D vector, use x and y values and calculate the angle in x and y plate (relative to y axis i.e Vector2(0, 1) ) and rotate the mentioned scene to that degree using an animation, using this code
func _process(delta: float) -> void:
var vec3 = Input.get_accelerometer()
accelVec = Vector2(-stepify(vec3.x, 0.1), -stepify(vec3.y, 0.1))
var angle = accelVec.angle_to(Vector2(0, 1))
rotateTween.interpolate_property(self, "rotation", rotation, angle, 0.2,
Tween.TRANS_LINEAR)
rotateTween.start()
return
But the problem lies in here that when the x value of accelerometer 3D vector changes from a positive to negative value i.e when the ball is at top of the circle and is going to go to the other half of the circle, it actually moves from bottom of the circle to the desired point rather than from top of the circle.
I hope I explained the problem well, though I doubt it.
I added the Gif that shows actual test on an android phone here Testing in Android.
Any idea how to solve this problem?
Thanks in advance.
This is because Tween is interpolating linear values. It doesn't know it's working with angles, or that they should wrap around.
So when you're at -179 degrees and you tween to 179--a difference of 2 degrees--Tween just sees -179 -> 179 and goes through the whole circle.
You probably don't need a Tween here at all because _process() happens every frame.
I want to rotate the whole value of a 3d vector into one axis using quaternion rotations.
The reason behind is that I want to align the X and Y Axis of my smartphone with the X and Y Axis of my vehicle in order to detect lateral and longitudinal acceleration separated on these two axis. Therefore I want to detect the first straight acceleration of the car and rotate the whole acceleration value into the heading axis (X-Axis) of the phone assuming a straight forward motion.
How do I achieve this?
I got it myself while this Link helped me a lot.
In the end I just needed to define the destination axis as a Vector V(Magnitude_Source, 0, 0), calculate the angle between V and the source vector and rotate the source vector.
I'm trying to rotate a figure based on the mouse position where a horizontal swipe will rotate it along the y axis and a vertical swipe will rotate it along the z axis. Now i followed the tutorial on android development and was able to create a rotation about the z axis. I now want to combine it with rotation along the y axis. Here is a part of my code:-
Matrix.setRotateM(mRotationMatrix,0,mAngle,0,0,-1.0f);
//i added the following line
Matrix.setRotateM(mRotationMatrix2,0,mAngle2,0,-1.0f,0);
Matrix.multiplyMM(scratch,0,mMVPMatrix,0,mRotationMatrix,0);
//and this line too
//i am rotating the already rotated figure on a different axis this time
Matrix.multiplyMM(temp,0,scratch,0,mRotationMatrix2,0);
mTriangle.draw(temp);
But when i run the above code, I am getting strange results. The code was working right before i added the 2 lines of code and the float matrix variable temp.
You should multiply only once with MVP, and do no transform after, so at the end just before drawing.
Since VP is doing the transformation from space to camera to screen (so further transforms would be awkward, like having a camera looking the screen displaying another camera ;-) ).
I will have an oval in the plane.The x and y co-ordinates of the center of oval and radius of the oval is known.Using a particle to move around the plane,have to verify whether the oval is circled(by moving the particle through all the sides of the oval).
How can I verify whether the oval is circled by the particle?
Consider centre of oval is (0, 0deg) in polar coordinates.
I am assuming your particle doesn't go inside the boundary of oval(from your words side of oval).
Minimize min and max with start position(atan(yParticle-yOvalCentre, xParticle-xOvalCentre)) of particles. Everytime particles goes towards min update min angle and similar for max. Once max - min > 360, you have completed the circle.
When you decided to start painting oval around circle, you created the starting vector from circle center to point (let's call it mouse point).
(mouseX-circleX, mouseY-circleY) = start vector
So, initial angle between current and starting vector will be 0 (until you move your mouse).
Mohit suggested you to check whether your current vector achieved some delta (359 deg is enough) between minimal and maximal angle value (angle can vary between -180 .. 180).
I'm not sure about his formula, though. I think it's better to find angle between starting vector (ax,ay) and current (bx,by) using the fact that
[a,b] = |a|*|b|*sin(alpha) = ax*by - ay*bx
(a,b) = |a|*|b|*cos(alpha) = ax*bx + ay*by
atan( [a,b] / (a,b) ) = atan(tan(alpha)) = alpha
So, calculate current angle on every mouse move and update min/max values, until max-min < 359
If you want to check it simultaneously for several circles, do it simultaneously for array of starting vectors and min/max values)
UPD I've noted big problem here, after painting 180 degrees around circle you'll achive -180 point immediately. So I think the best thing to do is to accumulate some summary value of signed delta angles between current and prevous "MouseMove" callback. So, when summary value will become 359 or -359 that mean you maked valid clock or counter-clockwise rotation.
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