I need to set phone's bounds as a wall where elements inside can bounce. The physic should be correct. Here an example :
I know that Interpolation allow bounce but I don't know how to create the physics
Check for the collisions with the walls. I'm assuming you're rendering Bitmaps, so the origin of the square we'll say is the top left corner. In that case:
if (x + width >= SCREEN_WIDTH || x <= 0) vx *= -1;
if (y + height >= SCREEN_HEIGHT || y <= 0) vy *= -1;
Where vx and vy are the x and y velocities.
Related
i'm trying to create a wrap around method to make a sprite bounce of each side of the screen. I've got the bottom and right side working but can't seem to figure out why it won't off the top or the left. Any help would be much appreciated. Here is my code:
public void wrapAround(){
wrapAround = true;
//Code to wrap around
if (x < 0) x = x + gameView.getWidth(); //increment x whilst not off screen
if (x >= gameView.getWidth()){ //if gone of the right sides of screen
xSpeed = (xSpeed * -1);
}
if (x <= gameView.getWidth())
{
xSpeed = (xSpeed * -1);
}
if (y < 0) y = y + gameView.getHeight();//increment y whilst not off screen
if (y >= gameView.getHeight()){//if gone of the bottom of screen
ySpeed = (ySpeed * -1);
}
if (y <= gameView.getHeight())
{
ySpeed = (ySpeed * -1);
}
Your question needs a lot of clarification.
if (x < 0) x = x + gameView.getWidth(); //this code executes only when x IS off screen on the left side, which means you want to move it to the right size. No bouncing is implied here.
The following reverses the velocity of the sprite if it is off the screen on the right side, but if the function wrap around is called again right afterwards, the velocity would reverse again making it wiggle back and forth when hitting left or bottom wall.
if (x >= gameView.getWidth()){ //if gone of the right sides of screen
xSpeed = (xSpeed * -1);
}
if (x <= gameView.getWidth())
{
xSpeed = (xSpeed * -1);
}
I have no idea what you're actually trying to achieve, because half your code attempts to wrap an item around, while the other half attempts to make it bounce.
I have a rectangle with known size and position. (flag)
I have to fill this rectangle with 4 other rectangles. (stripes)
Each stripe must have 1/4 of the total width of the flag and his position is near the previous.
I have to draw this stripes with a random angle that goes from 0° to 90°.
0° = Vertical stripes (stripe width = flag width / 4)
90° = Horizontal stripes (stripe width = flag height / 4)
How can I calculate the width of each stripe for other angles?
int stripes = 4;
RectF rect = new RectF(0, 0, 100f, 75f);
float angle = new Random.nextInt(90);
float stripeSize;
if (angle == 0) {
stripeSize = rect.width() / stripes;
} else if (angle == 90) {
stripeSize = rect.height() / stripes;
} else {
stripeSize = ?
}
canvas.save();
canvas.rotate(angle, rect.centerX(), rect.centerY());
float offset = 0;
for (int i = 0; i < stripes; i++) {
if (angle == 0) {
reusableRect.set(offset, rect.top, offset + stripeSize, rect.bottom);
} else if (angle == 90) {
reusableRect.set(rect.left, offset, rect.right, offset + stripeSize);
} else {
reusableRect.set(?, ?, ?, ?);
}
canvas.drawRect(reusableRect, paint);
offset += stripeSize;
}
canvas.restore();
Let's pretend you have one stripe. Depending on the angle, the stripe width is going to be a value between the shorter dimension (the height in your case) and the longer dimension (the width in your case). The formula for the stripe width calculation should look something like this:
height + ((width - height) * ?)
where ? varies between 0 and 1 based on the angle of rotation. To me that sounds like the sine function might be a good candidate: sine(0) = 0 and sine(90) = 1. You can use Math.sin(), but be aware that the argument it takes is in radians, not degrees, so you need to use Math.toRadians() on your angle first. Then just divide by the number of stripes:
double radians = Math.toRadians(angle);
float stripeTotal = height + ((width - height) * Math.sin(radians));
float stripeWidth = stripeTotal / 4; // or however many stripes you have
If it's not perfect, you can adjust the formula. One last point, since these values only need to be calculated once, I would do that separately every time the angle changes (if it ever changes), not inside of onDraw().
I have 4 points making a rectangle. I would like to know if another point in the view is near the border of that rectangle. It is not if the point is IN the rectangle or OUTSIDE of the rectangle. The idea is to know if the point is near (for example 25 pixels) from the rectangle.
What I am making is a rectangle to let the user re-size a view. If the user clicks outside of the rectangle then the changes are saved. All this is easy, but I also want to save changes when the user clicks inside the rectangle.
In this example I would like to detect if the user touches in the gray or pink area, and not in the black area (where probably is touching on the blue dots or around them)
Here is some pseudo code to solve the problem:
float distance = 25.0f; // constant distance
if ( ( point.x < rect.left && point.x > rect.left - distance ) ||
( point.x > rect.right && point.x < rect.right + distance ) ||
( point.y < rect.bottom && point.y > rect.bottom - distance ) ||
( point.y > rect.top && point.y < rect.top + distance ) ) {
// point is in black area
}
else {
// point is in pink/grey area
}
Just define distance to be the size around the rectangle to exclude, and make sure that both the point and rectangle coordinates are in the same coordinate system.
I have an written an Object called Shape which has a Point representing the topLeftCorner and a Dimension with represents its width and height.
To get the topRightCorner I can simply add the width to the topLeftPoint.x. I use to rotate them on a certain degree around their center. The problem after the rotation is, that my intersects(Shape) method fails, because it does not honor the rotation of the Shapes. The rotation will be the same for each Shape. My current implementation looks like this inside my Shape Object:
public boolean intersects(Shape s){
// functions returning a Point of shape s
return intersects(s.topLeft())
|| intersects(s.topRight())
|| intersects(s.bottomLeft())
|| intersects(s.bottomRight())
|| intersects(s.leftCenter())
|| intersects(s.rightCenter())
|| intersects(s.center());
}
public boolean intersects(Point p){
return p.x >= leftX()
&& p.x <= rightX()
&& p.y >= topY()
&& p.y <= bottomY();
}
Basically I need functions like rotatedLeftX() or rotatedTopRight() to work properly. Also for that calculation I think it doesn't matter when the topLeft point before a rotation of ie 90 will turn into topRight...
I already read this and this Question here, but do not understand it fully.
I modified an algorithm from Stackoverflow to do what you are indicating with rectangles for a battleship game I wrote. Here is the code:
private boolean overlaid(int [][][] shps, int curr)
{
for (int i = curr-1; i>=0; --i)
{
// From: http://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other/306332#306332
// if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 &&
// RectA.Y1 < RectB.Y2 && RectA.Y2 > RectB.Y1)
if (shps[curr][0][1] <= shps[i][1][1] &&
shps[curr][1][1] >= shps[i][0][1] &&
shps[curr][0][0] <= shps[i][1][0] &&
shps[curr][1][0] >= shps[i][0][0])
return true;
}
return false;
}
private int [][][] shps = { {{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1}} };
The shps parameter is just a matrix indicating the location of the top-left and bottom-right corners of each rectangle using {x0,y0}, {x1,y1}. For example, shps[curr][0][1] == the current shp's y0. For my purposes, I had to use <= and >=. Also, you have to be mindful of the reverse of y if you are using screen coordinates versus Cartesian coordinates. Also DeMorgan's Law if you want to use NOT overlaid.
I have a solution:
lets assume I want to calculate the rotation (90 degrees) of a Shape with x =1, y=1 (topLeft Point) and a width of 4 and a height of 6 around its center (3, 4) == (x1, y2)
rotatedX = x1 + cos(q) * (x - x1) - sin(q) * (y - y1)
rotatedY = y1 + sin(q) * (x - x1) + cos(q) * (y - y1)
in this case:
rotatedX = 3 + cos(90) * (1 - 3) - sin(90) * (1 - 4)
rotatedY = 4 + sin(90) * (1 - 3) + cos(90) * (1 - 4)
this is for a rotation in a cartesian-plane (where positive rotation values means rotation counter-clockwise)
So if you wanna aply a rotation of 90 degrees CLOCKWISE you just have to multiply rotation with -1;
Question:
How can I taken into account the rectangular shape of the screen to ensure that circles are drawn exactly to the padded boundary?
The following works for a 'round' area, but not a rectangular one...
dx = abs(center.x - place.x);
dy = abs(center.y - place.y);
dh = Math.sqrt((dx * dx) + (dy * dy));
radius = dh - padding;
Halo Design:
If the problem doesn't seem apparent, the following image represents the current approach which i'm using. Depending on where the place is effects how far it protrudes into the screen space.
I think this should work. Not tested.
dx = abs(placeLocationPixels.x - ourLocationPixels.x)
dy = abs(placeLocationPixels.y - ourLocationPixels.y)
ox = dx - ((screenSize.x / 2) - padding);
oy = dy - ((screenSize.y / 2) - padding);
if (ox < 0) ox = 0;
if (oy < 0) oy = 0;
radius = sqrt((ox*ox) + (oy*oy));
edit: this is not in any particular language.
I'm not sure but maybe you can calculate the convex hull, and then circumscribe it into a rectangle, then you will have the scale of your map to ensure that every circle is shown on the screen.
here: you can find information about the convex hull: http://en.wikipedia.org/wiki/Convex_hull