Math For Pong Game - java

Lets say I have an angle... what would be a reasonable way to go about finding the next point of where the ball would be?
Variables: bSpeed, bAngle, Ball.x, Ball.y
You knwon when you do c^2 = a^2 + b^2... is there a way you could find how long c^2 could be and actually "draw" it out and then use speed to go only part of that... with that find a^2 and b^2 so you can actually have a x and a y to draw the ball...
Thanks ahead of time! (BTW, I don't need code... just reasoning and wisdom)

Your 4 variables are effectively a vector - where the vector is a measure of both direction and magnitude/velocity (i.e. what you've represented as bSpeed and bAngle). Using this representation means that Ball.x and Ball.y simply become the horizontal and vertical components of the vector.
Given a vector called v1 we can calculate the movement in the x and y axis as follows...
xVelocityOfBall = v1.magnitude * cos(v1.angle);
yVelocityOfBall = v1.magnitude * sin(v1.angle);
GPWiki (Games Programming Wiki) is a great resource for anything maths/physics for games development. Here's a handy link to their vector page

delta_x = speed*cos(angle)
delta_y = speed*sin(angle)
new_x = x+delta_x
new_y = y+delta_y
and then you need just change speed and angle of ball in the case of wall strike)

Open up your textbooks on Sin, Cos and Tan since you're using bAngle. Specifically you'll probably be looking Sin for the vertical motion and Cos for the horizontal motion. Depending on where you've defined degree 0 to face.
Also, you could consider caching the horizontal and vertical speeds since Sin and Cos are expensive

You probably will need to consider the physics of the movement that the pong player is moving also. For example, if a player's paddle is speeding to the left as it contacts the ball, the ball will need to speed up wrt to the left direction. This represents transfer of momentum in physics. The general system of equations in the x and y directions will always be:
mass*velocity (in x) = the sum of the mass*velocity of all objects in x
mass*velocity (in y) = the sum of the mass*velocity of all objects in y
generally speaking sine you always have the speed of the ball in x and y all you need to do is determine the masses of both the ball and the paddles (i suppose that's up to you but I suggest making them the same for ease of calculation).
In terms of solving for the angle, it's very simple, you would just make sure the reflection is equal. If the ball is approaching a paddle (or wall) from a 60 degree incident, then the bounce should also be at a 60 degree incident.

First, convert the angle to a vector using the sin and cos functions. This tells you the relative x-speed and y-speed of the ball. Then, to find out how far the ball actually went, multiply these numbers by the ball's speed and time-of-flight. Finally, add to the ball's starting position. This gives you the ball's ending position.
In a pong game, the ball may hit an object, in which case you need to correct for the change in velocity.

Related

Get Coordinates of a Point from Distance

So, I'm doing a Bot Algorithm Game where i have 2 players and 7 balls.
I managed to calculate the distance between each player and each ball, but now i need my players to move to the coordinates of the ball with the shortest distance to the player.
The Players and the Balls are 2 classes with 2 integers, x & y which are their coordinates.
I calculated the distance by using
Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
Now what I need is somehow getting the x and y coordinates of the shortest distance. I thought about reversing the Formula to get the x value and the y value, but i'm not sure it will bring me the desired result.
Do you have any ideas or different approach ideas? I just need some Ideas and Solving approaches, the rest is up to me to do.
Can't post the code cause many of the other methods, classes etc. are not stored locally so i don't have access to the full source code. If you still think you can be a better help with the code, I'll upload it here.
I assume your project has objects representing the players and balls that contain X-Y coordinates and that you want to move towards the nearest ball (rather than teleporting to it).
If the assumptions are correct then what you want is a direction vector to the nearest ball. Each ball should know its X-Y coordinates, you shouldn't need to calculate them - or am I missing the point?
i.e. something like this pseudo-code:
class Player { float x, y };
class Ball { float x, y };
// Determine nearest ball
function distance(Player, Ball) = ... // the code you already posted
Ball nearest = for each ball { calculate distance } and find lowest
// Calculate direction to nearest ball
Vector dir = normalize(nearest.xy - player.xy);
// Move player
Vector move = dir * speed;
player.xy += move;
The nearest.xy - player.xy calculates the direction to the nearest ball and normalize turns it into a unit vector.
There are plenty of tutorials around for simple vector mathematics.

Box2D - Controlled Movement with Physics

I am creating a Java desktop 2D Game using LibGDX.
I need to be able to move objects (controlled by the player with W/A/S/D).
The movement speed is always the same (read out from a field of the moving object).
While they are moving, they should still be affected by physics.
This means that when moving away from a magnet would make you move slower, moving towards it makes you faster and moving past it would cause you to move a curve. (see blue part of image)
Also a single impulse in while moving would knock you away but you keep moving (see red part of image)
You should also be able to change direction or stop, only stopping your own movement, so you will still be affected by physics.
So I need to apply constant forces that will still be accessible (and removable) after the next step.
Can I do this with Box2D?
-If yes, how?
-If no, any other libraries that can do this? I don't really need density and stuff like that, the use cases in the image is pretty much all I need (+ Collision Detection).
*A magnet would be a body constantly pulling other bodies in a certain range to itself
*Kockback would just be a simple impulse or the collision of a projectile with the object.
EDIT: If you know The Binding of Isaac, thats the kinda physics I'm aiming for.
Set the distance where the magnet has his influence:
float magnetRadius = 30;
Set the attractive force of the magnet:
float magnetForce = 400;
Get the position of the player and of the magnet:
Vector2 magnetPos = magnet.getWorldCenter();
Vector2 playerPos = player.getWorldCenter();
Now calculate the distance between the player and the magnet:
Vector2 magnetDist = new Vector2((playerPos.x - magnetPos.x), (playerPos.y - magnetPos.y));
float distance = (float) Math.sqrt((magnetPos.x) * (magnetPos.x) + (magnetPos.y) * (magnetPos.y));
Then, if the player is inside the magnet's radius, you must apply a force to him that depends on the distance of the player from the magnet:
if (distance <= magnetRadius) {
float vecSum = Math.abs(magnetDist.x)+Math.abs(magnetDist.y);
Vector2 force = new Vector2((magnetForce*magnetDist.x * ((1/vecSum)*magnetRadius/distance)), (magnetForce*magnetDist.y * ((1/vecSum)*planetRadius/distance)));
player.applyForceToCenter(force, true);
}

How to change the Midpoint circle algorithm in order to take a Starting angle and ending Angle?

I've looked many places and haven't found any resources (that I understand) that explain how to turn the standard Midpoint circle algorithm (which uses octants to create the whole circle) into only considering a specific 'slice' of the circle. I'm using this to find the tiles within a line of sight radius.
the code I'm using is the basic Wikipedia code of how to implement the algorithm.
I'm using java inside "Processing" to prototype things out.
I'm trying to understand how this algorithm works so I can modify it, but I'm having trouble.
The midpoint algorithm or Bresenham algorithm can be extended with a condition if the point you are rasterizing falls into the range you have specified by the angles (points). To get the range you would have to find the starting and ending point on the circle. This can be easily accomplished with help of polar coordinates. If we have a circle with radius r, angle theta and center C(x0,y0) the point on the circle can be computed as pCircle(x,y) = (x0 + r * cos theta, y0 + r * sin theta). Note that the angle is in radians.

How to apply a force to a sprite?

I'm working with a game where you juggle a ball and to keep the ball in the air you need to apply forces to the ball.
I'm thinking if you touch right under the ball (180 degrees) and the maximum radius the more power you will kick away with the ball. So for an example if you touch the ball at 160 degrees and radius 6 you will be given less power than if you hit the ball at 170 degrees and radius 8,5.
How should I tackle it?
I would start by using several values:
Direction(int). on the right side direction is defined as 0, top 90,left 180,bottom 270; You can use it to describe an angle.
Force(double). a constant value to describe how much force is applied.
Point(int,int) to describe a point on your canvas.
~~~~~~~~~~~~~~~~~~~~~~~~~~
You can then add several useful calculations as:
int Distance(Point,Point): Math.hypot(x1-x2,y1-y2) [This is the source code giving the distance between two points]
The final movement can be done in several ways. I'd probably do it like this:
Per Tick:
Get current force. Add gravity force (9.81 in angle 270)
Per Click:
Take the position of the click and the position of the ball.
Calculate distance.
Calculate angle (Trigonometrics)
Finally calculate the force and add it.
How to add force?
Take angle.
Take "power".
Use more trigonometrics to calculate this.
I hope this helped you a bit. Sry for the format

how to use atan2 in my android game

I am making a side scroller shooting game. I currently have my character shooting horizontally to the right. I would like to get him shoot anywhere on the screen.
I understand that I should use atan2 to figure what angle my bullet will be shot at but I am confuse how to implement it into my game.
My question is how do I call my coordinates of the touch on the screen into atan2? Do I place this in my touch command codes or the class for my projectile. Lastly do I need to do another atan2 for speed?
First you need the vector of your player
P = (a, b)
And the position of your shooting point
T = (x, y)
This then gives us a vector from P to T, (x - a, y - b). By taking
angle = atan2 (y-b, x-a)
From there, it depends how you are implementing your shooting.
To reiterate, all atan2 does is finds the angle, so
do I need to do another atan2 for speed
depends on whether you want to have the projectile moving at a different speed depending on angle. In fact, you only really need the angle if you are rotating your projectile, otherwise you can just move it along the vector (if it is a circle, doesn't matter what way it is facing!)
You don't need any trigonometry and you should not use it, as it is slow, inaccurate and full of sign (+/-) cases to get wrong.
You can probably do everything you need with linear algebra. Use vectors, not angles.

Categories