-1

I am trying to make a particular "Pacman Game".
Basically the main objects are: pacman and fruit.

The background image is a picture of a certain map, meaning the game is bound by an exterior frame.
The frame itself is set in pixels which I convert to coordinates (Conversion of lat/lng coordinates to pixels on a given map (with JavaScript)). coordinates have the following parameters:

public Point3D(double x,double y,double z) 
{
    
    _x=x;// latitude
    _y=y;// longtitude
    _z=z;// altitude
}


public boolean isValid_GPS_Point(Point3D p)
{
    return (p.x()<=180&&p.x()>=-180&&p.y()<=90&&p.y()>=-90&&p.z()>=-450);
}

Fruits are static objects that do not move.

All pacmen have a universal speed of 1 meter per second and an eating radius of 1 meter, meaning every fruit in the eating radius of the particular pacman will be eaten and removed from fruit list(The 'Eating Radius' is more like a third dimensional sphere having a 1 meter radius).

Pacmen movement is determined by an algorithm (Algorithm: using a list of fruits and pacmen, we calculate the distance of 2 objects from both lists, taking the minimal distance needed for a specific pacman to travel.

Distance Formula:

  1. Determining the vector by 2 GPS points on map.
  2. Using square function on the vector:
    sqrt(delta(x)^2+delta(y)^2+delta(z)^2)

In terms of Gui:

package gui;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;


public class MainWindow extends JFrame implements MouseListener
{
public BufferedImage myImage;

public MainWindow() 
{
    initGUI();      
    this.addMouseListener(this); 
}

private void initGUI() 
{
    MenuBar menuBar = new MenuBar();
    Menu File = new Menu("File"); 
    Menu Run=new Menu("Run");
    Menu Insert=new Menu("Insert");
    
    MenuItem New=new MenuItem("New");
    MenuItem Open = new MenuItem("Open");
    MenuItem Save=new MenuItem("Save");
    MenuItem start=new MenuItem("start");
    MenuItem stop=new MenuItem("stop");
    MenuItem packman=new MenuItem("packman");
    MenuItem fruit=new MenuItem("fruit");
    
    menuBar.add(File);
    menuBar.add(Run);
    menuBar.add(Insert);
    
    File.add(New);
    File.add(Open);
    File.add(Save);
    Run.add(start);
    Run.add(stop);
    Insert.add(packman);
    Insert.add(fruit);  
    
    this.setMenuBar(menuBar); 
    
    try {
         myImage = ImageIO.read(new File("C:\\Users\\Owner\\Desktop\\Matala3\\Ariel1.png"));//change according to your path
    } catch (IOException e) {
        e.printStackTrace();
    }       
}

int x = -1;
int y = -1;

public void paint(Graphics g)
{
    g.drawImage(myImage, 0, 0, this);

    if(x!=-1 && y!=-1)
    {
        int r = 10;
        x = x - (r / 2);
        y = y - (r / 2);
        g.fillOval(x, y, r, r);

    }
}

@Override
public void mouseClicked(MouseEvent arg) {
    System.out.println("mouse Clicked");
    System.out.println("("+ arg.getX() + "," + arg.getY() +")");
    x = arg.getX();
    y = arg.getY();
    repaint();
}

@Override
public void mouseEntered(MouseEvent arg0) {
    System.out.println("mouse entered");        
}

@Override
public void mouseExited(MouseEvent arg0) {
    System.out.println("mouse exited");
    
}

@Override
public void mousePressed(MouseEvent arg0) {
    System.out.println("mouse Click pressed");  
}

@Override
public void mouseReleased(MouseEvent arg0) {
    System.out.println("mouse Click released"); 
}

}

Running it from this class:

package gui;
import javax.swing.JFrame;

import Geom.Point3D;
import gis.Fruit;


public class Main 
{
public static void main(String[] args)
{
    MainWindow window = new MainWindow();
    window.setVisible(true);
    window.setSize(window.myImage.getWidth(),window.myImage.getHeight());
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
}
}

My main goal is after that after I place my objects(pacmens and fruits) on the map it should somehow look like this: https://media.giphy.com/media/5UDHI0JLxBBElUFuCs/giphy.gif

I'm looking for a general direction for how to make my packmen move like this and not stay stationary.

Community
  • 1
  • 1
user6394019
  • 121
  • 5
  • 1
    TL; DR please post [mcve] . We do not need to know so much about your application to answer a general question like "make an object move by a certain criteria" . See [this answer](https://stackoverflow.com/a/53701618/3992939) for an example of mcve and moving an object based on calculation. – c0der Dec 19 '18 at 05:33

1 Answers1

0

You want to make so that the pac men (?) will chase the fruits in a straight line, right? This is really simple and it should look hilarious if you add more and more features.

You would make a loop trying to find the closest fruit and then make a line using two points: the position of the pacman and the fruit. You then need to subtract the point of the fruit minus the pacman. This is the vector that represents the line the pacman will need to go. Normalize the vector to get a unit-length vector that we can work with. Now, we need to add a tiny fraction of this vector to move the pacman to the fruit. The formula is "

vector * speed * time_delta

", where time_delta is 1 divided by the fps or the actual time difference if you can get it. I normally use the first. This is so in one second, you pacman will have moved at the specified speed. (Keep in mind you specify how many meters a pixel is!)

    // calculate line
    Vector a = packman.getPosition();
    Vector b = fruit.getPosition();
    Vector ab = b.subtract(a).normalize();

    // for each tick!
    packman.addPosition(ab.multiply(speed * 1 / fps));

Reference:

For the eating part you only delete fruits from a list if they are close enough from the pacman. To add fruits you can do the same, add them to a list. You can also make the pacman store the fruit object to simulate a "target lock" or constantly check for the closest fruit.

Hope this has helped you and I would love to see your game!