I have started to learn game physics and I am trying to move a ball with an angle. But it does not change its angle. Java coordinate system is a little different and I think my problem is there. Here is my code.
This is for calculating x and y speed:
scale_X= Math.sin(angle);
scale_Y=Math.cos(angle);
velosity_X=(speed*scale_X);
velosity_Y=(speed*scale_Y);
This is for moving ball in run() function:
ball.posX =ball.posX+(int)velosity_X;
ball.posY=ball.posY+(int)velosity_Y;
I used (int)velosity_X and (int)velosity_Y because in ball class I draw object
g.drawOval(posX, posX, width, height);
and here g.drawOval requires int. I dont know if it is a problem or not. Also if I use angle 30 it goes +X and +Y but if I use angle 35 it goes -X and -Y. I did not figure out how to work coordinate system in Java.
Math.sin() and Math.cos() expect the angle in radians. You should transform your angles to radians (angle*Math.PI/180).
Use Math#toRadians()
scale_X = Math.sin(Math.toRadians(angle));
scale_Y = Math.cos(Math.toRadians(angle));
Check your types. You probably want everything to be floats because your scale variables are going to be less than one, greater than zero. If they are multiplied by ints, there is a good chance you will end up converting to a 1 or 0 all the time. I'm not completely sure of this, I'd code up a few simple equations to make sure (rather than memorize all the rules) if I were you.
One way to do it is to downcast your floats (or doubles) to int at the last possible moment (when you need to pass the values to a method call). This is a little bit of an overkill but doesn't hurt anything but CPU that you aren't using anyway--and may prevent bugs.
Related
The following problem im working on is for one of my favorite past-times: game development.
Problem:
We're in 3D space. I'm trying to determine if a line between two vectors in said space is passing through a circle; the latter of which consists of: center vector, radius, yaw & pitch.
In order to determine that, my aim is to convert the circle to a plane which can either be infinite or just have the diameter of the circle for all it's sides.
Should the line between the two vectors in fact pass through that plane, i am left with the simple task of determining wether that intersection point is within the radius of the circle, in which case i can return either true or false.
What's already working:
I have my circles set up and the general framework is there. The circles are appearing/rendered in the 3D space exactly as specified, great!
What was already tried:
Copied some github gist codes and tried to make them work for my purposes. I kinda worked, sometimes at least. Unfortunately due to the nature of how the code was written, i had no idea what it was doing and just scrapped all of that.
Researched the topic a lot, too. But due to me not really understanding the language people speak when talking about line/plane intersections, i could have read the answer without recognizing it as such.
Question:
I'm stuck at line intersections. No idea where to go and how it works logically! So, where do i go from here and how can one comprehend all of this?
Note:
I did tag this issue as "java", but i'm not looking for spoon-fed code. It's a logical issue i'm trying to get past. If explained well enough, i will make the code work with trial and error!
Say if your circle is a circle in the XY plane with its centre on (0,0,0) and radius 1. How would you solve that?
You would check the values of X and Y when Z is equal to zero. And X squared plus Y squared would be less than 1 (radius squared) if the line passes through the circle.
In other words, you could transform the 3D coordinates to a simpler reference frame. So I think you need to learn transformation of 3D coordinates, which is really not too hard to do. You need to rotate the 3D space around until the centre vector only has a Z component, and yaw and pitch are zero. And then offset the coordinates so the circle centre is in (0, 0, 0). Then apply the same transformation to the line. You could lastly scale by radius, but to be honest that is not so important since the circle math is easy.
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
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.
Is there a way to iterate through all the points on a line in java 2D?
I've looked through the class documentation and can't see anything that does it built in. If not, would it be better to extend the class and write my own method which calculates the equation of the line and then goes through each point? (Would this actually work as the theoretical equation of a line and the pixels that it actually draws in seem to be slightly different)
For something moving on a line with a constant velocity, its coordinates are an affine function of time:
x(t) = vx*t + x0
y(t) = vy*t + y0
where (vx,vy) is the constant velocity (or speed) vector and (x0,y0) the origin position (at time 0).
I suggest reading some basic introductory course on kinematics
So you probably don't want to "iterate on the line" but simply to move something on the screen, that is to compute its position at every time quantum.
(I've learned such equations at high-school, in France)
It sounds like you are trying to do something along the lines of a bounding box. Essentially, you should have an imaginary box around your sprite. Then just check when the bounding box intersects with the line. When it does, you move your sprite in the opposite direction.
This question on Game Development Stack Exchange should help.
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.