Rotate sprite opposite of phone rotation with cocos2d? - objective-c

I am trying to rotate a sprite exactly the opposite of phone rotation so that the sprite stays upright relative to the ground, regardless of the position of the phone.
It works for the most part, but the variation in accelerometer readings, no matter how still the phone is, makes the sprite "bouncy". Basically my code is very simple - I just multiply the acceleration reading by -90 whenever I do an accelerometer reading:
_pink.rotation = acceleration.y * (-90);
This works, but even with the phone sitting on a tablet, it bounces back and forth due to inconsistent accelerometer readings. How can I make it smooth? I am aware of KFilteringFactor, which I implemented but it just made the movement slower, so it didnt keep up with the opposite of phone movement. Maybe I was using it wrong.

Try this
float angle = angle-90;
[pink setRotation:angle];

Related

Rotate camera but render sprites so that they appear in their original world positions

TLDR: I want to rotate camera but render sprites in regards to their world position not camera position.
Howdy,
I'm currently using LibGDX and have come across an issue in regards to camera/object rotation.
Say I have my camera with a rotation of 0 and I have an object(sprite) to the left of the camera's center.
i.e.
Camera Normal (0 degrees rotation)
The sprite renders fine when given a standard world coordinate, however once I rotate my camera, that world coordinate differs from the camera's new (x, y) values.
If I then rotate my camera smoothly 90 degrees to the right(clockwise so that the up direction is facing to the right like the picture below), the object(sprite) that used to be on the left should have simulated a left rotation in regards to the camera (the rotation happens via the camera, the sprite just needs to update position) and now be below the camera's center point.
i.e.
Camera Rotated (90 degrees clockwise)
I'm confused as to how I would calculate the sprite's new locations/positions during the smooth rotation.
Cheers,
Solist.
After looking everywhere for a solution to this problem for 3 weeks it was merely a matter of me needing to call the method
batch.setProjectionMatrix(camera.combined);
in order to update the sprites to their new position in regards to the changing camera rotation.
This link here explains how the Projection Matrix works.

Unity2d; How to raise(lift) camera during game mode at a fixed y axis minimum

I may be making this harder than what it really is, but I am also pretty new to developing games. Currently I am making a practice scene to get back used to the unity engine as I have not had time to use it since last summer. My issue is that I can not figure out how to lift the camera in game mode. Notice my photo below, and how much of the "underground" is showing. I want to raise the camera to keep it at the very least a specific y axis value, so that I can make less of the ground visible, and more of the background visible. If I am over complicating this, please also let me know. Thank you
If main camera is still then just lift the camera in scene view you can see changes in game view.
Or if camera moves with respect to player then you have to use a script and attach it to camera and get a reference of player transform in the script and according to the player position change position of the camera. Add an offset value in y component of the camera.

Objects distort if near the canvas sides - three.js

I have a three.js scene with spheres, but when I zoom in a sphere and I move with orbit controls so it is in the side of the canvas (on in the center and deep in) it looses its round shape (do not care about the lines) :
What can I do ?
There will always be some distortion, but it is worse with a large camera field-of-view (fov).
A reasonable value for camera.fov is 40-45 degrees. A smaller value would produce less distortion.
camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
And for reference, camera.fov is the field-of-view in the vertical direction.
three.js r.70
This is expected behaviour of a THREE.PerspectiveCamera. Try it yourself with a digital camera at home.
The only real way to fix it is to use an THREE.OrthographicCamera, but this likely has other issues you do not want to have, such as objects not becoming smaller when they are further away.

Processing camera frustum() and perspective() rotations

How can I make a camera that I can move around and rotate around its own axis in processing 2+?
I have a camera that I can move around in the world space and have some kind of rotation:
frustum(-10,10,-10,10,10,2000);
translate(camX,camY,camZ);//I move around by adding to these values when a button is pressed
rotate(angleX,1,0,0);//same here...
rotate(angleY,0,1,0);
rotate(angleZ,0,0,1);
Bu the problem with this is that the rotation is centered in the scene, meaning that I get very strange rotations when moving further away from the scene's center coordinates. Why does that happen when I have translated before rotating?
Thanks to Nicolás Carlo and his suggestion to watch This Youtube playlist made by Jorge Rodriguez I was able to fix what I almost made right the first time.
All I had to do was really just to do some simple trigonometric calculations to get the forward-vector from the angles I got, and then just add the camera position to that for the centerX,centerY,centerZ values in camera();
Ex. where camPos is the current position 3D-Vector and vecForward is the calculated 3D-forward vector that I needed.
vecForward.x = cos(yaw)*cos(pitch);
vecForward.y = sin(pitch);
vecForward.z = sin(yaw)*cos(pitch);
camera(camPos.x,camPos.y,camPos.z, camPos.x+vecForward.x,camPos.y+vecForward.y,camPos.z+vecForward.z, 0,1,0);
If some of you do not know, Pitch is the up-down angle and Yaw is left-right angle of the camera.
And as a last note here,I highly suggest watching Rodriguez "Math for game developers" that Carlo suggested since every video explains at least one of the most important/oftenly used mathematical solutions at a time to different problems and then giving an example in the end.

How to move the sprite with the movement of background?

I am working on a tilemap based game in cocos2d in which the player moves in four directions and I have used four images for the movement of player for example left,right,top and down. My problem is that when my background map change its position or move to other position then my sprite does not change its position. Can anyone tell me how to move a sprite with the movement of background.
Use a CCNode to contain both the background and the sprites for your players. Instead of moving the background, move that node.
There are a couple of ways to handle tilemap based games, and neither of them are very convenient. One way is to leave your character in the center of the screen at all times and move the background underneath it. If your character moves 'right', you simply slide the background to the left, and vice versa. This will give the illusion that the character moves around the map, when in reality it remains centered. Under this paradigm you must remember to convert all detection / collisions into the world's space, and not just the screen space. If you don't convert everything, then your 'range' of collision / detection is limited to the size of the screen.
The second method is to pan the camera over the world. You still keep the character in the middle of the screen, but it actually moves around in the world, and the camera follows. This makes the most intuitive sense to me because it allows you to view the game world as you see the real world. It is also much easier to deal with collisions because the position of the character and the world 'just work' and don't have to be converted. The downside here is that Cocos2D doesn't make it easy to use CCCamera, and the documentation is a little thin in that respect.
In your particular case, it sounds like you have a CCLayer problem. If your character is inside the layer you are moving, it will indeed remain in the same place relative to the map (as you are describing). Instead, float the character in a different layer on top of the map.
You could use a scrolling Parallex and then add the sprite onto the same layer as the background. They will move together.