I am attempting to write a code that simulates the motion of each planet around the sun in the solar system using a graph in Octave 4.2.1. One aspect of this project is to have the sun and all the planets on the graph be circles of a certain size (i.e. a specific radius) and to have each circle moving. The problem I am having is using the built-in set() function to set new x and y values for the center of each circle that is already drawn. I do not want to redraw each circle in each iteration of my loop, because it causes very choppy and inconsistent motion, and with the set() function it is very smooth and consistent motion. I have been able to use the set() function for simple point using a handle and the plot() function. But with the function I want to use, which is drawCircle(), using the set() function always returns an error.
Here is a simple version (that is not working) of what I want to do in my actual code:
figure 1
s=drawCircle(0,0,1);
hold on
axis([-2 2 -2 2])
set(s,'x0', 1)
This code returns the error: "set: unknown line property x0". To be more specific I am asking how to set the x and y coordinates of the center of the circle drawn with drawCircle() using the set() function, after it is drawn.
I would really appreciate if anyone has any ideas of how to get this to work, or any alternative workarounds to achieve the same thing.
It seems to me this does what you want:
clear;clf
th=2*pi*linspace(0,1,19);xc=cos(th);yc=sin(th);
h=plot(xc,yc,'linewidth',1);
axis([-2 2 -2 2]);
pause
x=get(h,'xdata');y=get(h,'ydata');
set(h,'xdata',x+1,'ydata',y+0.5)
Related
I've been struggling with this for the past 3 days so here we go:
I am building a virtual photo-studio using Three.js. The rotation is set with three sliders, one for each axis. Rotation needs to happen around the world axis. So far, I can get the object to rotate around the world x axis, however, y and z rotation only happens locally. Here is the code for one of the rotation sliders using quaternions.
let rotationX = new THREE.Quaternion()
sliderX.oninput = function () {
let newVec = new THREE.Vector3(1,0,0)
let newRad = THREE.Math.degToRad(this.value)
rotationX.setFromAxisAngle(newVec, newRad)
pivot.quaternion.multiplyQuaternions(rotationX, rotationY).multiply(rotationZ)
}
This approach has gotten me the farthest. The problem is that the rotation always happens around the vector of the first quaternion in the multiplication chain. That would be the quaternion "rotationX" of the following line.
pivot.quaternion.multiplyQuaternions(rotationX, rotationY).multiply(rotationZ)
Because I am working with quaternions, switching around the order of multiplication is also not an option as it changes the outcome.
Any help would be greatly appreciated.
Here is a link to the dependency free repo in case you want to recreate the situation: https://github.com/maxibenner/exporter
I have some SDF circles in spark ar studio that I need to animate in a circular path continuously. Is this possible? at the moment animating with patches I can only animate into a straight line using the x and y coords. How would I go about doing this?
Heres how it is currently set up.
You can achieve a circular path using sine and cosine, by animating the transition values from 0 to 2*Pi (6.283). Here is an example:
There are some nice visualizations that explain how sine and cosine work, for example: https://www.reddit.com/r/visualizedmath/comments/7runef/unit_circle_visualization_of_sine_and_cosine/
I am working with D3.js in version 3.x.
Following this tutorial, I am trying to move an element, in this case a rectangle, along a path and rotate the element based on its position on the path.
This works perfectly fine for any path, that does not have an arc as part of it. Something like this:
M56.200012207,96.1999969482c-51,295,-52,294,280,184c-286,-273,-243,-261,-35,-204
The angle is calculated "correct" on a path like this, and the element translates along this path in a smooth way.
But as soon as the path contains an arc, the angle gets some strange values at some points of the arc and therefore the rectangle flips / jumps around based on this angle while translating along the path. A path with an arc looks like this for example:
M56.20001220703125,66.19999694824219a174.796117,174.796117,0,1,0,275.99999999996874,-2.000000000042192
My assumption:
As for creating an arc, we only give some values, like startpoint and angle, and the rest of the points needed to draw the arc are computed in some way by svg.
Based on my tries, i saw that some of the computed points are not actually where i would expect them to be.
The function used in the linked example calculates two points, p1 and p2 and calculates the angle for the rotation by using Math.atan2 on p1 and p2.
I know the points are very close together, but to simplify my explanation, i have some distance between them in the image.
In this image, i would expect p1 to have smaller values for x and y than p2.
There are a lot of points in that area between p1 and p2, so the function calculates the required angle for each of these pairs. For most of them, the angle is correct, but not for all of them as can be seen in the following console.log() output:
Notice that for most points, the angle is around 67 degree, which is supposed to be like this on the respective area of the path. But then there randomly is one angle that is 33 degree, which of course causes this flipping / jumping effect.
For "expected angles" the transition looks good and something like this:
For "not expected angles", the transition looks bad and something like this:
If this happens a few times throughout the transition, this produces the flipping / jumping effect.
My question:
Why is this happening? The arc looks fine and all the points seem to be in place based on visually looking at them on screen.
Is there any way to avoid this while still being able to use paths with an arc inside?
Thank you very much for any help.
Edit: Added a jsfiddle to show you the problems discussed here and in the comments: Element rotation with point-along-path interpolation - not possible on path with arc?
There is a bug in current Chrome (57 and 58 as of writing) that affects the return values for getPointAtLength() when operating on arc path commands.
https://bugs.chromium.org/p/chromium/issues/detail?id=719516
For now there seems to be no easy fix other than smoothing the output values yourself. Or avoiding arc commands in your paths.
Hi, I am making a car game where I draw a car shape Rectangle as follows. xP and yP is coming dynamically from the keyboard event in JavaScript and so is the rotation.
ctxDrift.clearRect(0, 0, 426, 754);
ctxDrift.save();
ctxDrift.beginPath();
ctxDrift.translate(xP-car.getWidth()/2, yP-car.getHeight()/2);
ctxDrift.rotate((Math.PI / 180) * car.getRotation());
ctxDrift.translate(-xP, -yP);
ctxDrift.rect(xP-car.getWidth()/2, yP-car.getHeight()/2, car.getWidth(), car.getHeight());
ctxDrift.fillStyle = 'yellow';
ctxDrift.fill();
ctxDrift.restore();
Now there are some obstacles, with Rectangle shape, which are not rotated. Now how could I check the hit between these 2 objects. Or say how to check the rectangle points lies inside another rectangle, if rotated?
Even before you even get started with collision testing:
Canvas does not track where your objects are on the canvas. You must manually keep track of the accumulated .translate() and .rotate() done by the user. You do this by capturing the transformation matrix changes for each user keyboard event. Then you accumulate the transforms into one final transformation matrix that you can use to start hit testing.
From there, the math on collision testing gets quickly complicated!
Your simplest collisiion test is simply to surround each rectangle with a circle and then calculate whether the circle centerpoints are within the sum of the 2 circle radii. The code looks like this:
function CirclesCollide(x1,y1,radius1,x2,y2,radius2){
return ( Math.sqrt( ( x2-x1 ) * ( x2-x1 ) + ( y2-y1 ) * ( y2-y1 ) ) < ( radius1 + radius2 ) );
}
If you want better collision testing and you're willing to wade through LOTS of math, here is a good source of 3 collision tests: http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection
Perhaps the best solution is to use a canvas library like FabricJs which tracks where your objects are on the canvas and provides the hit-testing for you. Easy as this!
var theyAreColliding = myCar.intersectsWithObject(myObstical);
The easiest way is to rotate the rectangle bounding boxes, so they are essentially no longer rotated, before you do the collision check. Then rotate them back before the image is drawn.
Even better, have a bounding box that doesn't rotate which can be used for broad-phase testing (a quick and cheap check to see if you need to then do a narrow-phase check).
This is known as an axis-aligned bounding box, or AABB for short. This greatly simplifies your collision detection code.
update: Found this link that might be useful.
This is what i am looking for this Query
http://www.rgraph.net/blog/2012/october/new-html5-canvas-features.html
canvas have now addHitRegion() function, where we can track easily for this.
New One and best
http://www.playmycode.com/blog/2011/08/javascript-per-pixel-html5-canvas-image-collision-detection/
I have finally added my own logic, which is here
http://jslogic.blogspot.in/2013/02/javascript-bound-rectangle-area-while.html
I have a situation that I'm not realy sure how I can handle. I have an openGl object of about 20k vertices, and I need to offer the user the possibility to select any one of these vertices (let's say with a smallest margin of error possible). Now here is what I want to do in order to do this:
Next to the 3D canvas of the object, I also offer the user 3 'slices' done by the planes x=0; y=0 and z=0. Say for the simplest example for a sphere these would be 3 circles, correponding to 'cutting' out one of the dimensions. Now let's take the z=0 one for the purpose of the example. When the user clicks on a point say (x_circle, y_circle) i would like to get the actual point in the 3d representation where he clicked. The z would be 0 of course but I can't figure out a way to get the x and y. I can easily translate that (x_circle, y_circle) -> (x_screen, y_screen) which would have the same result as a click on the canvas at those coordinates, but I need to find a way to translate that into the (x, y, 0) coordinate in 3D view.
The same thing would need to be done with x=0, y=0 but I think if I can understand/implement a way for z=0 I can just apply more or less the same solution with an added rotation over something. If anyone can help with any examples/code or even math behind this it would help a lot because at the moment I'm not really sure how to proceed.
When the user clicks, you can render the vertices using GL.POINTS (with a certain size, if you like) to an off-screen buffer using a shader that renders each vertex' index into RGBA. Then you read back the pixel on the mouse position and see what index it is.