Edit2: See below for new problem.
I'm creating a Windows Phone 7 game and have run into some difficulty. What I'm trying to do is use touchscreen coordinates converted to radians to fire an object upwards at an angle. That part works fine.
However, I cannot figure out how to make the object bounce off the side walls. I had it working in an earlier implementation, and it was simply a matter of negating the Y value when colliding with a wall. Unfortunately, it's not so easy this time. When the object hits the wall, it bounces off the wall for a short distance before changing direction towards the wall again.
How can I structure my code so it stops getting pushed into the wall?开发者_运维百科
Thank you.
//Move bubble
public void MoveBubble(Bubble b, double radians)
{
if (b.stop == false)
{
{
//Move bubble 'up'
Y = (float)(speed * Math.Cos(radians));
X = (float)(speed * Math.Sin(radians));
}
b.bubblePosX += X;
b.bubblePosY += Y;
//Problem area
if(b.bubblePosY <= 0)
Y-=Y;
if(b.bubblePosY >= 350)
Y-=Y;
}
//Calculate Angle
private void CalculateAngle(int touchX, int touchY)
{
radians = (Math.Atan2(touchX - bubbleStart.X, touchY - bubbleStart.Y));
}
Edit2: Just wanted to add the latest code. Now the ball is bouncing off the walls perfectly, but I'm having an issue with the direction bool being initialized to true. Sometimes, when firing the object, the angle is reversed. How can set the initial bool to the appropriate true/false value?
//Move bubble
public void MoveBubble(Bubble bubble, Vector2 distance)
{
if (bubble.stop == false)
{
float velocityX = speed * distance.X;
float velocityY = speed * distance.Y;
bubble.X += velocityX;
if (direction == true)
bubble.Y += velocityY;
if (direction == false)
bubble.Y -= velocityY;
if (bubble.Y <= 0)
direction = !direction;
if (bubble.Y >= 350)
direction = !direction;
}
}
You're not negating the Y value, you're just subtracting its value from itself. Perhaps you meant to write:
Y = -Y;
Or am I missing something?
You aren't reversing the direction of the bubble movement. the most suitable solution for changing at that moment the direction would be:
public void MoveBubble(Bubble b, double radians)
{
if (b.stop == false)
{
{
//Move bubble 'up'
Y = (float)(speed * Math.Cos(radians));
X = (float)(speed * Math.Sin(radians));
}
b.bubblePosX += X;
//Problem area
if(b.bubblePosY <= 0 || b.bubblePosY >= 350)
b.bubblePosY -= Y;
else
b.bubblePosY += Y;
}
}
That portion of code would move backwards the bubble just for the next frame, to get the bubble change absolutely the direction of its movement, you would have to recalculate the radians getting the point where the bubble finds a collision and the old touch points.
In resume, in each point of collision, you would have to recalculate the direction is getting your bubble, if you goal this, you can have the whole problem solved, reflect direction in case of collision.
I was looking for some code I had, but I found I've lost it, but I hope mi explanation could help.
See you
You have to change the location and the velocity.
If you have a position of (0, -1) and a velocity of (1, -1) and you can't have negative values, you have to switch the y-value (as to make up for the velocity) to (0, 1) and change the velocity to it's negative (1, 1).
so, loc(ation) and vel(ocity) has to change for instance like:
loc(0, 1), vel(0, -1) // OK
loc(0, 0), vel(0, -1) // switch vel to y=-y; (so -1 becomes 1)
loc(0, -2), vel(1, -3) // switch loc to it's positive value, loc(0, 2)
// and the vel to it's opposite (1, 3)
loc(-1, -3), vel(-2, -4) // loc is set to (1, 3) an vel to (2, 4)
// since both X and Y have been breached
Do this (assuming X and Y are velocity):
if(b.bubblePosY <= 0 && Y < 0)
Y = -Y;
if(b.bubblePosY >= 350 && Y > 0)
Y = -Y;
What is happening in your code is that your bubbles are getting stuck in your wall. The code above only reflects the bubble's direction if it is moving into the wall. Once it is moving out of the wall, it ignores that wall.
Additionally, rather than calculating the angle from the difference in positions and then converting it back to a direction vector, try simply normalizing difference in positions to get a direction vector (a unit vector; length = 1):
Vector2.Normalize(new Vector2(touchX - bubbleStart.X, touchY - bubbleStart.Y))
You should be storing either velocity vector or the direction vector between frames, so that when you reflect your bubble it stays going in that direction until it is reflected again.
精彩评论