How to turn the turtle toward its move direction? - turtle-graphics

In Pencil code, how to turn the turtle toward its move direction or toward a x,y point (relative to home)?

You can use turnto:
help turnto
turnto(degrees) Turn to a direction. North is 0, East is 90: turnto 270
turnto(x, y) Turn to graphing coordinates: turnto 50, 100
turnto(obj) Turn to page coordinates or an object on the page: turnto lastmousemove
Is this what you need?

Related

How to determine the rotation angle?

I'm trying to implement a russian roulette game and want it to brute-force the solution for it. Here is my problem. I'm going to hard code the relative angles of the numbers on the wheel (eg. there are 36 numbers and each number would have 10 degree offset to each other, the one on the top, 12 o'clock position, will have the 0 and the next 10 and vice versa). I will rotate the wheel randomly and then determine the rotation of it based on some values that I can calculate (startPosition to finishedPosition). The wheel is an ImageView. Is there a way to actually do this? For example, get the top left x,y pos for its start and end, then by some formula to calculate how much it rotated. Or is there a better way to do this? There is not much of a source code to show it, so this is more like a mathematical question rather than a swift one. Any feedback is much appreciated.
To calculate rotation, you need coordinates of three points: start location sx, sy, end location ex, ey of the same point after rotation and center of rotation cx, cy
Then you can find angle using atan2 function
rot_angle = atan2((ex-cx)*(sx-cx)+(ey-cy)*(sy-cy), (ex-cx)*(sy-cy)-(ey-cy)*(sx-cx))
Note - I used argument order (x,y) from here, while most languages use reverse order (y,x), so check what order you really need (I have no experience in IOS languages). Also result value might be in radians or in degrees (above link doesn't specify it clearly)
Your question doesn't make much sense. If you rotate the wheel randomly, calculate the random value as an angle. If you want to change the previous rotation by some random angle, then do the math on the starting rotation and ending rotation. That is just adding and subtracting angles (modulo 2π). Then you will know how far it is rotated, and not have to calculate it.
Assuming you're talking about a roulette wheel, and not "Russian Roulette" (In American English at least, that term involves pointing a loaded revolver at your head) you'll need to track both the wheel rotation and the ball rotation. To apply the rotation to the wheel, you'll just take the image of the wheel and rotate it on the Z axis around it's x/y center point.
To plot the ball, you'll need to use trig to calculate the center of the ball based on the radius of the track the ball follows and the angle. But again, always track the angle, and then convert the angle to an x/y center point for the ball to plot it. Don't forget the angle and then have to convert back from the ball position to its angle. That's silly.

Is clockwise of Path.addArc in SwiftUI wrong?

Below is from the official Apple document:
This method creates an open subpath. The created arc lies on the perimeter of the
specified circle. When drawn in the default coordinate system, the start and end angles are based on the unit circle shown in Figure 1. For example, specifying a start angle of 0 radians, an end angle of π radians, and setting the clockwise parameter to true draws the bottom half of the circle. However, specifying the same start and end angles but setting the clockwise parameter set to false draws the top half of the circle.
But I found that the result seemed is just opposite. Below is my code
var body: some View {
Path { path in
path.addArc(center: CGPoint(x: 200, y: 370), radius: 50, startAngle: Angle(degrees: 0), endAngle: Angle(degrees: 180.0), clockwise: true)
path.closeSubpath()
}
}
I set the clockwise parameter to true but the result is the top half of the circle, not the bottom half
Did I understand wrong about Apple's document? My Xcode Version is 11.0 beta 4 (11M374r)
The meaning of the clockwise parameter exists in a quantum superposition that collapses when you examine the result. Unfortunately, it always collapses to the opposite meaning from what you wanted. 😅
More seriously, there are some flaws in Apple's documentation:
The “default coordinate system” really means the standard Cartesian coordinate system, in which the y axis increases toward the top of the canvas. But both SwiftUI and UIKit always set up the coordinate system with the y axis “flipped” so that y values increase toward the bottom of the canvas.
clockwise is accurate only in the standard Cartesian coordinate system. What it really means is “the direction of rotation that goes from the positive y axis toward the positive x axis”. So when you're working in a flipped coordinate system, clockwise means the opposite direction!
The diagrams below may clarify what I mean. The top diagram shows a standard Cartesian coordinate system, where the direction of rotation from the positive y axis to the positive x axis is indeed clockwise. The bottom diagram shows a flipped coordinate system, which is what SwiftUI and UIKit provide, and where the direction of rotation from the positive y axis to the positive x axis is actually counterclockwise, but which the APIs call “clockwise”.

Get reference frame with y axis pointing to magnetic north

With CoreMotion, is it possible to get a reference frame with the Y axis pointing to magnetic north? I would like to make the readings similiar to the ones from Android, which has the Y axis pointing to magnetic north. Thanks in advance.

Moving a drawing

This is an example of a drawing for the program I use
DrawCircle(mousePos.x, mousePos.y, mousePos.z, 650, ARGB(255, 255, 0, 0))
pretty simple x,y,z location radius of the circle and color, in this example it will draw a circle around my mouse and if my mouse moves the circle moves with it as is should, however what I would like to do is know how to draw a circle at lets say stationary position x,y,z and make the circle move from said position to new position a,b,c at x speed. sure I can just disable the draw at the starting point and redraw it at the destination point but I want the circle to visually move from point a to point b at speed x and I'm not sure what math I would need to be able to do this, furthermore if I was to draw a line how could I rotate that line in place so it looked like lets say helicopter blades spinning? Any help is appreciated thank you.
Not sure about LUA per se, but the solution to your problem is based around vector mathematics. LUA may have provide transformation functions to move a point in 3D space... not sure. As for the rotor blade question, if you are plotting the rotor blade in a 2D plane, you simply need a bit of trigonometry. There are lots of examples on the web, e.g.: trig example

2D vector math with direction and velocity

Heyo!
I'm starting to create games on iOS and I'm trying a Breakout-clone for start. As practice I wanted a ball to bounce around in a rectangle so I get my head around simple collision, direction and velocity.
My ball got the following:
Point position; // x, y
float direction;
float velocity;
In my "update" function, I want to move the ball in the current direction. What is the next position considering the velocity and direction?
Are there any helpers in some built-in frameworks in iOS?
I would really like to learn more about 2D-math so if someone got some reasources I would really appreciate if you send me a link.
What is the next position considering the velocity and direction?
Note that velocity already has direction; it is a vector
Bearing that in mind, your new position is:
position = CGPointMake(position.x + velocity.x, position.y + velocity.y)
Make velocity a CGPoint and make your direction variable redundant.
Convert the direction and velocity into a vector, scale it for time, and then add it to the current position, accounting for obstacles encountered along the path.

Resources