한 걸음/Creative coding
▼ 예제 3-1. rotate 함수를 이용한 회전 운동 //각도 float angle = 0; //각속도(속도와 비슷하다) float aVelocity = 0; //각가속도(가속도와 비슷하다) float aAcceleration = 0.0001; void setup() { size(200, 200); } void draw(){ background(200); fill(175); stroke(0); rectMode(CENTER); translate(width/2, height/2); rotate(angle); line(-50,0,50,0); ellipse(50,0,8,8); ellipse(-50,0,8,8); //각속도는 속도와 비슷한 방법으로 구한다. aVelocity += aAcceleration; //..
Mover[] movers = new Mover[20]; void setup() { size(200, 200); smooth(); background(77); for(int i=0; i height){ location.y = 0; } else if(location.y < 0){ location.y = height; } } } 결과물 중력은 가까울 수록 강하게 작용해야 한다. 즉, 객체가 마우스에 가까워지면 가속도가 더 커진다.
속도와 벡터를 활용한 이동 Mover mover; void setup() { size(200, 200); smooth(); // Mover 클래스로 객체를 생성 mover = new Mover(); } void draw(){ background(187); //Mover 객체의 함수 호출 mover.update(); mover.checkEdges(); mover.display(); } class Mover { PVector location; PVector velocity; Mover() { location = new PVector(random(width), random(height)); velocity = new PVector(random(-2,2), random(-2,2)); } void update()..
Nature of Code 책에서 나오는 벡터는 유클리드 벡터를 의미한다. 유클리드 벡터란, 크기와 방향을 가지는 객체이다. 벡터를 사용하면 아래와 같이 코드를 조금 더 간략하게 변경할 수 있다. //before float x; float y; float xspeed; float yspeed; //after Vector location; Vector speed; 벡터는 다양한 곳에 사용되는데, 가장 간단한 예시는 한 지점에서 다른 지점으로 이동하는 것을 표현할 때이다. 벡터연산 add() : 벡터 덧셈 sub() : 벡터 뺄셈 mult() : 벡터 곱셈 div() : 벡터 나눗셈 mag() : 벡터의 크기 구하기 setMag() : 벡터의 크기 지정 normalize() : 방향이 같고 단위 길이가 1인..
Nature of Code 책으로 프로세싱 공부를 시작했다. processing 사용을 위해서 먼저 프로그램을 설치해야 한다. 공식홈페이지(https://processing.org/)의 다운로드 탭에서 자기 컴퓨터에 맞는 프로그램을 골라서 설치해주면, 아래와 같은 화면에서 코딩할 수 있다. 상단바 파일-예제를 클릭하면 (단축키 컨트롤+쉬프트+O) 미리 만들어둔 예제를 실행해볼 수도 있다. 아래는 화면을 벗어나지 않고 튕기는 공을 구현한 소스코드이다. float x = 100; float y = 100; float xspeed = 1; float yspeed = 3.3; //setup 함수는 프로그램 시작 전 한 번만 실행됨 void setup() { size(200,200); smooth(); backg..