Calculation of objects positions with using sine and cosine

Hello, friends. So, i start experiment with objects positioning on scenes from code.
Because static point of player view is on X = 0 and Y = 0, i decided set objects on circumferences around camera with few meters step.
For position calculation i try to use school formulas for searching point on circle by sin and cos.
But it work wrong, because all test objects puts on one quadrant (really, i know, program can’t work wrong, but may be written wrong).
I show code and screenshots. I think, it fail, because all sin and cos in my program have positive value… Maybe, i need help with Pascal math?
In next code SectorsCount := 80, so, i want to change 80 sectors of circle and add in every sector 20 Experimental Tree.

constructor TEnvironment.CreateEnvironment(var Viewport: TCastleViewport);
begin
  SetLength(GridOfEssences, SectorsCount, 20);
  for i := 0 to SectorsCount - 1 do
    for j := 0 to 19 do
      begin
        GridOfEssences[i][j] := TCastleImageTransform.Create(FreeAtStop);
        Viewport.Items.Add(GridOfEssences[i][j]);
        GridOfEssences[i][j].Size := Vector2(2, 2);
        GridOfEssences[i][j].Translation := Vector3(5 * i * cos (360 / 80) * i, 1, 5 * j * sin (360 / 80) * i);
        GridOfEssences[i][j].Url := 'castle-data://Objects/Tree_01.png';
    end;
end;

P.S I little shy to share all my code, because know this is hard labour to understand somebody code. But i can show it, if it helps…

At first glance this:

doesn’t make much sense. Most likely what you wanted to write was this:

Vector3(5 * j * cos (360 / 80 * i), 1, 5 * j * sin (360 / 80 * i));

I.e. first radius is 5 * j you’ve had 5 * i instead

Next you have i outside of sin/cos argument, to have a “circle” you need sin(k * i).

1 Like

You can look at CGE DrawCircleOutline implementation to see how to use SinCos to get what you need :slight_smile: castle-engine/castleglutils_draw_primitive_2d.inc at master · castle-engine/castle-engine · GitHub

The important lines:

  for I := 0 to CircleSlices - 1 do
  begin
    SinCos(2 * Pi * I / (CircleSlices - 1), S, C);
    CirclePoint[I] := Vector2(
      Middle.X + S * HorizontalRadius,
      Middle.Y + C * VerticalRadius);
  end;

This generates CircleSlices points on a circle (well, it’s a circle if HorizontalRadius = VerticalRadius). These points are then rendered with

  DrawPrimitive2D(pmLineLoop, CirclePoint, ...);

This code is used e.g. for TCastleShape with circle: User Interface | Manual | Castle Game Engine

1 Like

Thanks too much :upside_down_face: