Processing camera frustum() and perspective() rotations - camera

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.

Related

Camera constraints on Verold

There is a problem that multiple users of my model have noticed, namely that when you right click the model (here), the movements are hypersensitive. Orbit and zoom are fine and steady, but pan now more often than not results on the model rapidly shooting off into the distance. I've been playing with the camera controls to no avail and I don't want to simply remove the pan option for the client.
Also, is there any way to transition between cameras without a fade, just a movement of the camera?
Also, Verold not working on Internet Explorer 11... any news?
Thanks
Solved: problem was the focal point (white lined sphere). Had been set off accidentally far off into the distance (can be easily done without noticing and there is no undo). Just brought it back to the object.

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.

Sand Physics for iOS

What is the best way to make sand particles animate in a view?
Essentially, I would like to half fill the iOS device's screen with small sand-like particles, then allow a user to rotate and shake the device to dictate the sand's position.
Assuming I have never done any physics programming before, can anyone recommend a tutorial or show me how it's done?
Thank you,
Query.
UPDATE:
I have now come across this (mine should be 2D though) - how can I bring something similar into my app?
Using spatial indexing for finding the nearest-particles to check for collision and using an integration technique for the transition between force(acceleration)-velocity-position and using only gravity force as an outer-fource would give you your sand-box.
You will need to select a good exclusion-force derived from a particle-potential if you use post-collision detection.
I advise you to use the Truncated Lennar-Jones potential and Verlet-Integrator. Easier than Runge-Kutta's and more precise than Euler's. Because it is used in molecular-dynamics. You dont need to use other forces . Just use exclusion force, gravity and wall forces.
If you have bullets in your simulator, you can use Euler-Integration for them. I think this is acceptable for free-falling but not colliding sand particles. After they close each-other, it would be good to use Verlet or Runge-Kutta.
All i mentioned above assumes your integration step is so big that energy is not conserved and even decreased. If your integration is good enough to conserve energy, you will need to give your particles friction force to make sands slow or you will get your particles exploding everwhere.
If you like to make it on iphone then you have to think of certain optimizations and tricks as iPhone can't really simulate water or sand.
Your trick is that most of your work is to draw scene.
Create scene in Box2D with balls at size 10-20 times bigger then sand particle.
iPhone would be able to simulate it.
Then you should draw 10-20 sand particle per ball.
Every frame you may check if ball collides with other balls or not.
If balls is not colliding then these sand particles are in the air and you should draw them on certain distance one from each other.
If ball collides with other balls then particles should be rendered together
You may also detect margin and render glider sand border on top.

how to make a circular line of sight in cocos2d tile map

I have a problem about cocos2d tile maps.
My aim is to make a circular line of sight that when player moves around, it must only see its around in circle but not see the rest. I have tried many things and successed it in rectangular area but I couldn't succeed it in circular area.
I am waiting for your answers.
Thanks for your help.
If you help me immidiately, I will appreciate.
Use rectangular area and add an alpha mask with round gradient as a child of the player to make the visible part round.
E.g.
with fading
without fading
Edit.
The green layer is the fog. Only a square of it is uncovered ("I have tried many things and successed it in rectangular area"). The red layer is the circular vision map. It covers some of the visible squares and so the user sees uncovered circle.

Rotate sprite opposite of phone rotation with cocos2d?

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];