Movement and movement curves
This commit is contained in:
		
							parent
							
								
									65f36ae820
								
							
						
					
					
						commit
						6fb770a0b8
					
				
							
								
								
									
										20
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								main.c
									
									
									
									
									
								
							@ -6,6 +6,7 @@
 | 
			
		||||
#include <raylib.h>
 | 
			
		||||
 | 
			
		||||
#include "card.h"
 | 
			
		||||
#include "move.h"
 | 
			
		||||
 | 
			
		||||
char *text = "こんにちわ、 世界!";
 | 
			
		||||
 | 
			
		||||
@ -16,7 +17,26 @@ int main(int argc, char** argv) {
 | 
			
		||||
    Card *c = malloc(sizeof(Card));
 | 
			
		||||
    c->position = (Vector2) { 200, 200 };
 | 
			
		||||
 | 
			
		||||
    Move *m = malloc(sizeof(Move));
 | 
			
		||||
    m->origin = c->position;
 | 
			
		||||
    m->destination = c->position;
 | 
			
		||||
    m->curve = CURVE_EASE_IN_OUT;
 | 
			
		||||
 | 
			
		||||
    float delta;
 | 
			
		||||
    Vector2 mouse_position;
 | 
			
		||||
 | 
			
		||||
    while (!WindowShouldClose()) {
 | 
			
		||||
      delta = GetFrameTime();
 | 
			
		||||
      if (IsMouseButtonPressed(0)) {
 | 
			
		||||
	mouse_position = GetMousePosition();
 | 
			
		||||
	m->origin = c->position;
 | 
			
		||||
	m->destination = mouse_position;
 | 
			
		||||
	m->current_time = 0.;
 | 
			
		||||
	m->end_time = 1.;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      c->position = move_position(m, delta);
 | 
			
		||||
 | 
			
		||||
      BeginDrawing();
 | 
			
		||||
      ClearBackground(RAYWHITE);
 | 
			
		||||
      draw_card(c);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										23
									
								
								move.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								move.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
#include <math.h>
 | 
			
		||||
#include "move.h"
 | 
			
		||||
 | 
			
		||||
Vector2 move_position(Move *m, float delta) {
 | 
			
		||||
  m->current_time += delta;
 | 
			
		||||
  float percentage = m->current_time / m->end_time;
 | 
			
		||||
  if (percentage < 0.0) percentage = 0.0;
 | 
			
		||||
  else if (percentage > 1.0) percentage = 1.0;
 | 
			
		||||
 | 
			
		||||
  switch (m->curve) {
 | 
			
		||||
  case CURVE_LINEAR:
 | 
			
		||||
    return (Vector2) {
 | 
			
		||||
      ((m->destination.x - m->origin.x) * percentage) + m->origin.x,
 | 
			
		||||
      ((m->destination.y - m->origin.y) * percentage) + m->origin.y
 | 
			
		||||
    };
 | 
			
		||||
  case CURVE_EASE_IN_OUT:
 | 
			
		||||
    percentage = -(cos(PI * percentage) - 1) / 2;
 | 
			
		||||
    return (Vector2) {
 | 
			
		||||
      ((m->destination.x - m->origin.x) * percentage) + m->origin.x,
 | 
			
		||||
      ((m->destination.y - m->origin.y) * percentage) + m->origin.y
 | 
			
		||||
    };
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								move.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								move.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
#ifndef _HF_MOVE_
 | 
			
		||||
#define _HF_MOVE_
 | 
			
		||||
 | 
			
		||||
#include <raylib.h>
 | 
			
		||||
 | 
			
		||||
typedef enum Curve {
 | 
			
		||||
  CURVE_LINEAR,
 | 
			
		||||
  CURVE_EASE_IN_OUT,
 | 
			
		||||
} Curve;
 | 
			
		||||
 | 
			
		||||
typedef struct Move Move;
 | 
			
		||||
 | 
			
		||||
struct Move {
 | 
			
		||||
  Vector2 origin;
 | 
			
		||||
  Vector2 destination;
 | 
			
		||||
  Curve curve;
 | 
			
		||||
  float current_time;
 | 
			
		||||
  float end_time;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Vector2 move_position(Move *m, float delta);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user