Rotate SCNNode if user tap on any child node it will point towards camera - scenekit

Sorry If my question is silly I am new in SceneKit.
I have added a globe using SCNnode sphere and added pins as a child node for that sphere.
Now when user tap on any child node I want to rotate globe to such angle that my child node will come toward center for camera.
Thanks in advance

If I'm understanding correctly, tap on any child and the globe rotates such that it faces you. I "think" you can do it this way.
Provided you have placed your child nodes accurately and they are pointing away from the center, then have lookat constraints on all of your children but set them to empty. Once one is clicked, set the lookat constraint on that child node and point it at the camera node.
func setTarget()
{
node.constraints = []
let vConstraint = SCNLookAtConstraint(target: targetNode)
vConstraint.isGimbalLockEnabled = true
node.constraints = [vConstraint]
}
The more difficult part may be to make sure your child nodes are actually facing away from the center because it will matter. IE: Poke a pencil in a ball and rotate it in your hand. The point of the pencil matters because it's glued to your parent object exactly how you placed it initially. If it was facing the center point of your globe for example, then you'd get the opposite effect.
One way to test it is to create the child nodes with a pointer - in this case, it's just a turret from one of my games.
let BoxGeometry = SCNBox(width: 0.8, height: 0.8, length: 0.8, chamferRadius: 0.0)
let vNode = SCNNode(geometry: BoxGeometry)
BoxGeometry.materials = setDefenseTextures(vGameType: vGameType)
let tubeGeometry = SCNTube(innerRadius: 0.03, outerRadius: 0.05, height: 0.9)
let fireTube = SCNNode(geometry: tubeGeometry)
tubeGeometry.firstMaterial?.diffuse.contents = data.getTextureColor(vTheme: 0, vTextureType: .barrelColor)
fireTube.position = SCNVector3(0, 0.2, -0.3)
let vRotateX = SCNAction.rotateBy(x: CGFloat(Float(GLKMathDegreesToRadians(-90))), y: 0, z: 0, duration: 0)
fireTube.runAction(vRotateX)
vNode.addChildNode(fireTube)
Once you have placed these on your globe and they are rotated correctly, setting lookat constraints "should" work. It should look kind of like a pin cushion (sorry, only example I can think of), and then your globe should rotate towards the camera.
Hopefully

Related

How to scale textures stored in/called from a Godot array

I have 100 .png images loaded into a Godot 3.5.1 array of objects (named splash_planets) as StreamTextures. I need to know how to reference them to allow scaling. I am able to draw them using the following:
for splash_planet in splash_planets:
center = Vector2(splash_planet._x, splash_planet._y)
draw_texture(splash_planet._texture, center)
Now I want to do something like:
draw_texture(splash_planet._texture, center, (scale_x, scale_y))
Thanks in advance for any advice.
First of all, in this line:
draw_texture(splash_planet._texture, center)
You would be drawing the texture with the upper left corner on the coordinates of center. If you want to draw the texture with the center on center, you do this:
var texture := splash_planet._texture
var size := texture.get_size()
draw_texture(texture, center - size * 0.5)
Since you are using draw_texture and you want to scale, you can use draw_set_transform:
draw_set_transform(Vector2.ZERO, 0.0, Vector2(scale_x, scale_y))
As you can see the third parameter is the scale. The first two parameters are translation and rotation. Once you have set the transform you can do the draw call as usual:
draw_set_transform(Vector2.ZERO, 0.0, Vector2(scale_x, scale_y))
var texture := splash_planet._texture
var size := texture.get_size()
draw_texture(texture, center - size * 0.5)
Calling draw_set_transform again (or calling draw_set_transform_matrix) will overwrite the previos transform.
Alternatively you can use draw_texture_rect:
var texture := splash_planet._texture
var size := texture.get_size() * Vector2(scale_x, scale_y)
var rect := Rect2(center - size * 0.5, size)
draw_texture_rect(texture, rect, false)
In this case we compute the rectangle where to draw the texture. We also specify we don't want to tile it (the false as third argument), so Godot will stretch it.
You might also be interested in draw_texture_rect_region, which allows you to draw a rectangular region of the source texture.

Display sun position with Expo and Three.js

So the idea is quite simple: given the sun's position (azimuth and elevation) I want my app to be able to display a shape using augmented reality when the camera is pointing at the sun.
So there is a few steps:
Convert azimuth and elevation into radians, then into cartesian coordinates to get a simple vector {x, y, z}.
Get the phone's gyroscope data to get its orientation in space as a 3D vector {x, y, z}.
Calculate new coordinates for the sun regarding the phone orientation.
Display a random shape using Three.js at these coordinates.
1 and 2 are quite easy. There are a lot of APIs out there giving the sun's position depending on a location. Then I used a formula to convert the sun's spherical coordinates into cartesian ones:
x = R * cos(ϕ) * sin(θ)
y = R * cos(ϕ) * cos(θ)
z = R * sin(ϕ)
with R, the distance of the point from the origin, θ (the azimuth) and ϕ (the elevation).
I got the device's orientation in space with Expo.io, using their Device Motion API. Documentation here
I'm really struggling with the third step. I don't know how to combine sun and device coordinates in space, and project the whole thing through Three.js perspective camera.
I found this post the other day: Compare device 3D orientation with the sun position but I've found the explanations a bit confusing.
Let's say I want to display a cube with Three:
const geometry = new THREE.BoxGeometry(0.07, 0.07, 0.07);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
cube.position.x = 0;
cube.position.y = 0;
cube.position.z = -1;
The final goal here will be to find the correct {x, y, z} so the cube can be displayed at the sun's location. This vector will be of course updated every time the user moves his phone in space.

How do I transform glOrtho clipping planes?

I've created an OpenGL application where I define a viewing volume with glOrtho(). Then I use gluLookAt() to transform all the points.
The problem is - as soon as I do this all the points fall out of the clipping plane because it is "left behind". And I end up with a black screen.
How do I change the viewing volume so that it is still tight to my objects once they have been transformed by gluLookAt()?
Here's some code to help better illustrate my problem:
Vector3d eyePos = new Vector3d(0.25, 0.25, 0.6);
Vector3d point = new Vector3d(0, 0, 0.001);
Vector3d up = new Vector3d(0, 1,0);
Matrix4d mat = Matrix4d.LookAt(eyePos, point, up);
GL.Viewport(0, 0, glControl1.Width, glControl1.Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, 1, 0, 1, 0, 1);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.LoadMatrix(ref mat);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Enable(EnableCap.DepthTest);
GL.DepthMask(true);
GL.ClearDepth(1.0);
GL.Color3(Color.Yellow);
GL.Begin(PrimitiveType.Triangles);
GL.Vertex3(0.2, 0.3, 0.01);
GL.Vertex3(0, 0.600, 0.01);
GL.Vertex3(0.600, 0.600, 0.01);
GL.Color3(Color.Blue);
GL.Vertex3(0, 0.6, 0.01);
GL.Vertex3(0.2, 0.3, 0.01);
GL.Vertex3(0.7, 0.5, 0.03);
GL.Color3(Color.Red);
GL.Vertex3(0.100, 0.000, 0.01);
GL.Vertex3(0, 0.0, 0.01);
GL.Vertex3(0.600, 0.600, 0.03);
GL.End();
glControl1.SwapBuffers();
I suppose a more succinct revision of my question is how do I transform two points by the lookat matrix?("mat" in my code)
Okay, I got your problem.
You set the camera position to (0.25, 0.25, 0.6) and with the point to look at your view direction is (-0.25, -0.25, -0.599) (has to be normalised). As I mentioned in my comment below your question, you are drawing a cube in the upper right corner of your view direction.
So the two points (0.25, 0.25, 0.6) (the camera position) and the point defined by the camera position and your view direction, your up vector and your right vector (Have a look at this) define the cube of the things which are shown on the screen (the two points define a diagonal within the cube). Unfortunatly, all of your points are not within this cube.
May it be, that you mixed up the camera position with the point you wanna look at? The change of the two values should solve your problem. The rest of your code seems to be correct. The only thing you have to do is to check whether your points are within the clip space of your projections. Another solution to check where your points are, would be to use, e.g. the keyboard input, to move your camera dynamically.

Trouble zooming with axis crossing at 0 in Oxyplot

[Edit: I submitted an issue about this on the OxyPlot GitHub]
I have an OxyPlot cartesian graph in a WPF Window with some FunctionSeries. When I set PositionAtZeroCrossing at true for both the axes, several problems appear :
1) The titles are not displayed correctly, one doesn't even appear. Changing the value of TitlePosition does not seem to change anything.
2) When zooming in or moving around, the x axis goes outside the graph area, as shown below :
Both problems do not appear when PositionAtZeroCrossing is not set as true.
I am wondering if there is a correct way to fix those problems, or a workaround (I am not familiar with OxyPlot).
//My PlotModel is binded to the Model of a PlotView in my WPF control.
//FonctionQlimPlim, maxX, maxY, minX and maxY are defined elsewhere
PlotModel plot = new PlotModel()
{
PlotType = PlotType.Cartesian
};
plot.Axes.Add(new LinearAxis()
{
Position = AxisPosition.Bottom,
Minimum = minX,
Maximum = maxX,
Title = "Q (kVAR)",
PositionAtZeroCrossing = true
});
plot.Axes.Add(new LinearAxis()
{
Position = AxisPosition.Left,
Minimum = minY,
Maximum = maxY,
Title = "P (kW)",
PositionAtZeroCrossing = true
});
//One of the FunctionSeries
var f = FonctionQlimPlim;
f.Color = OxyColors.Red;
plot.Series.Add(f);
It seems like the usage for this property is something along the following:
Let's assume your input is an x (horizontal),y (vertical) graph.
Let's say your x values go from 1 to 20, and let's assume that your y values will be a random number between -10 and 10.
So, we're expecting to see a graph with a 20point jumping up and down randomly.
When you set the axis PositionAtZeroCrossing, you're telling Oxyplot to put it where the crossing is at. Here's some screenshots to help you out: Link to bigger image.
So, depending on where your zero is, it might be far far outside of your viewable screen, and hence it seems to not be there.
To be honest, I don't see the point in setting them bot to true, but it might suit some needs I guess.
It was a bug, traced here, that was fixed in October 2014.

WPF: Getting new coordinates after a Rotation

With reference to this programming game I am currently building.
alt text http://img12.imageshack.us/img12/2089/shapetransformationf.jpg
To translate a Canvas in WPF, I am using two Forms: TranslateTransform (to move it), and RotateTransform (to rotate it) [children of the same TransformationGroup]
I can easily get the top left x,y coordinates of a canvas when its not rotated (or rotated at 90deg, since it will be the same), but the problem I am facing is getting the top left (and the other 3 points) coordinates.
This is because when a RotateTransform is applied, the TranslateTransform's X and Y properties are not changed (and thus still indicate that the top-left of the square is like the dotted-square (from the image)
The Canvas is being rotated from its center, so that is its origin.
So how can I get the "new" x and y coordinates of the 4 points after a rotation?
[UPDATE]
alt text http://img25.imageshack.us/img25/8676/shaperotationaltransfor.jpg
I have found a way to find the top-left coordinates after a rotation (as you can see from the new image) by adding the OffsetX and OffsetY from the rotation to the starting X and Y coordinates.
But I'm now having trouble figuring out the rest of the coordinates (the other 3).
With this rotated shape, how can I figure out the x and y coordinates of the remaining 3 corners?
[EDIT]
The points in the 2nd image ARE NOT ACCURATE AND EXACT POINTS. I made the points up with estimates in my head.
[UPDATE] Solution:
First of all, I would like to thank Jason S for that lengthy and Very informative post in which he describes the mathematics behind the whole process; I certainly learned a lot by reading your post and trying out the values.
But I have now found a code snippet (thanks to EugeneZ's mention of TransformBounds) that does exactly what I want:
public Rect GetBounds(FrameworkElement of, FrameworkElement from)
{
// Might throw an exception if of and from are not in the same visual tree
GeneralTransform transform = of.TransformToVisual(from);
return transform.TransformBounds(new Rect(0, 0, of.ActualWidth, of.ActualHeight));
}
Reference: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/86350f19-6457-470e-bde9-66e8970f7059/
If I understand your question right:
given:
shape has corner (x1,y1), center (xc,yc)
rotated shape has corner (x1',y1') after being rotated about center
desired:
how to map any point of the shape (x,y) -> (x',y') by that same rotation
Here's the relevant equations:
(x'-xc) = Kc*(x-xc) - Ks*(y-yc)
(y'-yc) = Ks*(x-xc) + Kc*(y-yc)
where Kc=cos(theta) and Ks=sin(theta) and theta is the angle of counterclockwise rotation. (to verify: if theta=0 this leaves the coordinates unchanged, otherwise if xc=yc=0, it maps (1,0) to (cos(theta),sin(theta)) and (0,1) to (-sin(theta), cos(theta)) . Caveat: this is for coordinate systems where (x,y)=(1,1) is in the upper right quadrant. For yours where it's in the lower right quadrant, theta would be the angle of clockwise rotation rather than counterclockwise rotation.)
If you know the coordinates of your rectangle aligned with the x-y axes, xc would just be the average of the two x-coordinates and yc would just be the average of the two y-coordinates. (in your situation, it's xc=75,yc=85.)
If you know theta, you now have enough information to calculate the new coordinates.
If you don't know theta, you can solve for Kc, Ks. Here's the relevant calculations for your example:
(62-75) = Kc*(50-75) - Ks*(50-85)
(40-85) = Ks*(50-75) + Kc*(50-85)
-13 = -25*Kc + 35*Ks = -25*Kc + 35*Ks
-45 = -25*Ks - 35*Kc = -35*Kc - 25*Ks
which is a system of linear equations that can be solved (exercise for the reader: in MATLAB it's:
[-25 35;-35 -25]\[-13;-45]
to yield, in this case, Kc=1.027, Ks=0.3622 which does NOT make sense (K2 = Kc2 + Ks2 is supposed to equal 1 for a pure rotation; in this case it's K = 1.089) so it's not a pure rotation about the rectangle center, which is what your drawing indicates. Nor does it seem to be a pure rotation about the origin. To check, compare distances from the center of rotation before and after the rotation using the Pythagorean theorem, d2 = deltax2 + deltay2. (for rotation about xc=75,yc=85, distance before is 43.01, distance after is 46.84, the ratio is K=1.089; for rotation about the origin, distance before is 70.71, distance after is 73.78, ratio is 1.043. I could believe ratios of 1.01 or less would arise from coordinate rounding to integers, but this is clearly larger than a roundoff error)
So there's some missing information here. How did you get the numbers (62,40)?
That's the basic gist of the math behind rotations, however.
edit: aha, I didn't realize they were estimates. (pretty close to being realistic, though!)
I use this method:
Point newPoint = rotateTransform.Transform(new Point(oldX, oldY));
where rotateTransform is the instance on which I work and set Angle...etc.
Look at GeneralTransform.TransformBounds() method.
I'm not sure, but is this what you're looking for - rotation of a point in Cartesian coordinate system:
link
You can use Transform.Transform() method on your Point with the same transformations to get a new point to which these transformations were applied.

Resources