Timeline speed keeps increasing when I recall method - javafx

I have a timeline which moves my images along the Y coordinates of my screen, but I use the same timeline for each new wave occurence. The problem is the speed keeps increasing at an impossible to keep up speed, is there any way I can fix this?
Duration movEn = new Duration(55);
KeyFrame f = new KeyFrame(movEn, e -> moveEnemies());
Timeline timeline = new Timeline(f);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
That is the timeline code I use and it works great for the first few, but then it just gets way to fast

Related

How to Find global position of objects in a rotating scene THREE.JS

I am working on a 3D mesh manipulator using this : http://leapmotion.com. So far, I have been able manipulate the points just fine, by 'grabbing' and moving them, however I now want to be able to rotate the mesh and work on the opposite face. What I have done is add an extra object that is called 'rotatable' as Shown below:
scene=new THREE.Scene();
camera = new THREE.PerspectiveCamera(70,window.innerWidth/window.innerHeight,1,8000)
renderer=new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, maxLights:5 } )
//This is the 'Mesh Scene'
rotatable = new THREE.Object3D()
scene.add(rotatable)
//Mesh we are altering
var material = new THREE.MeshNormalMaterial()
material.side=2
var geom = new THREE.SphereGeometry(200,10,10);
var sphere = new THREE.Mesh(geom, material)
rotatable.add(sphere)
I am then trying to change the vertices of this sphere, but to do so I need to do a 'collision test' in order to see if the vertex is being 'grabbed' This involves check the vertex position and see if it coincides with one of your finger position (psuedoCode below)
if(finger.x == vertex.x && finger.y == vertex.y && finger.z == vertex.z){
vertex.grabbed = true
}
This works fine when the rotatable's rotation is zero, however when it starts to rotate, the collision test will still be testing for the unrotated vertex position (which makes sense). My question is how to find the position of the vertex in its 'scene / global' position. The only way I can think of doing this so far is to calculate the rotation of the 'rotatable' and use this vector to calculate the new vertex position.
I know nothing about math, so this may not be the way to go, and even if it is I will have to struggle through it so hard that I won't ever know if I'm just doing the math incorrectly, or this isn't the way I should go about calculating it. Obviously I'm willing to go through this work, but just want to make sure this is the way to do it, rather then an other simpler method.
If there are any other questions about the code, please let me know, and Thanks in advance for your time!
Isaac
To get the world position of a vertex specified in local coordinates, apply the object's world transform to the vertex like so:
vertex.applyMatrix4( object.matrixWorld );
(I am not familiar with leapmotion, so hopefully it does not impact this answer.)
Tip: maxLights is no longer required. And it is best to avoid material.side = 2. Use material.side = THREE.DoubleSide instead.
You can find the constants here: https://github.com/mrdoob/three.js/blob/master/src/Three.js
three.js r.55

JavaFX Animation

I've been going through some JavaFX tutorials and I have a few questions about the animation system used.
Timeline timeline = new Timeline();
Node circle = circles;
timeline.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO, // set start position at 0
new KeyValue(circle.translateXProperty(), random() * 800),
new KeyValue(circle.translateYProperty(), random() * 600)),
new KeyFrame(new Duration(40000), // set end position at 40s
new KeyValue(circle.translateXProperty(), random() * 800),
new KeyValue(circle.translateYProperty(), random() * 600)));
// play 40s of animation
timeline.play();
primaryStage.show();
}
What I didn't understand were the use of the translateXProperty() functions (and Y) as well as the purpose of the random numbers in the KeyValue constructor. The way I understood the process to work was you created two KeyFrames which you interpolated between the two frame and that the KeyValue objects simply different states of your nodes, but I'm not sure that's correct.
The tutorial I'm looking at is here: http://docs.oracle.com/javafx/2/get_started/animation.htm
I just used one circle in the code above to simplify things.
The code moves (translates) a circle from one random location to another over a period of 40 seconds. The first keyframe specifies the starting translation co-ordinate. The second keyframe specifies the end translation co-ordinate. The JavaFX animation subsystem takes care of interpolating the translation key values across the intermediate frames behind the scenes.
The code could have animated different properties such as scale or rotation. And it could have used absolute values rather than random ones. It just used the properties and values it did because they specify the effect the author is trying to achieve in this instance.

Make QGraphicsView do smooth centerOn

i am not really newbie in Qt, but there are a few things i don't know...
I am programming in Python, but feel free to post your answers in ANY language.
So, i have a few QGraphicsItem (s), positioned inside a QGraphicsScene, the scene is viewed with a normal QGraphicsView. Everything is normal.
My scene is very large, 10,000 x 10,000 pixels, all graphic items are scattered around.
For example :
# Creating Scene object.
scene = QtGui.QGraphicsScene()
scene.setSceneRect(0, 0, 10000, 10000)
# Creating View object.
view = QtGui.QGraphicsView()
view.setScene(scene)
# Adding some objects to scene.
# scene.addItem(...)
# ...
# The list of items.
items = scene.items()
# This is how i center on item.
view.centerOn(some_item)
view.fitInView(some_item, Qt.KeepAspectRatio)
My question is, how can i center the view on every item, using something similar to centerOn, but smoothly ?
Currently, centerOn goes FAST on next item, i want to move it slooowly, maybe using QPropertyAnimation with easing curve ?
I tried to move the view to the next item using view.translate(1, 1) in a big cicle, but the movement is too fast, just like centerOn.
I tried to put some waiting with time.sleep(0.01) between the translating, but the windows blocks untill the cicle exists... so it's bad.
Thank you very much !
I once used a QTimeLine (with EaseInOutCurve), connected it to a slot, and then used that value to translate the view rect, like this:
const QRectF r = ...calculate rect translated by timeline value and trajectory...
view->setSceneRect( r );
view->fitInView( r, Qt::KeepAspectRatio );
For the trajectory I used a QLineF with start and end position for the smooth scrolling.
Then one can use the value emitted by timeline nicely with QLineF::pointAt().
In my case I needed to set both SceneRect and fitInView to make it behave like I wanted.
I solved my problem by placing a high value on setSceneRect. Then I centralize the scene on an object or position.
example:
this->ui->graphicsView->setSceneRect (0,0,100000000, 100000000);
this->ui->graphicsView->centerOn(500,1030);
With the setSceneRect size GraphicsView does not work right.

Forcing smaller redraw regions in actionscript

I've got a small action-script chart, that's meant to be live updating, and also be able to support more than 10000 points of data. The way it's currently set up it doesn't need to redraw the whole chart if the new line we wish to add doesn't extend that chart's boundaries.
Yet it does, the redraw regions show the whole chart as being redrawn as opposed to the single line i need to add.
When the chart gets a new piece of data from the javascript it does the following.(some stuff has been stripped for clarity.
private function registerJSCallbacks() : void
{
ExternalInterface.addCallback( "addData", addData );
}
private function addData( val : * ) : void
{
trace( "addData",val );
var g: Graphics = this.graphics;
g.moveTo(x1,y1);
g.lineTo(x2,y2);
}
Is there any better way to do that, that won't redraw the whole screen? Is my coding pattern wrong for this type of update?
I'm a novice so even vaguely relevant advice would be appreciated.
You're editing the underlying vector, so it has to redraw the whole thing. You've got a couple options:
(easiest): Spawn a new Sprite after every X draw operations, so that each draw only recalculated a few vector lines
(more involved): Use one Sprite to draw, and every X draw operations, write the graphics in the sprite to a backing BitmapData object (using bitmapData.draw) and clear the sprite.
Option 2 is probably performs better than option 1, but I haven't benchmarked this specific scenario. You might get comparable performance if you sprite.cacheAsBitmap = true on each Sprite in option 1 as you move to a new "active" sprite.

Flex Matrix rotate Image n degrees

How can I rotate an Image eg. 180 degrees clockwise using the Matrix
I can use the following code to rotate the image 90 degrees, but it is incremental, meaing
var matrix:Matrix = new Matrix();
matrix.rotate(Math.PI/2);
matrix.tx = imgControl.content.height;
var bitmapData:BitmapData = new BitmapData(imgControl.content.height, imgControl.content.width);
bitmapData.draw(imgControl.content, matrix);
imgControl.source = new Bitmap( bitmapData);
Each time I run the code the image is rotated +90 degrees.
What I want is not to increment by 90 each time, but explicit say rotate 180, rotate90 and so on.
I am not familiar with the Matrix, but I guess it does real bitmapdata manipulation rather than just eg. rotate the Image component box (arrest me if I am wrong).
If so, I guess I have to reset the image each time I do the rotate command.
Am I missing something?
Thanks in advance for any advice and tips
Ran
The matrix does no real bitmapdata manipulation.
It is the bitmap.draw call that draws the rotated image of the imgcontrol.content into the bitmap, after which your code overwrites imgcontrol.content with the rotated image.
So as your code is currently standing, yes, you either have to refresh the image from scratch before every rotation, or you will have to keep track of the rotations in a variable and calculate how many more times you have to rotate to get to the desired rotation.
If you need to do multiple 90 degrees rotation in one step, then replace
matrix.rotate(Math.PI/2);
with
matrix.rotate(Math.PI/2 * howmanytimesyouwanttorotateby90degrees);

Resources