We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello :)
I made some simple shapes (only quadrilaterals and ellipses)
I aligned them using the input of the mouse, in order to have them follow the mouse
Now I would like to have certain shapes delayed, so that they are following the movements of the mouse from a couple of seconds ago.
How can I code this kind of delay?
Thanks in advance!
Aidan :)
Answers
This is what I've done so far:
very cool! How did you figure out all the numbers in your calls to
quad
andellipse
? You could probably make this a lot easier to read and write with a for loop.In case you have no experience in using Arrays, Classes, loops or linear interpolation this could be tough. The following code is an example how it could be achieved, "play" with it to see how it works:
you can use
rect
instead ofquad
if you're making a rectangle, it's a bit easier. If you userectMode(RADIUS)
andellipseMode(RADIUS)
, then the size of the rect and the circle can have the same radius. Makes it way easier!You can draw the same thing like this:
having something trailing the mouse is a little harder, depending on how you want to do it.
ah. Here's a pretty simple way to do a trailing draw. Basically what you have to remember is that you need to store the coordinates of where the mouse is right now, and you need to hold onto that information until you're ready to use it. So, in this example, we have a red dot and then a blue dot that is drawn some 10 frames later, based on where the mouse was. So we need to have 10 frames of history at any time.
@jordanorelli: The new operator is slow (peformance wise), using an array with a given amount of entries (like in my example above) is fast. Using remove(0) on an ArrayList (or any other data structure with an underlying Array) is always a bad idea. All entries above 0 have to be shiftet one index down, which means ~18 array accesses. Use a LinkedList instead, it has no underlying Array and it's get(index) is therefore really slow (use getFirst() instead), but random insertion/removal is super fast, as it will only update one link/reference per insertion/removal.
yeah, it makes sense to use a LinkedList instead because the backing store for an ArrayList is ... an array.
umm, the descriptions of "this is fast" and "this is slow" are kinda... misleading, though.
get(0)
andgetFirst()
will be very similar on a linked list, since you never have to traverse beyond the first element of the list, which you have a reference to to begin with. "Random insertion is fast" isn't strictly true; any operation you do using an index on a linked list will still require traversing the list up to that index, so inserting to the middle of a large linked list isn't going to be fast if all you have is an index. Random insertion to a linked list is only fast if the elements of the list are exposed, but in Java's LinkedList, only the contents of the list elements are exposed, so I don't see how you would ever be in a situation where you could perform a list insertion in the way you've stated.Same thing with LinkedList:
get(0) will attempt to loop the list's entries to find the specified index, therefore it will create an Iterator and object creation is still costly. getFirst() will simply return the first element (it's reference is already saved in the list).
You can simply insert/remove elements while iterating a list with an Iterator. In case of entity/particle/object/item/etc. lists this is the most common case.
This code is better.
What about a version using just a regular array? And no extra PVector re-instantiations along the way?! :P
Well, I've got an old online trailing example which I've tweaked from @9egenwind back then:
http://studio.processingtogether.com/sp/pad/export/ro.9GTDpA6dp4tH1/latest
http://forum.processing.org/one/topic/newbie-question-understandig-problems-with-arrays
So I've taken the same approach and tweak yours as well! :D
I guess performance is above par! ;;)
http://studio.processingtogether.com/sp/pad/export/ro.90vdKMfkiO$zf/latest
@GoToLoop: You were faster again! :))
I know the lerp()'s are a bit slow, but the following code also utilizes arrays and avoids new calls in the draw loop:
Wow! It's a different twist! And very similar to the old Chase-Shadow Circles I had:
http://studio.processingtogether.com/sp/pad/export/ro.9GTDpA6dp4tH1/latest
Prominent differences is that yours right-shifts the array, while mine left-shifts it.
And yours use lerp() for color effects, while mine use alpha + different sizes.
@GoToLoop: Nice sketch!
It's advisable to place every fixed configuration in setup(), so the big draw() "loop" has less things to worry about and be faster:
@Poersch
get(0)
won't create any iterator, it just creates one reference and does a bunch of pointer following: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/LinkedList.java#LinkedList.entry(int)it just goes through a few checks but eventually returns a reference to the same thing that
getFirst
does, so the difference is effectively nothing.anyway, I kinda feel like this sort of premature optimization only serves to make the material less approachable for a beginner.
Sorry, I'm currently not at home. In any case, using getFirst() instead of get(0) has nothing to do with optimization. It's simply more readable/elegant maybe even easier to understand.
@jordanorelli , nice source code resource on Java API! \m/
However, I've got to concur w/ @Poersch :
1 of the 1st things I've learned about Java is that LinkedList is the slowest class to access elements thru' direct index!!! b-(
Just compare an ArrayList vs LinkedList loop using get() to check what I mean! ;))
While getFirst() or element() are merely ->
return header.next.element;
, get() is a loop as deep long as the index requested!Although it seems a lower index like
get(0)
the looping search ends up shorter after all! :-$Somewhat that's true. But it's also important to spread adequate knowledge. 8-X
Well, IMO, if some1 intends to use index to access data within a structure, LinkedList shouldn't be his 1st choice by far!!! (~~)
@jordanorelli: Sorry, I was wrong get(0) will only take ~4 times as long as getFirst() but it's still an overhead and not as clean as getFirst(), imo.
@jordanorelli, @GoToLoop: Optimization can be premature, but that highly depends on how frequent your code is running. One reason the most AAA games are not written in Java isn't it bad performance, it's the habit of most Java coders to write highly unoptimized code. Have a look at Minecraft's source code for example, you'll find instructions like this.getBlockIcon(block).getIconName().equals("grass_top") (in the render loop, that will be executed for every block) instead of block == Block.grass. Minecraft is full of those bad code examples. Many of it's players need mods like Optifine, which replaces parts of Minecraft's rendering code, in order to be able to play it on the low graphics settings. With a little bit of optimization you could at least double the framerate. I don't say, you should optimize everything, but if there's a way to double or triple the speed of your code with almost the same amount or even fewer instructions, then do it. (%)
@Poersch 4 times as long? how are you measuring that? My yardstick is reading the code ;)
@jordanorelli: Just count the opcodes and multiply them by how many CPU cycles they need in average. ;)
Anyhow, maybe we should stop spamming this thread with our tech talk, agfd hasn't even responded to one of our answers and for a beginning programmer this could easily be too much information. At the end of the day, I hope we gave him a nice repertoire of possible solutions to his problem.