java - Smooth velocity based movement with predictable duration -


anyone has algorithm how smooth predictable movement point a -> b in 2d in language?

i need have 1 function setting velocity each frame:

function getvel(current_pos : vector2, dest_pos : vector2, current_vel : vector2) {  var new_vel : vector2d;  .......  return new_vel; } 

and corresponding:

function getdesttime(current_pos : vector2, dest_pos : vector2, current_vel : vector2 ) {  var duration : float;  .......  return duration; } 

simply using acceleration leads lots of sliding smoothdamp algorithm can predicted exact dest time need.

any ideas?

let's assume v(0) = 0 , v(t) = 0 and, v(t) quadratic function maximum value @ t = t/2.

accordingly, can assume form,

since point moves l within t seconds, integrating v(t) 0 t must give l. so, can equation,

solving these equations gives,

using these , b, can compute current velocity.

it rather long, made java toy realize this. please check it!

import java.awt.*; import javax.swing.*;  public class movepoint extends canvas implements runnable {     public static void main(string... args) {         thread thread = new thread(new movepoint());         thread.start();     }      private static final int width = 500;     private static final int height = 500;      public movepoint() {         super();         this.setbackground(color.white);         this.setforeground(color.black);         this.setsize(width, height);          jframe f = new jframe("move point");         f.setdefaultcloseoperation(jframe.exit_on_close);         f.add(this);         f.pack();         f.setvisible(true);     }      private point v;     private point s = new point(50, 50);     private point e = new point(450, 450);     private double duration = 5.0;     private double dt = 0.03;     private image buffer;     private graphics gbuf;      public void run() {         double t = 0.0;         v = s.copy();         while (t < duration) {             v = point.add(v, calcvelocity(v, s, e, t, duration).scale(dt));             t += dt;             repaint();             try {                 thread.sleep(30);             } catch (interruptedexception e) {                 e.printstacktrace();             }         }         system.exit(0);     }      public void paint(graphics g) {         if (gbuf == null) {             buffer = createimage(width, height);             gbuf = buffer.getgraphics();         }         gbuf.setcolor(color.white);         gbuf.fillrect(0, 0, width, height);         gbuf.setcolor(color.black);         gbuf.filloval((int)(v.x - 5), (int)(v.y - 5), 11, 11);         g.drawimage(buffer, 0, 0, this);     }      public void update(graphics g) {         paint(g);     }      public point calcvelocity(point current, point start, point goal, double t, double t) {         double l = point.distance(start, goal);         double = -6.0 / (t * t * t);         double b =  3.0 / (2.0 * t);          double s = (t - 0.5 * t);         double v = * s * s + b;         return point.subtract(goal, start).scale(v);     } }  class point {     public double x;     public double y;      public point(double x, double y) {         this.x = x;         this.y = y;     }      public point copy() {         return new point(x, y);     }      public static double distance(point p, point q) {         double dx = p.x - q.x;         double dy = p.y - q.y;         return math.sqrt(dx * dx + dy * dy);     }      public static point add(point p, point q) {         return new point(p.x + q.x, p.y + q.y);     }      public static point subtract(point p, point q) {         return new point(p.x - q.x, p.y - q.y);     }      public point scale(double s) {         return new point(x * s, y * s);     } } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -