CodingTrain / Suggestion-Box

A repo to track ideas for topics
571 stars 86 forks source link

VERY important Simple Pendulum Project #1172

Open oheuysal opened 5 years ago

oheuysal commented 5 years ago

I have a project.This is very important to me.I have similar code to this project but this code is too hard and not understanding to me.I want to very easy code because this project wants to be some calculation and simple pendulum.Details: adsiz OR https://i.imgur.com/GUGheoR.png

I PREPARE THIS CODE:

void setup() { size(1000,900); } float t = 0; void draw() { float g = 9.8; float l = 1; float w = sqrt(g/l); float theta0 = 1.5; float theta = theta0 sin(wt); println(theta); t += 0.1; }

AND I FIND THIS CODE:

Pendulum p; void setup() { size(1000,950); // Make a new Pendulum with an origin position and armlength p = new Pendulum(new PVector(width/2,500),300); } void draw() { background(255); p.go();
} void mousePressed() { p.clicked(mouseX,mouseY); } void mouseReleased() { p.stopDragging(); } class Pendulum { PVector position; // position of pendulum ball PVector origin; // position of arm origin float L; // Length of length float angle; // Pendulum arm angle float aVelocity; // Angle velocity float aAcceleration; // Angle acceleration float ballr; // Ball radius float damping; // Arbitary damping amount

boolean dragging = false;

Pendulum(PVector origin, float r) {

origin = origin_.get();
position = new PVector();
L = r_;
angle = PI/2;

aVelocity = 0.0; aAcceleration = 0.0; damping = 0.995; // Arbitrary damping ballr = 100; // Arbitrary ball radius } void go() { update(); drag();
display(); } void update() {

if (!dragging) {
  float gravity = 0.05;                              // gravity constant
  aAcceleration =sqrt((gravity / L)) * cos(angle);  // Calculate acceleration(w) 
  aVelocity += aAcceleration;                 // Increment velocity
  aVelocity *= damping;                       // Arbitrary damping
  angle += aVelocity;                         // Increment angle
                }
          }

void display() { position.set(Lcos(angle), Lsin(angle), 0); // Polar to cartesian conversion position.add(origin); // Make sure the position is relative to the pendulum's origin

stroke(0);
strokeWeight(5);

line(origin.x, origin.y, position.x, position.y);
ellipseMode(CENTER);

ellipse(position.x, position.y, ballr, ballr); }

void clicked(int mx, int my) { float d = dist(mx, my, position.x, position.y); if (d < ballr) { dragging = true; } }

// This tells us we are not longer clicking on the ball void stopDragging() { if (dragging) { aVelocity = 0; // No velocity once you let go dragging = false; } }

void drag() { // If we are draging the ball, we calculate the angle between the // pendulum origin and mouse position // we assign that angle to the pendulum if (dragging) { PVector diff = PVector.sub(origin, new PVector(mouseX, mouseY)); // Difference between 2 points angle = atan2(-1*diff.x, diff.y) - radians(90); // Angle relative to vertical axis } } }

BUT SECOND CODE VERY HARD AND COMPLICATE TO ME. Please help me to simple code :D Thank you

Californ1a commented 5 years ago

He's already done a pendulum simulation before in one of the older videos, and more recently, a double pendulum.