Note that there are some explanatory texts on larger screens.

plurals
  1. POGravity in processing (Floors)
    text
    copied!<p>I'm using the <a href="http://www.processing.org" rel="nofollow">processing</a> java library to simulate a ball bouncing between two walls using Newtonian physics; horizontal velocity is a constant, as there is no acceleration. I got the simulation to work, however I would like to specify the height of the floor. However in my equations when I attempt to change direction when the ball is 10 pixels from the bottom of the screen, after every bounce, the ball gets lower and lower below the "floor". If I raise the floor to >20 pixels, the ball does not come to a halt and instead indefinitely bounces. Here is the pertinent code: Note that processing's coordinate system begins at the top and runs down and right. Thanks for the help in advance.</p> <pre><code>public class Testing extends PApplet { boolean inFloor=false; float xPosition=500; float yPosition=200; float xVelocity=25; float yVelocity=-80.0f; float yAccelleration=+10.0f; float elasticity=0.80f; public void setup (){ size(displayWidth,displayHeight); noStroke(); ellipseMode(RADIUS); frameRate(35); } public boolean sketchFullScreen() { return true; } public void draw(){ background(0); //Changes direction of motion when hitting a wall if(xPosition&gt;=displayWidth||xPosition&lt;0){ xVelocity=-xVelocity; } //supposed to change direction of motion when the ball hits the floor if(yPosition&gt;=displayHeight-20){ yPosition=(displayHeight-20); yVelocity=-(yVelocity)*elasticity; if(yVelocity&gt;=-1 &amp;&amp; yVelocity&lt;=0){ xVelocity=xVelocity*elasticity; yVelocity=0; yAccelleration=0; } } else{ yVelocity=yVelocity+yAccelleration; } yPosition=yVelocity+yPosition; xPosition=xPosition+xVelocity; ellipse(xPosition,yPosition,10,10); } </code></pre> <p><strong>Edit:</strong> Could this possibly be a timing issue?</p> <p><strong>Edit:</strong> Thank you for all the responses. Unfortunately I can't upvote any of them, (only 6 rep). I combined a mixture of @tobius_k's answer, @Roberto_Mereghetti's answer and some sample code from <a href="http://openprocessing.org/sketch/17115" rel="nofollow">OpenProcessing.org</a> and that solved it. In the solution provided below, because the canvas is measured in pixels (integer values), the use of floats to specify co-ordinates was causing graphical glitches in processing. So I implemented a system where float values were rounded, with decimal values being added to an accumulator ("xRounder" and "yRounder") which when were great than -1 or 1 were rounded and added to the Ball's current position. This gave me a floor!</p> <p><em>Final Code:</em></p> <pre><code> import processing.core.*; //import processing.xml.*; import java.applet.*; import java.awt.Dimension; import java.awt.Frame; import java.awt.event.MouseEvent; import java.awt.event.KeyEvent; import java.awt.event.FocusEvent; import java.awt.Image; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; import java.util.regex.*; public class Testing extends PApplet { int xPosition=500; int yPosition=200; float xRounder=0; float yRounder=0; float xVelocity=25; float yVelocity=-80.0f; float yAccelleration=+10.0f; float elasticity=0.80f; public void setup (){ size(displayWidth,displayHeight); noStroke(); ellipseMode(RADIUS); frameRate(15); } public boolean sketchFullScreen() { return true; } /* (non-Javadoc) * @see processing.core.PApplet#draw() */ public void draw(){ background(0); yPosition=round(yVelocity)+yPosition; yRounder+=(yVelocity-round(yVelocity)); xPosition=round(xVelocity)+xPosition; xRounder+=(xVelocity-round(xVelocity)); if(xRounder&gt;=1||xRounder&lt;=-1){ xPosition=xPosition+round(xRounder); xRounder=xRounder-round(xRounder); } if(yRounder&gt;=1||yRounder&lt;=-1){ yPosition+=round(yRounder); yRounder=yRounder-round(yRounder); } if(yPosition&gt;displayHeight-50 &amp;&amp; yVelocity&gt;0){ yPosition=displayHeight-50; yVelocity=-(yVelocity)*elasticity; xVelocity=xVelocity*elasticity; } if(xPosition&gt;=displayWidth||xPosition&lt;0){ xVelocity=-xVelocity; } yVelocity=yVelocity+yAccelleration; ellipse(xPosition,yPosition,10,10); } static public void main(String args[]) { PApplet.main(new String[] { "--bgcolor=#ECE9D8", "Testing" }); // new Testing().setVisible(true); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload