Reputation: 13
First of all, I must say this is a repost. My previous post was marked a duplicate, and in spite of me quite clearly saying in an edit why this was nothing like the other one, it seems nobody would visit it anyway. The question is still perfectly valid and completely unanswered, so please don't mark it a duplicate simply because someone else was also making a game with Pacman. If my method of rectifying this is wrong, please forgive me and show me the correct way to fix such an issue, as I still have very little knowledge of how this site functions.
My previous post is below:
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: 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:
Thank you.
UPDATE (on repost): My best code has not changed since the last update. However, I have given some thought to why it doesn't work, and have produced the following hypothesis: It is working just as horribly on the 90 degree multiples as it is on every other angle. However, the nature of the angles prevents me from seeing that the line is moving. As such, I must guess that the code is flawed because it possesses no way to ignore the movement of Pacman.
UPDATE:
In response to requests for pictures that I have received multiple times, I have this rendering from one of the few angles that VGR's excellent code works in.
As I said, the code works in multiples of 90 degrees, such as the one above, but does not render properly for all other angles. And in response to VGR's comment, they never move when Pacman's x and y change (because they shouldn't have to), but will move in response to Pacman changing angle. Think of the lines as the walls of a Corridor
that always moves to where Pacman needs to go. They'll never move as Pacman goes down the hallway, but as soon as he reaches a dead end, the corridor shifts to fit his new heading. I sincerely hope this helps.
Upvotes: 0
Views: 689
Reputation: 44308
You had the right idea:
Shape originalClip = myBuffer.getClip();
myBuffer.clipRect(0, 40, 1000, 1000 - 40);
double x = pm.getX();
double y = pm.getY();
double angle = Math.atan2(pm.getdy(), pm.getdx());
double dx = PACMAN_RADIUS * Math.sin(angle);
double dy = PACMAN_RADIUS * Math.cos(angle);
double x1 = x - dx;
double x2 = x + dx;
double y1 = y - dy;
double y2 = y + dy;
myBuffer.setColor(Color.BLUE);
myBuffer.drawLine((int) (x1 - 5000 * dy),
(int) (y1 + 5000 * dx),
(int) (x1 + 5000 * dy),
(int) (y1 - 5000 * dx));
myBuffer.drawLine((int) (x2 - 5000 * dy),
(int) (y2 + 5000 * dx),
(int) (x2 + 5000 * dy),
(int) (y2 - 5000 * dx));
myBuffer.setClip(originalClip);
Some notes:
Upvotes: 2