slingshot-esque aiming?
in
Programming Questions
•
1 month ago
Hi All,
I'm currently working on something that allows the user to "fire" points around the canvas, with each point connecting to all other points, creating cool geometric shapes. Although I'm having some difficulties with the aiming system. This is the current code I am using:
- void mousePressed() {
- vecXStart = mouseX;
- vecYStart = mouseY;
- aimDraw = true;
- }
- void mouseReleased() {
- float vecX = vecXStart - mouseX;
- float vecY = vecYStart - mouseY;
- point[pointCount] = new linePoint(mouseX, mouseY, vecX, vecY, random(255), random(255), random(255)); // locationx, locationy, speedx, speedy, R, G, B
- pointCount++;
- aimDraw = false;
- }
This works fine, but the only problem is the velocity. I want to make it so if the user draws a long line, the point doesn't go shooting off at a crazy speed.
I tried dividing the vectors by 10, but this caused the points to fire off slightly to the left/right. I'm guessing I'd have to use some king of trigonometry here?
Here is the full code, if it's hard to understand what's going on above:
- linePoint[] point;
- PVector[] Vector;
- int pointCount = 0;
- float R = 1;
- float G = 6;
- float B = 9;
- boolean strobe = false;
- float vecXStart;
- float vecYStart;
- boolean aimDraw = false;
- void setup() {
- size(1366, 768);
- background(0);
- point = new linePoint[50];
- }
- void draw() {
- strobe();
- if (aimDraw) {
- background(0);
- stroke(255);
- line(mouseX, mouseY, vecXStart, vecYStart);
- }
- for (int i = 0; i < pointCount; i++) {
- point[i].move();
- }
- connectDots();
- }
- void strobe() {
- if(strobe) {
- R += 0.1;
- G += 0.1;
- B += 0.1;
- background((127*sin(R)),(127*sin(G)),(127*sin(B)));
- }
- }
- void keyReleased() {
- strobe = !strobe;
- }
- void mousePressed() {
- vecXStart = mouseX;
- vecYStart = mouseY;
- aimDraw = true;
- }
- void mouseReleased() {
- float vecX = ((vecXStart - mouseX) / 10);
- float vecY = ((vecYStart - mouseY) / 10);
- point[pointCount] = new linePoint(mouseX, mouseY, vecX, vecY, random(255), random(255), random(255)); // locationx, locationy, speedx, speedy, R, G, B
- pointCount++;
- aimDraw = false;
- }
- void connectDots() {
- for (int i = 0; i < pointCount; i++) {
- for (int j = 0; j < pointCount; j++) {
- stroke(point[i].R, point[i].G, point[i].B);
- strokeWeight(2);
- line(point[i].x, point[i].y, point[j].x, point[j].y);
- }
- }
- }
- class linePoint {
- int x, y;
- float xSpeed, ySpeed;
- float R, G, B;
- int threshold = 10;
- linePoint (int tempX, int tempY, float tempXSpeed, float tempYSpeed, float tempR, float tempG, float tempB) {
- x = tempX;
- y = tempY;
- xSpeed = tempXSpeed;
- ySpeed = tempYSpeed;
- R = tempR;
- G = tempG;
- B = tempB;
- }
- void move() {
- if ((x >= width) || (x <= 0)) {
- if ((xSpeed > threshold) || (xSpeed < -threshold)) {
- xSpeed *= -1;
- } else {
- xSpeed *= -1.1;
- //strobe = !strobe;
- }
- }
- if ((y >= height) || (y <= 0)) {
- if ((ySpeed > threshold) || (ySpeed < -threshold)) {
- ySpeed *= -1;
- } else {
- ySpeed *= -1.1;
- }
- }
- x += xSpeed;
- y += ySpeed;
- point(x, y);
- }
- }
1