Rotate self between an arc on the local axis Godot - random

I have a node which fires a bullet, but I want to randomly rotate it a little so that certain guns are unpredictable. Specifically, I want to rotate it on the X and Y axes between a -2.5 - 2.5 degree arc on the local axis. I've tried rotation.x = rand_range(-2.5, 2.5 * PI) and rotation.y = rand_range(-2.5, 2.5 * PI), but these seem to rotate the node on the global X and Y axes. How should I rotate the bullet emitter node locally between an arc? Thank you in advance.

Step 1: Spawn your bullet
Step 2: Give it the rotation of the parent object
Step 3: To that rotation add you random rotation
Step 4: Shoot

Related

Three.js | GLTF Rubik Cube 3D Rotation Problem

Im trying to create a rubik cube but i keep having problems with rotations, when is a single face rotation everythinks looks fine but when i combine 2 rotations it get weird
One Rotation 90 degrees on the left face - Rotation on the x axis
After second rotation on the back face with 45 degrees - rotation on the Z axis
I also found out if i do the X axis rotation it appears that i have to do Y axis rotation on some cubes and Z axis rotation on another for all to work good but that solution doesnt work.
I created a group consisted of the frame of the cube and the faces.
Loads everything then if it is Last, adds the group to the scene.
The this.cube is an array of groups.

How to find orientation of rubik cube?

I'm trying to make a rubik cube game in webgl using three.js (you can try it here).
And I have problems to detect on witch axis I have to rotate my cube according the rotation of the cube. For instance, if the cube is in original position/rotation, if I want to rotate the left layer from down to up, I must make a rotation on the Y axis. But I rotate my cube 90 degrees on Y, I will have to rotate It on the Z axis to rotate my left layer from down to up.
I'm trying to find a way to get the correct rotation axis according the orientation of the cube.
For the moment I check witch vector of the axis of the rotation matrix of the cube is most parallel with the vector(0,1,0) if I want to move a front layer from down to up. But it do not works in edge cases like this for instance :
I guess there is some simple way to do that, but I'm not good enough in matrix and mathematical stuff :)
An AxisHelper can show the aixs of the scene which you could determine the orientation with.
var axishelper = new THREE.AxisHelper(40);
axishelper.position.y = 300;
scene.add(axishelper);
You could also log your cube and check the position and rotation properties with Chrome Developer Tools or Firebug.
You can store the orientation of each cube in its own 4x4 matrix (i.e. a "model" matrix) that tells you how to get from the cube's local coordinates to the world's coordinates. Now, since you want to rotate the cube around to an axis (i.e. vector) in world coordinates, you need to translate the axis into cube coordinates. This is exactly what the inverse of the model matrix yields.

How do you map a point(x,y) on a 2D image to 3D space? (illustration provided)

I have a stack of images (about 180 of them) and there are 2 stars (just basic annotations) on every single image. Hence, the position (x,y) of the two stars are provided initially. The dimensions of all these images are fixed and constant.
The 'distance' between the image is about 1o with the origin to be the center (width/2, height/2) of every single 2D image. Note that, if this is plotted out and interpolated nicely, the stars would actually form a ring of an irregular shape.
The dotted red circle and dotted purple circle are there to give a stronger scent of a 3D space and the arrangement of the 2D images (like a fan). It also indicates that each slice is about 1o apart.
With the provided (x,y) that appeared in the 2D image, how do you get the corresponding (x,y,z) in the 3d space knowing that each image is about 1o apart?
I know that MATLAB had 3D plotting capabilities, how should I go about implementing the solution to the above scenario? (Unfortunately, I have very little experience plotting 3D with MATLAB)
SOLUTION
Based on the accepted answer, I looked up a bit further: spherical coordinate system. Based on the computation of phi, rho and theta, I could reconstruct the ring without problems. Hopefully this helps anyone with similar problems.
I have also documented the solution here. I hope it helps someone out there, too:
http://gray-suit.blogspot.com/2011/07/spherical-coordinate-system.html
I believe the y coordinate stays as is for 3D, so we can treat this as converting 2D x and image angle to an x and z when viewed top down.
The 2D x coordinate is the distance from the origin in 3D space (viewed top down). The image angle is the angle the point makes with respect to the x axis in 3D space (viewed top down). So the x coordinate (distance from orign) and the image angle (angle viewed top down) makes up the x and z coordinates in 3D space (or x and y if viewed top down).
That is a polar coordinate.
Read how to convert from polar to cartesian coordinates to get your 3D x and z coordinates.
I'm not great at maths either, here's my go:
3D coords = (2Dx * cos(imageangle), 2Dy, 2Dx * sin(imageangle))
Given the 2D coordinates (x,y) just add the angle A as a third coordinate: (x,y,A). Then you have 3D.
If you want to have the Anotations move on a circle of radius r in 3D you can just calculate:
you can use (r*cos(phi),r*sin(phi),0) which draws a circle in the XY-plane and rotate it with a 3x3 rotation matrix into the orientation you need.
It is not clear from you question around which axis your rotation is taking place. However, my answer holds for a general rotation axis.
First, put your points in a 3D space, lying on the X-Y plane. This means the points have a 0 z-coordinate. Then, apply a 3D rotation of the desired angle around the desired axis - in your example, it is a one degree rotation. You could calculate the transformation matrix yourself (should not be too hard, google "3D rotation matrix" or similar keywords). However, MATLAB makes it easier, using the viewmtx function, which gives you a 4x4 rotational matrix. The extra (fourth) dimension is dependent on the projection you specify (it acts like a scaling coefficient), but in order to make things simple, I will let MATLAB use its default projection - you can read about it in MATLAB documentation.
So, to make the plot clearer, I assume four points which are the vertices of a square lying on the x-y plane (A(1,1), B(1,-1), C(-1,-1), D(1,-1)).
az = 0; % Angle (degrees) of rotation around the z axis, measured from -y axis.
el = 90; % Angle (degrees) of rotation around the y' axis (the ' indicates axes after the first rotation).
x = [1,-1, -1, 1,1]; y = [1, 1, -1, -1,1]; z = [0,0, 0, 0,0]; % A square lying on the X-Y plane.
[m,n] = size(x);
x4d = [x(:),y(:),z(:),ones(m*n,1)]'; % The 4D version of the points.
figure
for el = 90 : -1 :0 % Start from 90 for viewing directly above the X-Y plane.
T = viewmtx(az, el);
x2d = T * x4d; % Rotated version of points.
plot3 (x2d(1,:), x2d(2,:),x2d(3,:),'-*'); % Plot the rotated points in 3D space.
grid
xlim ([-2,2]);
ylim ([-2,2]);
zlim([-2,2]);
pause(0.1)
end
If you can describe your observation of a real physical system (like a binary star system) with a model, you can use particle filters.
Those filters were developed to locate a ship on the sea, when only one observation direction was available. One tracks the ship and estimates where it is and how fast it moves, the longer one follows, the better the estimates become.

Rotating about 2 axes independently

I am looking for a solution to the following problem:
I have a ball travelling to the upper right corner of the screen. That is, its velocity to the right and up are identical.
To simulate its rotation, it has an angular velocity along the X and the Y axis - those are also both equal.
This works fine so far. My problem is now to correctly rotate the ball on display: I am using OpenGL and a simple sphere for the ball.
Now my naive approach was to use
glRotate(rx, 1, 0, 0); // rotate about x axis
glRotate(ry, 0, 1, 0); // rotate about y axis
But this does not work as I intended: The second rotation depends on the first one. On second thought, this works as I was used to it. But now I am looking for a solution to rotate my ball correctly by applying both rotations independently.
I also tried using quaternions but did not succeed. I am even not sure if I get something completely wrong an my approach of "rotate some degrees about x, then some about y" makes sense at all.
Any "enlightment" is greatly appreciated.
Thank you!
How about glRotate(rr, 1,1,0); in that case you rotate around the arbitrary vector (1,1,0).

Archery game programming algorithm

I need the algorithm to animate the arrow based on 2 parameters, angle while shooting and power while drawing the bow.
Ive tried to use y=asinx but it works only when shooting in up direction. Doesnt work well while shooting with straight or down direction. Thanks.
The flight of your projectile is described by
x(t) = v * cos(theta) * t
y(t) = v * sin(theta) * t - 1/2 * g * t^2
where t is time, v the initial velocity (power), theta the angle, g the acceleration due to gravity (e.g. 9.8 m/s^2), x the horizontal coordinate and y the height.
You could try simulating the motion instead of deriving the analytic function. i.e. keep track of the current position, velocity and acceleration vectors for the arrow, and each time-increment, update the position based on the velocity and the velocity based on the acceleration.
otherwise, if you need an analytic function, See #bnaul's answer for the analytic version

Resources