#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Having a vector maintain position once it hits ground.

edited May 2017

Hi, I'm trying to create a bow and arrow simulator, it's very much in the early stages. Right now I can launch little rectangles out from a launch point, but when they hit the ground they lay flat. I would like the rectangle to maintain the same angle and position as when it hits the ground. (think of a arrow embedding itself into the ground.)

there are only two vectors involved, gravity and the launch vector. Right now when the rectangle reaches y=350 I multiply the vel and acc by 0 and set the pos.y to the ground's y value.

this is a gif.

``````function Projectile(apos, angle, power) {

this.pos = createVector(apos.x,apos.y);
this.vel = p5.Vector.fromAngle(angle-(PI));
this.vel.mult(power/15);
this.acc = createVector(0,0);
this.applyForce = function(force){
}
this.update = function() {
this.acc.mult(0);
if (this.pos.y > 350){
this.pos.y = 350;
this.acc.mult(0);
this.vel.mult(0);
}
}
this.render = function(){
push();
translate(this.pos.x,this.pos.y);
rectMode(CENTER);
fill(255);
rect(0,0, 10, 6);
pop();
}
}
``````

P.S. I tried formatting the code, but it wasn't working. I feel like a noob.

Tagged:

• ok the gif didn't auto run so here is a direct link to it. My issue is clearly evident.

sta.sh/0p9y4p13oy2

• Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

Kf

The velocity vector defines the heading. Instead of setting it to zero, you should use a boolean field that defines if the projectile should be moving or if it reached the ground. When it reaches the ground, you changed your boolean field to false and then this false value is used so to stop any updates in the projectile position.

Kf

• edited May 2017

Okay, so I created a boolean variable isMoving and set it to true, then ran the update only when it's true. Once the projectile reaches y=350 i set the boolean to be false. This works, but the projectile keeps moving in their the +x or -x direction based on it's angle.

I feel like I need to grab the position values when it hits ground then reset to those?

``````   function Projectile(apos, angle, power) {
var isMoving = true;
this.pos = createVector(apos.x,apos.y);
this.vel = p5.Vector.fromAngle(angle-(PI));
this.vel.mult(power/15);
this.acc = createVector(0,0);
this.applyForce = function(force){
}
if(isMoving){
this.update = function() {
this.acc.mult(0);
if (this.pos.y > 350){
this.pos.y = 350;
this.acc.mult(0);
isMoving = false;
}
}
}
this.render = function(){
push();
translate(this.pos.x,this.pos.y);
rectMode(CENTER);
fill(255);
rect(0,0, 10, 6);
pop();
}
}
``````
• edited May 2017 Answer ✓

Once you do, the problem becomes apparent:

``````function Projectile(apos, angle, power) {

var isMoving = true;
this.pos = createVector(apos.x, apos.y);
this.vel = p5.Vector.fromAngle(angle-(PI));
this.vel.mult(power/15);
this.acc = createVector(0, 0);

this.applyForce = function(force) {
}

if (isMoving) { // <--- What is THIS doing HERE?
this.update = function() {
this.acc.mult(0);
if (this.pos.y > 350) {
this.pos.y = 350;
this.acc.mult(0);
isMoving = false;
}
}
}

this.render = function() {
push();
translate(this.pos.x, this.pos.y);
rectMode(CENTER);
fill(255);
rect(0, 0, 10, 6);
pop();
}

}
``````

Your check for `if(isMoving) {` should be INSIDE the `update()` function!

• edited May 2017

Haha, it worked thanks!!! Will work on formatting!