I want to create a translate animation in relation to other objects on the screen
if(vipUpSet == null) {
Rect contactUsLocation = new Rect();
Rect infoLocation = new Rect();
Rect vipLocation = new Rect();
contactUs.getGlobalVisibleRect(contactUsLocation);
info.getGlobalVisibleRect(infoLocation);
vip.getGlobalVisibleRect(vipLocation);
vipUp = new TranslateAnimation(vipLocation.left, contactUsLocation.right + 20, vipLocation.top, contactUsLocation.top);
vipUpSet = new AnimationSet(true);
vipUpSet.setInterpolator(new LinearInterpolator());
vipUpSet.addAnimation(scaleDown);
vipUpSet.addAnimation(vipUp);
vipUpSet.setDuration(1000);
vipUpSet.setFillAfter(true);
}
I have two buttons at the top of the screen called contactUs and info
I want vip to take off from where it is, and be to the right side of the contactUs button
the current setup, makes the view disappear and fly in from the bottom, I want it to fly from its current state.
How do I do that ?
You are using the wrong deltas.
This should do:
float deltaY = contactUsLocation.top - vipLocation.top;
vipUp = new TranslateAnimation(0.f, 0.f, 0.f, deltaY);
// ... same ...
This code will shift contactUs from its current position up to where it is aligned horizontally with vip (but no horizontal translation).
The initial deltas set to 0.f mean the View starts animating from its current position. The final deltas are the offsets of the translation.
Be also careful that the scaleDown animation don't interfere with the translation.
Related
We are trying to detect collision on a image view that is moving across the screen. The problem is we only want a negative reaction when the bottom or left side of the brickRect collides with the top or right of the ballRect. If the ballRect wants to land on the top of the brickRect then NO negative reaction is desired. Our question is how to write the if statements for these three interactions of the ballRect with the brickRect? We understand how to obtain the top bottom and left values We will post that code not sure it adds to the question
Rect brickRect = new Rect();
brickFOUR.getHitRect(brickRect);
int BRICKbottom = brickRect.bottom;
int BRICKtop = brickRect.top;
int BRICKleft = brickRect.left;
int BRICKright = brickRect.right;
The object here is to have the ballRect jump up and land on the brickRect
If the ballRect jumps and hits the bottom of the brickRect then do what ever
If the ballRect.right intercepts the brickRect.left then do what ever
So how do we turn this logic into code?
We have looked at numerous post but they all deal with over all detection
Assuming that you have a pair of positions or Rect models, for the before and after movement of the ball and that the position increases to the top and right, then you could write the code as follows:
Rect ballBefore = ...
Rect ballAfter = ...
Rect brickRect = new Rect();
brickFOUR.getHitRect(brickRect);
if(ballBefore.top > ballAfter.top
&& ballBefore.top < brickRect.bottom
&& ballAfter.top >= brickRect.bottom) {
//movement in top direction
//and position before is below
//and position after is not
}
if(ballBefore.right > ballAfter.right
&& ballBefore.right < brickRect.left
&& ballAfter.right >= brickRect.left) {
//if movement is in right direction
//and position before is left
//and position after is not
}
I am trying to randomize the position of a few textviews inside a frameview. The textviews will also have a randomized rotation between 0 and 360 degrees. The textViews is not allowed to be on top of eachother which means I need to check for collisions (or at least know which points that are valid/not valid). I do not know how to check for collision between two textviews when they are rotated. I have tried to use Rect intersects but this does not really work because this function only works if there is no rotation to the view.
Here is an example on what i want:
TEXT1 is placed first. When TEXT2 is placed the green border around the TEXT1 and TEXT2 is colliding which means that TEXT2 should not be allowed to be placed there. TEXT3 does however not collide with anything and should be allowed to be placed. So I want to check the collision for the green border and not the blue rectangle. How do I do this?
Edit
To rotate the view I am using View.setRotation(float)
To position the textview I am using setX(float) and setY(float).
I ended up with the following solution where I create 4 points, one for each corner of the textView, which I then rotate at the same angle as the textView. With these points I then create a Path which I am using to create a region.
private Region createRotatedRegion(TextView textView){
Matrix matrix = new Matrix();
matrix.setRotate(textView.getRotation(), textView.getX() + textView.getMeasuredWidth() / 2, textView.getY() + textView.getMeasuredHeight() / 2);
Path path = new Path();
Point LT = rotatePoint(matrix, textView.getX(), textView.getY());
Point RT = rotatePoint(matrix, textView.getX() + textView.getMeasuredWidth(), textView.getY());
Point RB = rotatePoint(matrix, textView.getX() + textView.getMeasuredWidth(), textView.getY() + textView.getMeasuredHeight());
Point LB = rotatePoint(matrix, textView.getX(), textView.getY() + textView.getMeasuredHeight());
path.moveTo(LT.x, LT.y);
path.lineTo(RT.x, RT.y);
path.lineTo(RB.x, RB.y);
path.lineTo(LB.x, LB.y);
Region region = new Region();
region.setPath(path, new Region(0, 0, textViewParent.getWidth(), textViewParent.getHeight()));
return region;
}
private Point rotatePoint(Matrix matrix, float x, float y){
float[] pts = new float[2];
pts[0] = x;
pts[1] = y;
matrix.mapPoints(pts);
return new Point((int)pts[0], (int)pts[1]);
}
When I have two regions which now have the same position and rotation as two textViews I can then use the following code to check for collision:
if (!region1.quickReject(region2) && region1.op(region2, Region.Op.INTERSECT)) {
return true; //There is a collision
}
Probably not the best solution but it gets the job done.
I'm trying to use a simple translation in which I have a button and I want to move it to a position where another button is. The thing is the button just disappears from the screen right away when the translation starts, it doesn't even move.
my code:
Button poli1 = checkPosPoli1();
TranslateAnimation animpoli1 = new TranslateAnimation(checkPosPoli1().getLeft(),checkBtn().getLeft() , checkPosPoli1().getTop(), checkBtn().getTop());
animpoli1.setDuration(10000);
animpoli1.setFillAfter( true );
poli1.startAnimation(animpoli1);
Both checkPosPoli1() and checkBtn return a button and I have printed both their values in console and it's ok.
Take a look at the documentation of TranslateAnimation: http://developer.android.com/reference/android/view/animation/TranslateAnimation.html . The constructor you are using is this one: TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) which means you want the first and third parameters to be 0 if the view is already in its starting position otherwise you are moving the view way off right at the beginning of the animation.
TranslateAnimation animpoli1 = new TranslateAnimation(0, targetX - currentX, 0, targetY - currentY);
if you want it to be relative to the parent, you can use the second constructor:
TranslateAnimation animpoli1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, currentX, targetX, Animation.RELATIVE_TO_PARENT, currentY, targetY);
I'm trying to create a translate animation to an imageView. The animation works but not according to the value I'm putting in the setDuration()
This is the code
/* flipping the images out of the screen */
float outOfScreenX = currentImageLocationX * DRAG_ACTION_SCREEN_BOUNDS_THREASHOLD;
float outOfScreenY = currentImageLocationY * DRAG_ACTION_SCREEN_BOUNDS_THREASHOLD;
translate = new TranslateAnimation(0, outOfScreenX, 0, outOfScreenY);
long velocity = Math.max(Math.abs((int) vTracker.getXVelocity()), Math.abs((int) vTracker.getYVelocity())) / 10;
velocity = (long) Math.min(velocity, 200);
translate.setDuration(velocity);
translate.setFillAfter(true);
mDragView.clearAnimation();
mDragView.startAnimation(translate);
imageStack.removeView(mOriginator);
The value of "velocity" derived from the vTracker, the speed of the user's touch on the view. It's always between 200-600 proximately, but the view moves much faster than this(and also a little flickering but this is another topic).
I'm developing a simple game by andengine.
I have 10 balls which are moving randomly on screen.i'm importing the balls as picture in sprites.if they move at the same coordinate , they pass though their own insides.but i want: if they move at the same coodirnates ,they should change their directions.so they cannot pass through their insides.how can i do that?
private Runnable mStartCircle = new Runnable() {
public void run() {
int i = circleNumber++;
Scene scene = Level1Activity.this.mEngine.getScene();
float startY = -64.0f;
float startX = randomNumber.nextFloat()*(CAMERA_WIDTH-70.0f);
float a= randomNumber.nextFloat()*(CAMERA_WIDTH-70.0f);
circles[i] = new Sprite(startX, startY, textRegCircle[i]);
circles[i].registerEntityModifier(
(IEntityModifier) new SequenceEntityModifier (
new MoveModifier(10.0f, circles[i].getX(), a,
circles[i].getY(),CAMERA_HEIGHT+64.0f)));
}
scene.getLastChild().attachChild(circles[i]);
if (circleNumber < 10){
mHandler.postDelayed(mStartCircle,1000);
}
}
};
Each object(ball) requires a bounding box, or in your case a bounding circle, which is equal to the size of your sprite.
When the game updates and any balls position changes, you have to test for collisions.
Circle to circle collision testing is the simplest type to do.
if distance between (ball1.pos + ball2.pos) is less than (ball1.radius + ball2.radius) = collision.
You then handle the collision by reversing the velocities or calculating new momentums or something. (You also need to move the objects apart so they are no longer colliding)
Just apply a physical connector between balls:
so it will collide and bounce back.
final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0.1f, 0.5f, 0.5f);
final Body ballBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, circles[i],BodyType.DynamicBody, boxFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(circles[i], ballBody, true, true));
this.mScene.attachChild(circles[i]);