Auto rotate camera change direction when limit - three.js

How to make camera auto rotate right and up, if it reach the limit so it rotate right and down like this demo (http://carvisualizer.plus360degrees.com/threejs/)

If you want to get a smooth up-and-down with easing, you can use a sine wave. The core part of the animation looks like this:
angle += speed;
camera.position.y = centerY + (Math.sin(angle) * waveHeight);
I made a a JSFiddle you can check out. You can play with the centerY, speed, and waveHeight properties to get the feel you want.
The example also has a quick-and-dirty way to spin around an object.

you can just add some conditions in render function..
if(camera.rotation.x <= 100)
camera.rotation.x +=0.01;
else if(camera.rotation.x > 100)
camera.rotation.x -= 0.01;
have a try in your code..

Related

Why is my SpotLight casting a perfect circle?

I am having a hard time figuring out why my three.js SpotLight is behaving the way it is. I am trying to make a flashlight for a game. I am adding the SpotLight to the scene like this:
flashLight = new THREE.SpotLight( 0xffffff, 1 );
scene.add( flashLight );
And then updating the flashlight to move in sync with the player like this:
flashLight.position.copy(player.body.position);
flashLight.target.position.set(flashLight.position.x + player.lookDirection.x,
flashLight.position.y + player.lookDirection.y,
flashLight.position.z + player.lookDirection.z);
flashLight.target.updateMatrixWorld();
For some reason, the flashlight casts a perfect circle on the surfaces that it shines on: Image
The expected behaviour is demonstrated here
Thanks!
In the demo, you can see it takes the camera position but offsets it a little
flashLight.position.copy(camera.position);
flashLight.position.x += 2;
flashLight.position.y -= 3;
flashLight.position.z -= 1;
I feel you are seeing a perfect circle, because you're viewing the beam from the same angle as its source.

Rotate a Sprite around another Sprite -libGDX-

video game link
I'm trying to make a game (see link above) , and I need to have the stick rotate around himself to maintain the orientation face to center of the circle.
this is how I declare the Sprite, and how I move it around the circle:
declaration:
line = new Sprite(new Texture(Gdx.files.internal("drawable/blockLine.png")));
line.setSize(140, 20);
lineX = Gdx.graphics.getWidth()/2 - line.getWidth()/2;
lineY = (Gdx.graphics.getHeight()/2 - line.getHeight()/2) + circle.getHeight()/2;
movement:
Point point = rotatePoint(new Point(lineX, lineY), new Point(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2), angle+= Gdx.graphics.getDeltaTime() * lineSpeed);
line.setPosition(point.x, point.y);
rotatePoint function:
Point rotatePoint(Point point, Point center, double angle){
angle = (angle ) * (Math.PI/180); // Convert to radians
float rotatedX = (int) (Math.cos(angle) * (point.x - center.x) - Math.sin(angle) * (point.y-center.y) + center.x);
float rotatedY = (int) (Math.sin(angle) * (point.x - center.x) + Math.cos(angle) * (point.y - center.y) + center.y);
return new Point(rotatedX,rotatedY);
}
Any sugestions ?
I can't test right now but I think the rotation of the line should simply be:
Math.atan2(rotatedPoint.getOriginX() - middlePoint.getOriginX(), rotatedPoint.getOriginY() - middlePoint.getOriginY()));
Then you'll have to adjust rad to degrees or whatever you'll use. Tell me if it doesn't work!
I would take a different approach, I just created a method that places n Buttons around a click on the screen. I am using something that looks like this:
float rotation; // in degree's
float distance; //Distance from origin (radius of circle).
vector2 originOfRotation; //Center of circle
vector2 originOfSprite; //Origin of rotation sprite we are calculating
Vector2 direction = new vector2(0, 1); //pointing up
//rotate the direction
direction.rotate(rotation);
// add distance based of the direction. Warning: originOfRotation will change because of chaining method.
// use originOfRotation.cpy() if you do not want to init each frame
originOfSprite = originOfRotation.add(direction.scl(distance));
Now you have the position of your sprite. You need to increment rotation by x each frame to have it rotate. If you want the orientation of the sprite to change you can use the direction vector, probably rotated by 180 again. Efficiency wise I'm not sure what the difference would be.

Move CCSprite around another CCSprite Circle Shape

What I am trying to accomplish is this:
We have CCSprite Circle A and CCSprite Circle B.
Move Circle B around Circle A. I already tried to create a CCNode and attach the circle B to it. In this case it works perfectly but the position is constant also. I need to move the circle around A and update the position. I will have more objects on the screen and I will check if B intersect some other objects, but for that case I need to update the position while rotating. Much appreciate your help guys. I am using Cocos2D v3.0
Maybe something like this? Put this in your update method. I used this code in my box2d project for orbit. Change it for your needs.
b2Vec2 center = bodyA->GetPosition();
int smoothness = 1000;
int radius = 100;
for (int i = 0; i < smoothness; i++) {
float angle = (i / smoothness) * 360 * DEGTORAD;
b2Vec2 pos( sinf(angle), cosf(angle));
b2Vec2 newposition = center + radius * pos;
bodyB->SetTransform(newposition, bodyB->GetAngle());
}
Put the anchor point of B in the position of the anchor point of A and then rotate the B by 360 in animation.

Rotate an image around a specific point

Is it possible to rotate an image around a specific point, rather than the center? I see options for RVG's, but not for Magick::Image's :/
http://www.simplesystems.org/RMagick/doc/rvgxform.html#rotate <- RVG pivot point
angle -= angleSpeed * delta;
image.x = target.x + sin(angle) * distanceFromTarget;
image.y = target.y + cos(angle) * distanceFromTarget;
you could try changing the anchor point of the image to where you want it to rotate around, instead of the center

howto generate a smooth movement in xna for wp7?

i want to create a game and addes a image to my game, now i want it to move down smoothly. i have a code like this:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
pos.Y = pos.Y + 1;
base.Update(gameTime);
}
the movement works but it dont looks smooth, it looks like it jiggle. pos is a vector2 for the position in the image.
how to make it more smooth?
If you want movement to be smooth without adding a physics library you just have to factor in gameTime to your position update.
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
pos.Y = pos.Y * 100 * (float)gameTime.ElapsedGameTime.TotalSeconds;
base.Update(gameTime);
}
I don't have access to XNA + visual studio right now, but the changes I made should give you an idea of what to try out. Keep in mind the Update call happens multiple times a second so the elapsed time will be a small number so then you have to multiply it by a larger "movement" value in this case I put 100. Tweak 100 until you see the movement speed you desire.
Beanish is right, you should multiply by GameTime if you want smoothness. Physics is an overkill if you only want your animation to look smooth.
The best way I've found to do animation is by using position interpolation, for this to work you have to know the initial (you already know this) and final position of the image.
If you want to move from A to B in, say, 2 seconds, you can use the following code.
Vector2 a = new Vector2(0, 0);
Vector2 b = new Vector2(0, 100);
float elapsedTime = 0;
float duration = 2.0;
public override void Update(GameTime gameTime)
{
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
elapsedTime += dt;
if (elapsedTime > 1)
elapsedTime = 1;
float param = elapsedTime / duration;
pos = Vector2.Lerp(a, b, param);
}
The best thing about using this approach is that you can now use "easing" to make you animation look really really nice.
To do this just add a Power operation to the interpolator parameter:
pos = Vector2.Lerp(a, b, (float)Math.Pow(param /2.0, 0.5));
This will make you image slow down as it arrives to B. You can play with the exponent value (0.5) to get different results, try 2.0 for example.
Another important thing is that your image will always stop at B. If you use the Euler integration approach (your approach, adding a velocity each frame) you might have some trouble making the image stop at the right position (aka B) and it gets even worse when using 2 or 3 dimesions.
To know more about easing, check Robert Penner's Easing Equations.
First I can tell you what the problem isn't. You don't need a physics engine to have smooth movement. And changing the Update to include the ElapsedGameTime will not make a lick of difference for the smoothness (assuming you haven't changed the default of IsFixedTimestep to false). When there is a fixed timestep, ElapsedGameTime will always have the same value, it will not vary.
I don't how much you are doing in your code, but if it's too much, XNA will start skipping the Draw portion of your code, and this can definitely cause jerkiness. One way to check: in your Update method, test the value of IsRunningSlowly. Whenever it is true, XNA will skip some Draw calls.
If you are not doing anything complicated, then the culprit may be the refresh rate of your monitor. If it is set to anything other than 60Hz, you will have jerkiness. You could fix this by changing your monitor's refresh rate. Alternatively you can change the value of TargetElapsedTime to match your monitor's rate.
You should consider adding to your game a library for handling physics, as for example FarseerPhysics. By calculating the position in a per time base with physics rules applied your movements will be smooth and natural.

Resources