Difference between revisions of "GSDP:GAM100/CProcessing/vec2 add()"

From Inside
Jump to: navigation, search
>Zachary.logsdon
(New page: {{GSDP:GAM100API.css}} = vec2_add() = == Description == Returns the sum of two Vec2s as a new Vec2. ==== Parameters ==== ...)
 
>Zachary.logsdon
 
Line 20: Line 20:
 
   speed = vec2_set(50.0f, 0.0f);
 
   speed = vec2_set(50.0f, 0.0f);
  
   circleMode(CENTER);
+
   ellipseMode(CENTER);
 
   fill(color(255, 0, 0, 255));
 
   fill(color(255, 0, 0, 255));
 
}
 
}

Latest revision as of 17:26, 5 October 2019

vec2_add()

Description

Returns the sum of two Vec2s as a new Vec2.

Parameters

vec2_add(Vec2 a, Vec2 b)

  • a - (Vec2) Left hand operand of addition.
  • b - (Vec2) Right hand operand of addition.

Example

Vec2 position;
Vec2 speed;

void init()
{
  position = vec2_set(canvasWidth / 2, canvasHeight / 2);
  speed = vec2_set(50.0f, 0.0f);

  ellipseMode(CENTER);
  fill(color(255, 0, 0, 255));
}

bool forward = true;

void update()
{
  background(color(255, 255, 255, 255));

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

  // Update position
  if (forward)
    position = vec2_add(position, vec2_scale(speed, dt()));
  else if(!forward)
    position = vec2_sub(position, vec2_scale(speed, dt()));

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

Related

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>