NOTICE: To those with the ability to remove the duplicate, please see the third UPDATE.
I'm creating a Pacman themed pong game. To add to the theme, I would like to draw two lines at the angle that Pacman is facing, one on either side of the ball. I am putting everything in a single JPanel, as I have not reached the point in my education that uses components. I attempted to create a corridor object that takes arguments from my Pacman, but I do not know how to do the trig. My frame is 1000 by 1000, with a bar at the top 40 pixels tall for displaying data. My Pacman gives info in degrees, so please account for that in answers. Thank you, and I will do my best to answer any questions.
UPDATE: I've done some work, and I've got some code to share on this topic. It is below:
import java.awt.*;
public class Corridor
{
public Corridor()
{
}
public void drawLines(Graphics myBuffer, BallPacman pm)
{
double x1 = pm.getX();
double y1 = pm.getY() + 150;
double ix = pm.getdx();
double iy = pm.getdy();
double x2 = pm.getX();
double y2 = pm.getY() + 150;
if (iy < 0) {
while (x1 > 0 && y1 > 40) {
x1 -= 0.1;
y1 -= 0.1 * (iy / ix);
}
if (x1 <= 0)
{
y1 += (0 - x1) * (iy / ix);
x1 = 0;
}
else {
x1 += (40 - y1) * (iy / ix);
y1 = 40;
}
while (x2 < 1000 && y2 < 1000) {
x2 += 0.1;
y2 += 0.1 * (iy / ix);
}
if (x2 >= 1000) {
y2 -= (x1 - 1000) * (iy / ix);
x2 = 1000;
}
else {
x2 -= (1000 - y2) * (iy / ix);
y2 = 1000;
}
}
else {
while (x1 > 0 && y1 > 40) {
x1 -= 0.1;
y1 -= 0.1 * -(iy / ix);
}
if (x1 <= 0)
{
y1 += (0 - x1) * -(iy / ix);
x1 = 0;
}
else {
x1 += (40 - y1) * -(iy / ix);
y1 = 40;
}
while (x2 < 1000 && y2 < 1000) {
x2 += 0.1;
y2 += 0.1 * -(iy / ix);
}
if (x2 >= 1000) {
y2 -= (x2 - 1000) * -(iy / ix);
x2 = 1000;
}
else {
x2 -= (y2 - 1000) * -(iy / ix);
y2 = 1000;
}
}
myBuffer.setColor (Color.BLUE);
myBuffer.drawLine ((int)x1, (int)y1, (int)x2, (int)y2);
}
}
Unfortunately, this produces odd results, such as moving and perpendicular lines. I realize it only produces one line, but it is meant as a test. BallPacman has a move method which moves it by dx and dy every time a timer is called. dx and dy may be positive or negative.
UPDATE: I asked a friend of mine, who said that essentially, what I want is to have two lines parallel to the line which Ballpacman follows. Unfortunately, I don't know how to achieve this result.
UPDATE: This is not a duplicate of the other question at all. The other question was about drawing Pacman himself, which I do not need. This question is about how to use trigonometry to find a parallel line to Pacman's direction, and finding teh code draw it. I would like to ask those in the community with the power to remove this question's duplicate status to do so. I would like to stress that I am using a single component, with objects within that know how to draw themselves. While I am making an update, I would like to post some new and additional code: The method I used to get the angle (input dx and dy):
public double getFAngle (double x, double y)
{
return Math.toDegrees(Math.atan2(x, y));
}
A new strategy I attempted to use to get the line (present in the drawLines method):
double angle = pm.getFAngle (pm.getdx(), pm.getdy());
int x1 = (int) (pm.getX() + 5000 * Math.cos (angle * Math.PI / 180));
int x2 = (int) (pm.getX() - 5000 * Math.cos (angle * Math.PI / 180));
int y1 = (int) (pm.getY() + 5000 * Math.sin (angle * Math.PI / 180));
int y2 = (int) (pm.getY() - 5000 * Math.sin (angle * Math.PI / 180));
myBuffer.setColor (Color.BLUE);
myBuffer.drawLine (x1, y1, x2, y2);
This method was an attempt to make a line that would go straight through Pacman's path. My thinking was that if I could do that, I could achieve the walls by simply increasing and decreasing the y points for lines 1 and 2, respectively. It worked seemingly perfectly in every multiple of 90, including 0. However, it worked in a very odd way for all the other angles. The only specific I can provide for this is that it became perpendicular at 45 degrees. In addition, the lines were moving, which was not desirable in the least. This code also compromised my desires to have the endpoints directly on the walls. Allow me to elaborate on the aims of my program:
- Sense the angle and direction of Pacman
- Draw two lines parallel to Pacman's path, so that Pacman will not intercept with them at any point. Preferably, these lines will have endpoints on at least two of the following four lines: x = 0, x = 1000, y = 40, y = 1000;
- Draw the same lines regardless of Pacman's X & Y. In other words, the lines should not change due to any change of Pacman's progression along the path.
- When Pacman's angle changes, the lines will change to reflect that change. It does not matter whether the calculations are run every call, as long as the result is correct.
Thank you.