Shoot in Direction of Crosshairs

Hello all,

I am creating a game where I need a ship to shoot other ships.
So, I created cross-hairs that can move on the screen (up, down, left, right) according to user input, so the user can aim on a certain place.
Now I need I to shoot a cannon from my ship, in the direction that the cross-hair is standing at.

How can I shoot at the place that the cross-hair is pointing at?

Thank you.

There is a problem here, what “place” are you referring to? Going from your eye to the mouse pointer is an imagined ray, that ray projects into 3D space. So the mouse pointer is actually just a point along a ray where the screen happens to be. So the problem is, what other “place” along that ray are you thinking about.

Maybe you mean: I want to project a ray from the eye, through the mouse pointer and whatever object in the 3D-space that ray first intersects (i.e. picking) is the place I want to aim at. Then you can see in this tutorial how to do picking: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_picking

<cite>@M-ASH said:</cite> Maybe you mean: I want to project a ray from the eye, through the mouse pointer and whatever object in the 3D-space that ray first intersects (i.e. picking) is the place I want to aim at.

@jmaasing, yes that is exactly what I mean. Thank you :slight_smile: I have been trying for quite some time now to apply the aiming that was in the “Hello Picking” tutorial. But, the problem is that I am using a chase camera instead of the default camera in the game.

For example, in the tutorial, he defines the ray as so:
[java]Ray ray = new Ray(cam.getLocation(), cam.getDirection());[/java]
So, in this way, he defined the beginning of the ray at [java]cam.getLocation()[/java] , and defined the direction at [java]cam.getDirection()[/java]. And, if I understand correctly, the end of the ray is that place that the crosshair is pointing at in the 3D space.

  • If we assume that the crosshair moves with the chaseCam, how can I get these same parameters using chaseCam?
  • If we assume that the case is as so:
    In the game, I allow the ship to move, until the user wishes to shoot. Therefore, pressing a button that makes that crosshairs appear and the chaseCam no longer moves around the ship, it stays still in a fixed place following the ship . Only the crosshairs move around the screen.
    So I want the beginning of the ray to be the place of the crosshair on screen. And I want the end of the ray to be the place the crosshair is pointing at in the 3D space.
    How can I create this ray by finding the direction the crosshair is pointing at?

Pressed for time, forgive the terse response, but I can give you a hint. I did something similar in a Ludum Dare entry a while ago.
https://code.google.com/p/subterraneanfarmmaniac/
browse the source, there should be some class called something like VillainRay or similar that uses the screen-coordinates to shot a ray. Maybe that will help.

I’ll have a look at it. Thank you jmaasing. :slight_smile:

You don’t need a raycast to check in the direction the coss hairs are, you just need it in the Vector of the camera.

@jrlowe , I’m not sure I understand you completely. If I have a moving crosshair on the screen, and I want to get the direction the crosshair is pointing at in the 3D space so I can detect its collision with another body, how can I do this?

The solution I found, (thanks to jmaasing :slight_smile: ); creates the ray from the cam, in the direction of the cam, to the target-body.

How can I get this to be; creates the ray from the crosshairs, in the direction of the crosshairs, to the target-body?

u might want to be more specific.

Is this cursor actually in 3d space, like 1 unit in front of the ship, and you want to shoot from the ship, in the direction where the cursor is. Or do you want to shoot “straight ahead” but where the cursor is?

There is a tutorial that does exactly that. Essentially you use a ray and the information is stored into a collision result and you can find the intersected geometry, the location and all that.

This is how the game is going until now:

  • The player moves the ship as he wishes.
  • When the player wants to shoot, they press a certain button, which closens the camera to be more closer to the ship (as if a person is standing on the ship), and creates the crosshairs on the screen.
  • Now, the player needs to target an enemy ship. Using this code from the tutorial:
    [java]
    else if (name.equals(“shoot”) && !keyPressed ) {
    // 1. Reset results list.
    CollisionResults results = new CollisionResults();
    // 2. Aim the ray from cam loc to cam direction.
    ray = new Ray(cam.getLocation(), cam.getDirection());
    // 3. Collect intersections between Ray and Shootables in results list.
    shootableEnemyShips.collideWith(ray, results);
    // 4. Print the results.
    System.out.println("----- Collisions? " + results.size() + “-----”);
    for (int i = 0; i < results.size(); i++) {
    // For each hit, we know distance, impact point, name of geometry.
    float dist = results.getCollision(i).getDistance();
    Vector3f pt = results.getCollision(i).getContactPoint();
    String hit = results.getCollision(i).getGeometry().getName();
    System.out.println("* Collision #" + i);
    System.out.println(" You shot " + hit + " at " + pt + “, " + dist + " wu away.”);
    }
    // 5. Use the results (we mark the hit object)
    if (results.size() > 0) {
    // The closest collision point is what was truly hit:
    CollisionResult closest = results.getClosestCollision();
    // Let’s interact - we mark the hit with a red dot.
    mark.setLocalTranslation(closest.getContactPoint());
    rootNode.attachChild(mark);
    Cannon can = new Cannon(assetManager, Ship, rootNode, bulletAppState, ch, cam, ray);
    can.makeCannonBall();
    } else {
    // No hits? Then remove the red mark.
    rootNode.detachChild(mark);
    }
    }[/java]
  • There are two problems with the above code:
  1. If we say that the crosshairs aren’t moving on the screen (they are fixed in the center of the screen), then this code creates the ray from the camera to the targeted ship. In order for this to be, I have to take care of moving the camera into a suitable position to take the target rather than moving the ship into a suitable position. This meaning, that the camera has to be extremely close to the target ship so that it can detect collsion and you can see the red mark on the target ship.
    2) BUT, I want to be able to move the crosshairs on the screen (@wezrule, no the crosshairs are not in the 3D space. They are created using the guiNode, as in the tutorials.). I want to be able to move the crosshairs so that the player isn’t that extremely tightened into a certain place that they have to be. Meaning that they can shoot from far and from near.
  • Now there is something besides these two points, and that is I can’t get the cannon to shoot in the direction of the ray that we created above.

So, the question now is how can I be able to target a cannon towards a ship, by creating a ray (whose origin is the crosshairs that is on the GUI screen)?

@jrlowe, that tutorial is where I got the above code. Thank you. :slight_smile:

<cite>@M-ASH said:</cite> So, the question now is how can I be able to target a cannon towards a ship, by creating a ray (whose origin is the crosshairs that is on the GUI screen)?

I have trouble understanding what this even means in a third person camera.

See, the crosshair “on the screen” is in 2D space. It’s origin is the location of the camera that the screen is representing. So maybe you need to draw or show us a picture of what you really mean.