Why is my JavaFX PerspectiveCamera Rotating Around a Seemingly Arbitrary Point? - javafx

I am working on a first-person game, and right now I am building a game engine using JavaFX. I am using the PerspectiveCamera as my player character. However, when I use the default rotation method in JavaFX to rotate my camera, it ends up rotating around some point, moving it far and wide. What I want is for the camera to simply rotate on the spot, and stay in place.
Here is my code:
camera.setRotationAxis(new Point3D(camera.getLayoutX() + (Toolkit.getDefaultToolkit().getScreenSize().width/2), camera.getTranslateZ(), 0));
if (event.getCode() == KeyCode.RIGHT){
camera.getTransforms().add(new Rotate(5, camera.getTranslateX(), camera.getTranslateY(), camera.getTranslateZ(), Rotate.Y_AXIS));
camrot += 5;
if (camrot > 360) {
camrot -= 360;
}
System.out.println(camrot);;
}else if (event.getCode() == KeyCode.LEFT){
camera.getTransforms().add(new Rotate(-5, camera.getTranslateX(), camera.getTranslateY(), camera.getTranslateZ(), Rotate.Y_AXIS));
camrot -= 5;
if (camrot < 0) {
camrot += 360;
}
System.out.println(camrot);
}
Does anyone know what might be wrong here? Maybe something to do with the rotation axis? Not sure, somebody else told me what to put there.

Related

Zooming whole scen in Flash

I've made a interactive map in Adobe Flash Profesional CS6 and i was wondering if there is a way to zoom in and out. Yes i know convert map in to a symbol and do Mouseevent. MOUSEWHEEL etc...... But this doesnt make the whole scene zoom in. So is there any way to do that? Thank you for answering guys.
so I've tried this:
stage.addEventListener(MouseEvent.MOUSE_WHEEL, MouseZoom) function MouseZoom(e:MouseEvent):void{
if (e.delta > 0)
{
trace(e.delta);
Lake_mc.width += 20
Lake_mc.height += 20
}
if (e.delta < 0)
{
trace(e.delta);
Lake_mc.width -= 20;
Lake_mc.height -= 20;
}}

QGraphicsItemAnimation: How to indicate rotation point using setRotationAt

When I use QGraphicsItemAnimation::setRotationAt() function to rotate a QGraphicsItem for an animation, the original point is always left top point of the item.
So I tried QGraphicsItem::setTransformOriginPoint() to make the rotation point to be at right top. But it still doesn't work. Is there any way to rotate the item itself at the right top?
for (int i = 0; i < 1000; ++i){
mAnimation->setTranslationAt(i / 1000.0, size.w, 0);
mAnimation->setRotationAt(i / 1000.0, qreal(i / 1000.0 * angle));
mAnimation->setTranslationAt(i / 1000.0, -size.w, 0);
}
Here is the answer i found, when set frames of the item, need to translate the original point before rotation, and translate it back.

Rotate css3 bug

I'm making a 3d cube based on http://www.paulrhayes.com/experiments/cube-3d/
but mine react on mouse event and touch event on mobile.
I introduce RotateZ to evitate the strange behaviour of the original Hayes cube.
But when if i try to go left -> down and left again it bug.
-webkit-transform:rotateX(-90deg) rotateY(-90deg) rotateZ(0deg);
RotateX and Z make move cube on the same axis I don't know why.
Does it a bug or there is a solution ?
here's is a detailled jsfiddle of my problem :
http://jsfiddle.net/uq2cr/6/
Thank's for your answers !
Problem
This is because when you move the mouse up/down, whilst moving left/right and clicking it will reside to the up/down action.
Current Strucutre
I benchmarked the update of x co-ords.
For both left and right directions, x 0, doesn't seem relative.
The xAngle, yAngle, zAngle increment, but do not reset to 0.
Suggestion
I suggest making some changes to the structure of cubePointerMove(). Make each direction their own stack. That way, there will be no forthcoming problems with the 4 directions.
The way it's set out now, is with duplicate up/down actions in the left/right blocks.
Current
// Left
if( xMove < 0) {
// Up
if( yMove < 0 ) {
if(xMove < yMove){
}
}
// Down
else {
}
}
// Right
else
{
}
Revised
// Up
if( yMove < 0 ) {
}
// Down
else {
}
// Left
if( xMove < 0) {
}
// Right
else {
}

Image Rotate 3D in Flex, but display another image on back of this image?

i want to rotate 3D an Image called img1 in Flex. I want to rotate it around y axis 180 degree. I can do this by using 3D effect already built in Flex but i want to do a bit more different.
I want during rotating, there's another image called img2 appear on back of img1 (in default case, the image appear on the back is img1) and when rotating finish, the image will be img2.
How can i do this ?
Thank you.
If you need no perspective effect, it's quite easy to do. A rough implementation (not tested!):
// Event.ENTER_FRAME event listener
void on_enter_frame(event:Event):void
{
// m_angle is a member of the class/flex component where on_enter_frame is declared
// ANGLE_DELTA is just a constant
m_angle += ANGLE_DELTA;
// Angle clamping to the range [0, PI * 2)
m_angle %= Math.PI * 2;
if (m_angle < 0)
m_angle += Math.PI * 2;
// If we currently look at the front side...
if (m_angle < Math.PI)
{
img1.visible = true;
img2.visible = false;
img1.scaleX = Math.cos(m_angle);
}
else
{
img1.visible = false;
img2.visible = true;
// If you omit negation, the back-side image will be mirrored
img2.scaleX = -Math.cos(m_angle);
}
}
So every frame we increase the rotation angle, clamp it to the range [0, PI * 2). Then depending on the value of the rotation angle, we hide/show the pair of your images, and then perform x-scaling of the visible image.
Thank you, now i found a solution. Please check it here, it's very easy to do.
http://forums.adobe.com/thread/921258

Curve Sprite Movement COCOS2D

I'm trying with no luck to move a sprite position in an simetric curve way with the touch drag. So far I use the following code with no luck
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event
{
lastTouchLocation = [MultiLayer locationFromTouch:touch];
return YES;
}
-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
CGPoint currentTouchLocation = [MultiLayer locationFromTouch:touch];
moveTo = ccpSub(lastTouchLocation, currentTouchLocation);
lastTouchLocation = currentTouchLocation;
}
-(void) update:(ccTime)delta
{
CCSprite* sprite = [sprites objectAtIndex:1];
if (moveTo.x != 0){
float X = sprite.position.x + moveTo.x;
float Y = sprite.position.y + (pow(X,2));
sprite.position = CGPointMake(X, Y);
}
}
The curve way i'm trying to simulate is in the form y=x^2. Is this posible?
Thanks in advance!
Yes it's possible. You can do it manually as your are trying, but it's better to use a built-in solution. There is a CCJumpTo and CCJumpBy actions in cocos2d engine. They are created for parabolic moves. Use them

Resources