# Rendering help for a TESR: a laser beam

## Recommended Posts

I would like to render a thick beam of light. So essentially this should be a quad that is oriented in such a way that it always faces the camera. But it should start at some point in 3D and end at another point. And it should also be perspective correct. I have this code so far:

```    private void drawLine(Coordinate c1, Coordinate c2, float width) {
Tessellator tessellator = Tessellator.instance;

tessellator.setColorRGBA(255, 255, 255, 128);
tessellator.setBrightness(240);

GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);

// Calculate the start point of the laser
float mx1 = c1.getX() + .5f;
float my1 = c1.getY() + .5f;
float mz1 = c1.getZ() + .5f;
Vector start = new Vector(mx1, my1, mz1);

// Calculate the end point of the laser
float mx2 = c2.getX() + .5f;
float my2 = c2.getY() + .5f;
float mz2 = c2.getZ() + .5f;
Vector end = new Vector(mx2, my2, mz2);

// Given the two points above I would like to render a quad that
// always faces the camera. i guess I need to use things like
// this.renderManager.playerViewY

...

Vector p1 = ...;
Vector p2 = ...;
Vector p3 = ...;
Vector p4 = ...;

tessellator.draw();
}

```

So basically I need something for the '...'. Anyone who can help me with the correct formula to get a correct looking laser beam like that?

Thanks!

##### Share on other sites

Hi

What you're trying to find is the plane which is normal (perpendicular) to the line of vision from the player and which also includes the laser beam.  This is a bit like holding a ruler so that the flat face of the ruler is directly facing you so you can read the numbers along the long edge.  You know the vector direction that the long edge of the ruler is facing in, that's the laser beam start and end points, but in order to draw the ruler properly you need to calculate the vector direction of the short edge of the ruler.  (The thickness of the ruler is ignored)

So you have three points:

player eye position at P [xp, yp, zp]

start of beam at S [xs, ys, zs]

end of beam at E [xe, ye, ze]

You need to create the two vectors PS and SE

PS = [xs-xp, ys-yp, zs-zp]

SE = [xe-xs, ye-ys, ze-zs]

then find the cross product of PS and SE (Vec3 class has a crossproduct function) and normalise it (also a method of Vec3)

this gives you a unit length vector that is perpendicular to both the line of sight from the player as well as the laser beam itself.

If you call this vector say "acrossBeam" you draw a quad with the four points:

S plus half beam width * acrossBeam

S minus half beam width * acrossBeam

E plus half beam width * acrossBeam

E minus half beam width * acrossBeam

The larger your value of half beam width, the wider the laser beam will appear.

How much sense this makes to you probably depends on whether you've done much vector math before...  a bit of google will help if necessary

-TGG

##### Share on other sites

Thanks a lot. I think I can manage with that :-)

##### Share on other sites

Ok. Thanks to this I got it working:

Awesomeness

-TGG

##### Share on other sites

• 7 years later...
On 8/7/2015 at 8:24 PM, TheGreyGhost said:

Awesomeness

-TGG

You know how can i do this for 1.18.2? i mean if you updated the code i would like to see it, and modify it for other purposes

## 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.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×

• ### Posts

• This bug always happen whenever i rejoin the world anyone know how to fix this?
• Basically what the title says. Previously I could launch a world however now I can't even make one because of this error (am forced to crash the game to be able to exit)... debug log - https://mclo.gs/AcvL33G latest log - https://mclo.gs/2OMzQYG
• I Solved this. It was a code problem. I re-created a registry models method. Now The method takes in parameters List of items converted to array and registers models for each item separately
• You can look at the vanilla subclasses to get an idea of how to implement your PreparableReloadListener.

• ### Who's Online (See full list)

×

• #### Activity

×
• Create New...