Changing an object's X,Y coordinates

Started by Jordanowen42, Wed 01/01/2025 08:38:43

Previous topic - Next topic

Jordanowen42

Hello all-

I'm currently working on a Myst style game and I have a puzzle where there's a circular table top with concentric circles carved into it. The player drops marbles into the circular grooves and repositions them to affect the puzzle's outcome. So, my question is this: how do I alter the x,y coordinates of each of the marbles in the grooves so that it appears that each time one is clicked on they're being moved gradually around the groove?

Kind regards,
-Jordan

Khris

Moving an object along a circle is done by changing the angle and using sine and cosine to calculate the coordinates.

pseudo code:
  object.x = Maths.Cos(angle) * radius;
  object.y = Maths.Sin(angle) * radius;


An angle of zero produces the coordinates (radius, 0) i.e. exactly east of the center of the circle.
As the angle increases, the x coordinate goes down to 0 while the y coordinate increases towards the radius. I.e. at an angle of 90° the object is now directly south of the center.

Jordanowen42

#2
So my question then is how do I establish the circumference of the circle?

Snarky

#3
The circumference is 2π r (where r is the radius of the circle), and the 360° angle around is also radians (the unit used in the Maths.Sin() and Maths.Cos() functions).

This means that if you rotate something α radians around a point that is r distance away, it moves a distance αr along the circumference of the circle around the point. (So, for example, if you have a circle that has a radius of 100 pixels, and you want to rotate it so each point on the circumference moves 20 pixels, that's a 0.2 radian rotation because 0.2*100=20. Note that this is the distance along the curved circumference, not the straight path.)

π is represented in AGS as the constant Maths.Pi, so you would calculate the full circumferences as:

Code: ags
  float circumference = 2*Maths.Pi*radius;

Khris

Moving a marble from point1 to point2 within a groove means it moves from angle1 to angle2.
A screenshot / the room background would be a big help with regard to providing the best approach.

SMF spam blocked by CleanTalk