Jump to content

babylon.js: Drawing a crosshair


realizer
 Share

Recommended Posts

Hi guys,

I am developing a html5 shooter with babylon.js. How can I draw a crosshair?

What I tried:

- crosshair via div, but it disappears in full-screen-mode

- drawing it with the nativ canvas function (canvas.getContext("2d"), moveTo, lineTo, ...), but I get some errors, which make sense, because I'm in webGL mode and not in a 2d  context).

 

Do you have any soultions I could try?

 

Thx :)

Link to comment
Share on other sites

Well a couple of options that might work if it is a fps game (making the assumption that there will be a gun and arm(s) as well)

 

Create a plane with a crosshairs graphic ( a.png file with most of it transparent). Parent the plane to the camera. Adjust scale of the plane and distance from camera to your liking.

 

Create a simple piece of geometry(mesh) in blender (or whatever you use to model ) to represent the cross hair, import it with gun/arms, and parent that to the camera

 

cheers, gryff :)

Link to comment
Share on other sites

Sort of this: 

function CrosshairInFrontOfCamera(crosshair, camera, distanceFromCamera, crosshairType) {        crosshair.parent = camera;        crosshair.position = camera.position.add(new BABYLON.Vector3(0, 0, distanceFromCamera)); // distance should be more than 0, of course        crosshair.cellIndex = crosshairType;}CrossHairInFrontOfCamera(YourCrosshairVarNameHere, YourCameraVarNameHere, AnyDistanceYouLikeHere, ChooseYourCrosshairHere);
I didn't tried it, but it should work just fine :) Good luck, realizer!

Edit:

That's just a thought though, there's always some better ways to do it. Added 'crosshairType' to choose from different crosshairs in a single 'png' file (same way as you use animations for sprites, but pick a single frame of it instead).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...