JavaScript - rotate a shape by a fixed angle - javascript

I want to move a shape around the circumference of a circle on HTML canvas. I am using the following JavaScript logic:
speed = 0.005;
angle = Math.PI/2;
angle += speed * direction;
var x = cx + (radius * Math.cos(angle));
var y = cy + (radius * Math.sin(angle));
direction is set by a key press (left arrow = -1, right arrow = +1). cx and cy are fixed - they are the x and y co-ordinates of the center of the circle around which the shape is moving.
I want to move the shape around the circle in fixed steps, like the seconds hand of a clock. However, there should be 187 steps. I know that dividing 360/187 = 1.9251 degrees = 0.03359 radians. However, my drawing function is inside a loop, so writing angle += 0.03359 makes the shape spin around the circle forever.
How can I make each key press move the shape either clockwise or anti-clockwise around the circle, but in steps of 0.3359 radians?
I am using the logic I found in the answer written by rafaelcastrocouto for this question: how to move object in circle forward and backward in html5 canvas?

You need to save the starting angle in avariable that you access each time you begin to draw:
This line:
angle = Math.PI/2;
should then look like this:
angle = window.starting_angle;
On each keypress, you either increment or decrement this variable and redraw the shape.

Related

Using atan2 with offset circle

I am trying to calculate the Arc Length, so that I can color my circle border given the Arc Length.
Users can click on the circle edge and the code should automatically calculate the Arc Length from position y=radius x=cx. Given the function atan2(), I cannot achieve this successfully, because if I understand correctly, atan2 return the angle between positive X-axis and the ray from my center of circle towards the point clicked. But I need the angle between the point clicked and the y-axis.
I am attaching the picture, in case it will make more sense:
What I have currently is:
// Calcualte the deltas for point X and Y which are required for atan2 function
let deltaX = event.pageX - this.centerX
let deltaY = event.pageY - this.centerY
// The Math.atan2() function returns the angle in the plane (in radians) between the positive x-axis and the ray from (0,0) to the point (x,y), for Math.atan2(y,x)
let angleRadian = Math.atan2(deltaY, deltaX)
// Apply formula to calculate Arc length
let arcLength = radius * angleRadian
I am aware that atan2 calculates the angle as I described above, but I do not know how to modify my code to achieve what I need it to do - that is, get the angle between the Point Clicked and the y-axis.
Just swap atan2 arguments to get angle from OY axis.
For counterclockwise (CCW) system:
let angleRadian = Math.atan2(deltaX, deltaY)
(based on formulas cos(a)=sin(Pi/2-a), sin(a)=cos(Pi/2-a))
For clockwise (CW) system, where OY axis direction is down:
let angleRadian = Math.atan2(deltaX, -deltaY)

Polar coordinates and circular player scrub

I'm having issues positioning the cursor at the right point in this circular time scrubber:
I'm calculating angle with simple formula: Math.atan2(y, x).
This gives me the angle in radians, then I map it to a range [0, 1].
This range updates the player progress which is binded back to the cursor position (simple bi-directional binding).
What I need to do is just have angle = 0rad, when mouse.x = 0 and mouse.y >= radius.
Right now angle is set to 0 when I'm at mouse.x >= rootRadius, and mouse.y = 0.
This image perhaps add more insight, I'm having difficult explaining it in a simple manner, however this should be progress/angle = 0:

Sphere rotation in Babylon.js

I want to ask for help regarding rotation using the Babylon.js framework.
I need the sphere to rotate 45 degrees, exactly aligned with the diagonal circle, which has a 45 degree orientation, but I'm not getting it.
The code I made is in the link below:
https://codepen.io/polalas/pen/VwvaKwL
The method responsible for the rotation is the loop () method, which is triggered every time the scene is rendered.
function loop () {
var y1 = scene.getMeshByName("I1");
y1.rotation.y - = 0.01 * Math.sin (Math.PI / 4);
y1.rotation.x - = 0.01 * Math.sin (Math.PI / 4);
}
I imagine that I mishandled the rotation. Could someone help, please?
Using your code, the best way to achieve that is to first rotate the parent of the sphere (what you called newMesh (or I1)) 45 degrees around the Z axis right before adding the sphere as a child:
newMesh.rotate(BABYLON.Axis.Z, Math.PI / 4);
Afterwards you can rotate it around its Local (!) X axis in your render loop:
function loop(){
var y1 = scene.getMeshByName("I1");
}
This way you get a perfect rotation around your (mocked) pivot.

position of the end of a rotating arrow

I have an arrow that rotates. How can I calculate the position of the end/bottom of it relative to its angle/rotation ?
The thing you need is called Polar to Cartesian.
You need to know 2 things:
Angle of rotation
The length of the arrow
Then you can apply this formula to get your x,y position:
x = r * cosθ,
y = r * sinθ
Where r = length of arrow and θ = the angle of rotation.
EDIT: When using this, have in mind that the start of the arrow is considered the origin point (0,0).

find angle made by a line passing through origin

In above image the blank arrow will rotate randomly and stop at particular box.
I know the X & Y of each box how can i find the angle made by blank arrow. basically i want to find out at what angle each box is. I need to calculate this in javascript.
You can use slop to calculate angle
deltaY = Y //(of Box Point 2 is (0,0) so Delta = Y2-Y1=Y2);
deltaX = X //(of Box);
Than angle in degrees is
Degrees = arctan(deltaY / deltaX) * 180 / PI
If atan2 is present in library use(JavaScript)
Degrees = Math.atan2(deltaY ,deltaX) * 180 / PI

Categories