Issues with velocity
in
Programming Questions
•
10 months ago
Hey guys, I've been working on a bowman like game, and I'm trying to figure out how to properly make the projectile motion for the arrow.
//drag the mouse to 'fire' the arrow
void setup(){
size(700, 500);
colorMode(HSB, 100);
}
int s = 60;
int x, y;
int x_start, x_end = mouseX;
int y_start, y_end = mouseY;
float xmag; //this is the velocity of horizontal motion
void mousePressed(){
x_start = mouseX;
y_start = mouseY;
}
void mouseDragged(){
strokeWeight(3);
stroke(30-(dist(x_start, y_start, mouseX, mouseY))/10, 255, 255); //
line(x_start, y_start, mouseX, mouseY);
strokeWeight(1);
}
void mouseReleased(){
x_end = mouseX;
y_end = mouseY;
xmag = (dist(x_start, y_start, mouseX, mouseY))*cos(atan((y_end-y_start)/(x_end-x_start))); //horizontal //component of velocity
}
void arrow(int x, int y, int s){
fill(255, 99, 0);
stroke(0);
strokeWeight(2);
rect(x + xmag, y + xmag, s, s/20);
strokeWeight(1);
triangle(x+s + xmag, y+s/10 + xmag, x+s + xmag, y-6*s/100 + xmag, x+11*s/10 + xmag, y+2*s/100 + xmag);
x = x + floor(xmag);
}
void draw(){
background(255, 0, 99);
if(mousePressed)
{strokeWeight(3);
stroke(30-(dist(x_start, y_start, mouseX, mouseY))/10, 255, 255); //color of line
line(x_start, y_start, mouseX, mouseY);}
else
{line(x_start, y_start, x_end, y_end);}
arrow(20, 20, s);
}
edit: the ultimate goal is to make a game fairly similar to Bowman. The angle of the line you drag is the projectile ange, and it's magnitude is the force shot with. I'm trying to work out the horizontal motion first. I found how to do it, but since the code that moves the arrow is within the mouseReleased function it only gets called once and doesn't make it appear to be moving.
1