We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey All,
Having the hardest time simulating a delayed mouse cursor.
It ALMOST works, but keeps flashing, then last point disappears.
I want the delayed cursor (ellipse) to draw just as smoothly and remain just as the non-delayed one (rectangle). But the delayed one is choppy, flashing and eventually disappears. (I actually want to remove the rectangle and keep the lagged cursor only);
I've tried using delay() and sleep(), framerate() and vector lerp(), etc, to try and smooth this out, (probably in all the wrong ways).
I know it may have to do with my array storing method, sampling rate (there are multiple coordinates for one ms time), and I am sure the solution is much simple than this.
I don't want to use something like sleep that stops execution. In short its just replaying a set of coordinates?
Also I plan to do this for many other input methods, so I am wondering if there should be a more general approach.
Any ideas?
ArrayList <chronoPointer> points;
int currentTime; //milliseconds
int lagTime = 300; //milliseconds
void setup() {
frameRate(60);
size(400, 400);
points = new ArrayList<chronoPointer>();
noCursor();
}
void draw()
{
background(60);
smooth();
fill(color(0, 255, 0));
rect(mouseX, mouseY, 20, 20); // this is the non-delayed cursor. Just for debugging point of reference.
currentTime = millis(); //milliseconds
fill(color(0, 255, 0));
text("Current Time: " + currentTime + " ms", 20, 20);
text("Frame Rate: " + int(frameRate), 20, 35);
text("Added Lag: " + lagTime + " ms", 20, 50);
fill(color(225, 173, 140));
turtleCursor(currentTime); //sends current time to the method to draw the delayed cursor.
}
void mouseMoved()
// Storing mouse coordinates and the time of capture in an array custom type
{
points.add(new chronoPointer(mouseX, mouseY, currentTime));
}
void mousePressed() {
fill(color(255, 0, 0));
// For Debugging purposes to see the times for each coordinate.
for (int i = 0; i < points.size(); i++) {
println(" #" + i + ": { X: " + points.get(i).getCoords().x + " Y: " + points.get(i).getCoords().y + ", TIME: " + points.get(i).getCoordTime() + " milliseconds} ");
println("\n");
}
println("\n\n");
}
void mouseReleased() {
fill(color(0, 255, 0));
}
/* My method to draw delayed cursor*/
void turtleCursor(int currentTime) {
int delay;
try {
for (int i = 0; i< points.size(); i++) {
delay = points.get(i).getCoordTime(); // I know this may be wrong. Multiple coodinates, or one set?, will be returned?
if (delay == currentTime - lagTime) { //get the time from the object
points.get(i).getCoords().lerp(points.get(i+1).getCoords().x, points.get(i+1).getCoords().y, 0.0, 0.1); //trying to lerp here to get smooth drawing between two points.
ellipse(points.get(i).getCoords().x, points.get(i).getCoords().y, 25, 25);
}
}
}
catch (Exception e) {
}
}
// this class stores the pointer x y coordinates and associated time value.
class chronoPointer {
PVector pointCoordinates;
int pointTime;
void init() {
println("A chronoPointer has been initialized");
}
public chronoPointer(float xCoord, float yCoord, int timeFrame) {
pointCoordinates = new PVector(xCoord, yCoord);
pointTime = timeFrame;
}
public PVector getCoords() {
return pointCoordinates;
}
public int getCoordTime() {
return pointTime;
}
}
Answers
The following code is from Processing's bundled example StoringInput. Are you looking for something like this?
Hi -VK, Sorry for the late reply. Yes and thank you. I actually had previously played with that example code before with no luck. Later, I played with it a bit more and created a much shorter method (so alas it was simpler). Thank you again for getting me to reconsider this bit.
So here is my much shorter method turned out as follows: I have two screens one with no delay effect on the left and the other with the effect on the right.