GSDP:GAM100/CProcessing/CP Vector Angle()

From Inside
Jump to: navigation, search

CP_Vector_Angle()

Description

Returns the angle between two CP_Vectors as a float.

Parameters

CP_Vector_Angle(CP_Vector a, CP_Vector b)

  • a - (CP_Vector) - First vector in calculation
  • b - (CP_Vector) - Second vector in calculation

Example

CP_Vector position;
CP_Vector position2;
CP_Vector speed;

void init()
{
  position = CP_Vector_Set(CP_System_GetWindowWidth() / 2, CP_System_GetWindowHeight / 4);
  position2 = CP_Vector_Set(CP_System_GetWindowWidth() / 2, 3 * CP_System_GetWindowHeight / 4);
  speed = CP_Vector_Set(50.0f, 0.0f);

  CP_Settings_EllipseMode(CP_POSITION_CENTER);
  CP_Font_Set(CP_Font_GetDefault());
}

void update()
{
  // Check if direction should be inverted
  if (position.x > CP_System_GetWindowWidth())
  {
    speed = CP_Vector_Negate(speed);
    position.x = CP_System_GetWindowWidth();
  }
  else if (position.x < 0)
  {
    speed = CP_Vector_Negate(speed);
    position.x = 0;
  }

  // Update position
  position = CP_Vector_Add(position, CP_Vector_Scale(speed, CP_System_GetDt()));
  position2 = CP_Vector_Subtract(position2, CP_Vector_Scale(speed, CP_System_GetDt()));

  // Draw the circles
  CP_Settings_Fill(CP_Color_Create(255, 0, 0, 255));
  CP_Graphics_DrawCircle(position.x, position.y, 40);
  CP_Settings_Fill(CP_Color_Create(0, 0, 255, 255));
  CP_Graphics_DrawCircle(position2.x, position2.y, 40);

  // Draw the angle the two points make with the top left corner
  float angle = CP_Vector_Angle(position, position2);
  char angle_text[128] = {0};
  sprintf_s(angle_text, 128, "Origin to Positions Angle: %.0f", angle);
  CP_Settings_Fill(CP_Color_Create(0, 0, 0, 255));
  CP_Font_DrawText(angle_text, CP_System_GetWindowWidth() / 2 - 160, CP_System_GetWindowWidth() / 2);
}

Related

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>