GSDP:GAM100/CProcessing/CP Vector Add()

CP_Vector_Add()

Description

Returns the sum of two CP_Vectors as a new CP_Vector.

Parameters

CP_Vector_Add(CP_Vector a, CP_Vector b)

  • a - (CP_Vector) the vector to add with b
  • b - (CP_Vector) the vector to add with a

Example

CP_Vector position;
CP_Vector speed;

void init()
{
  // Set position to the center of the screen
  position = CP_Vector_Set(CP_System_GetWindowHeight()/ 2, CP_System_GetWindowWidth() / 2);
  // Set speed to 50 in the x-direction
  speed = CP_Vector_Set(50.0f, 0.0f);
 
  // Set Ellipse mode so that the center is drawn at the specified location
  CP_Settings_EllipseMode(CP_POSITION_CENTER);
  // Set fill color for shapes to red
  CP_Settings_Fill(CP_Color_Create(255, 0, 0, 255));
}

CP_BOOL forward = true;

void update()
{
  CP_Settings_Background(CP_Color_Create(255, 255, 255, 255));

  // Check if direction should be inverted
  if (position.x > CP_System_GetWindowWidth())
  {
    forward = false;
    position.x = CP_System_GetWindowWidth();
  }
  else if (position.x < 0)
  {
    forward = true;
    position.x = 0;
  }

  // Update position
  if (forward)
    position = CP_Vector_Add(position, CP_Vector_Scale(speed, CP_System_GetDt()));
  else if(!forward)
    position = CP_Vector_Subtract(position, CP_Vector_Scale(speed, CP_System_GetDt()));

  // Draw the circle
  CP_Graphics_DrawCircle(position.x, position.y, 40);
}

Related