# PVector: setting force of vector to something

**URL:** <https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481>\
**Category:** Coding Questions\
**Created:** [January 13, 2019, 7:57pm UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481 "2019-01-13T19:57:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 13, 2019, 7:57pm UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/1 "2019-01-13T19:57:14Z")

</div>

Recently 3Blue1Brown posted vid:

[![](https://img.youtube.com/vi/HEfHFsfGXjs/maxresdefault.jpg "The most unexpected answer to a counting puzzle") ](https://www.youtube.com/watch?v=HEfHFsfGXjs)

calculating PI with physics, and i wanted to recreate it in processing.  
I know it would be inacurate representation of it, but i want to challenge myself.  
So, how can i set force of vector to something? Pls dont post solution of this problem, so i can do it myself.  
I want to apply blue square starting velocity of 1m/s, and than transfer it to white object and so on.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [January 13, 2019, 11:27pm UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/2 "2019-01-13T23:27:57Z")

</div>

Which part of this is giving you trouble? Which step are you stuck on?

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [January 14, 2019, 1:31am UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/3 "2019-01-14T01:31:57Z")

</div>

As the video says, start with a physics simulator. You will want two masses, both of which will have a position, mass and speed.

Next, you will want a wall. Put the wall at x = 0, so positions are always positive…

Give your masses’ variables initial values.

Simulate the simulation for one step. That is, how do the positions of the masses change?

Next, add logic to simulate collisions.

What happens when the small mass’s position hits the wall at position x=0?

What happens when the large mass’s position hits the small masses’s position?

Will the large mass ever hit the wall?

Did you remember to count the collisions?

Try other initial values!

Here’s a page with some handy formulas…

> **[Elastic collision](https://en.wikipedia.org/wiki/Elastic_collision)**
>
> An elastic collision is an encounter between two bodies in which the total kinetic energy of the two bodies remains the same. In an ideal, perfectly elastic collision, there is no net conversion of kinetic energy into other forms such as heat, noise, or potential energy.
> During the collision of small objects, kinetic energy is first converted to potential energy associated with a repulsive force between the particles (when the particles move against this force, i.e. the angle between the force ...

Bonus question: Does the widths of the masses make any difference? Does their starting positions?

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 14, 2019, 1:37am UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/4 "2019-01-14T01:37:30Z")

</div>

Also you should know not all elastic collisions are handled the same and it’s important you find a solution that matches the one in this video. I’d check the links in the video description

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [January 14, 2019, 2:09am UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/5 "2019-01-14T02:09:28Z")

</div>

DON’T LOOK! This is what I came up with…

```auto
float am = 1, as = 0, ap = 10;
float bm = 100, bs = -1, bp = 100;
int c = 0;

void setup(){
  size(800,200);
}

void draw(){
  
  simulate();
  
  background(0);
  fill(200);
  noStroke();
  rect(0,0,20,height);
  translate(20,100);
  stroke(200);
  line(0,0,width,0);
  
  pushMatrix();
  translate(ap,-40);
  rect(0,0,40,40);
  
  popMatrix();
  
  pushMatrix();
  translate(bp+40,-40);
  rect(0,0,40,40);
  
  popMatrix();

  fill(200);
  text( c , 100, 50);
  
}

void simulate(){
  ap += as;
  bp += bs;
  
  if( ap <= 0 ){
    ap = 0;
    as *= -1;
    c++;
  }
  
  if( bp <= ap ){
    float as_t = ( (am - bm) / (am + bm) ) * as + ( (2 * bm) / (am + bm) ) * bs;
    bs = ( 2 * am / (am + bm) ) * as + ( (bm - am) / (am + bm) ) * bs;
    as = as_t;
    bp = ap;
    c++;
  }
  
}

```

Wow.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 18, 2019, 5:33pm UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/6 "2019-01-18T17:33:53Z")

</div>

Thx guys! I hope it didnt take u too much time… emm… and if u enjoyed doing it TfGuy, im happy for u 🙂

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 18, 2019, 5:34pm UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/7 "2019-01-18T17:34:21Z")

</div>

it didnt make too much sense…

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 21, 2019, 9:39pm UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/8 "2019-01-21T21:39:25Z")

</div>

@TfGuy44 's demo helps you see one or two digits. A solution that will allow you to view 4+ digits in a reasonable amount of time (and have the bounces appear within the horizontal screen space) is going to have to incorporate things that account for the exponential number of frames to display it.

There are many ways to approach that. For example, have simulate loop n number of times in a single frame,

```
steps = 8;
//...
simulate(steps);
//...

void simulate(int steps) {
  for (int i=0; i<steps; i++) {
  //...

```

…perhaps scale the horizontal output based on the number of degrees requested?

```
float scale = 1;
//...
scale = scale * 1/digits;
//...
translate(scale * (bp+40), -40);

```

Also, adjust the `frameRate()`

```
frameRate(120);

```

…et cetera.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [February 7, 2019, 2:23pm UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/9 "2019-02-07T14:23:13Z")

</div>

well 3Blue1Brown has made a vid with better way to solve it

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 7, 2019, 9:53pm UTC](https://discourse.processing.org/t/pvector-setting-force-of-vector-to-something/7481/10 "2019-02-07T21:53:33Z")

</div>

> [@CodeMasterX](#):
>
> a vid with better way to solve it

I don’t know what that means.

The video renders e.g. 314 clacks, then shows them again in slow motion, then shows them in groups of billion billions.

I was suggesting some ways to produce output like the video output, “recreate it in processing” – which was your question. Some ideas are multi-pass frames (which could be algorithmic), scaling, and frameRate control.
