Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Best Way to Create a Fired Laser Beam? (3D)

Author
Message
JamesWalker101
16
Years of Service
User Offline
Joined: 14th Apr 2010
Location: England
Posted: 17th Oct 2012 21:59
Hi, I am making a first person 3d game, where I need to make a laser appear from the end of the barrel of the gun at this location,

to the nearest wall, and collide with at at the centre of the screen. Like a gun would shoot a bullet in any FPS game.

How do I go about this?
Thanks,
James
Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 17th Oct 2012 22:27
few ways i could think of:
1) ever seen how lightsaber created for games?
simply there are lots of planes close up to each other with light texture (http://animizemedia.com/Glow11.jpg) and ghost object on
wich makes it something like: o==[]XXXXXXXXXXXXX (think of the X as the planes close to each other, looking on the camera with that light texture)

2) you can create 1 cylinder object, calculate the distance between the gun and the wall and set the size of the celinder.
cast on it a rainbow effect http://www.thegamecreators.com/products/DarkShader/content/darkshader_3_full.jpg

Advanced Updater for your games!
JamesWalker101
16
Years of Service
User Offline
Joined: 14th Apr 2010
Location: England
Posted: 17th Oct 2012 23:07
Thanks, will try that, 2 sounds easier and quicker. I will probably just keep it as one colour or add a texture to it.
But how do I calculate distance between gun and wall? My collision uses a sphere, which moves back to last location if colliding.
And, my main question, how do I rotate it so it points at the wall, where the crosshair is pointing?
Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 18th Oct 2012 00:00
lets do that step by step
- for collision, i recomend you to use Sparky Collision plugin.
- to calculate distance between XYZ and XYZ point you need some kind of formula:
now you got a distance formula where you input 2 different XYZ positions for your distance.
- lets say you know the XYZ position of your gun, right? now we need to find the XYZ position of the wall when it hits, here comes Sparky Collision.
ill make a quick sample for you using only X position:
lets say your gun is at X=10, the wall is at X=20
in sparky coliision, you can check for collision from 10 to 10000 (or untill there will be a hit)
so after you reciving the XYZ position of the "hit" with the wall, you can use the distance formula i gave you earlier to calculate the distance beween it.
use that distance to create the cylinder in same size as the distance. so back to the X-only sample
the distance between gun(10) and wall (20) is 10 (that what distance formula will return to you), you creating the cylinder in length of 10. positioning it at the start point of your gun and "pointing" it to the XYZ of the wall.

i hope you understood what i meant.
working with XYZ is kinda hard as a starter, so i suggest trying to make something like that in 1 dimantion(X) then moving to 2D (XY)
and then to 3D

good luck.

Advanced Updater for your games!
JamesWalker101
16
Years of Service
User Offline
Joined: 14th Apr 2010
Location: England
Posted: 18th Oct 2012 12:11
thanks, will try that.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 18th Oct 2012 13:33
The best way is Billboarding (pretty much what all games use), or more specifically Arbitrary Axis Billboards. But your looking at some complex maths unless you have ezrotate. Unless your ready for a lot of trig/vector/matrix/quaternion maths then I'd stick with Sergey K's methods.
Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 18th Oct 2012 15:18
my methods are quite easy.
the hardest math here is the distance formula (wich already gaven by me)
the others are easy.
just point the beam model into the right direction using:
Point Object command its pretty usefull in this case
(you can get the X,Y,Z rotation out of it, so no need sin/cos/tan at all)

Advanced Updater for your games!
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 18th Oct 2012 17:28
The reason I would invest in billboarding is because it's easy to fake lasers from the camera, but lasers shot at the camera it's not, a cylinder shot at the camera still looks like a cylinder shot at the camera. Though you could use a shader for the laser texture so its always aligned correctly no matter the angle of the camera.
JamesWalker101
16
Years of Service
User Offline
Joined: 14th Apr 2010
Location: England
Posted: 18th Oct 2012 23:17
I will try Sergey's Methods, because when the player gets hit by the laser in my game, it is in the chest, below the camera( eyes)With shaders/textures, it should look ok.
JamesWalker101
16
Years of Service
User Offline
Joined: 14th Apr 2010
Location: England
Posted: 18th Oct 2012 23:21
Just got down to starting this, and I have a [possibly] n00bish question.
For the formula,

how do i get the location of the piece of wall I am aiming at? Because at the moment, it will take 0,0,0 as the x,y,z co-ords. Or could i just keep stretching the cylinder further till it hits a wall? Dunno how I would code that in one cycle of a loop statement either.
Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 18th Oct 2012 23:49
Quote: " Or could i just keep stretching the cylinder further till it hits a wall?"

thats the method

but i would suggest keep checking for hit by with function like that:


this code doesnt compliles.
just try to understand what im doing here.
its pretty easy once you understand what i did.

Advanced Updater for your games!
Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 19th Oct 2012 02:08
Quote: "Unless your ready for a lot of trig/vector/matrix/quaternion maths then I'd stick with Sergey K's methods."


look on my code i posted earlier,
NON of those maths that you posted arent there

Advanced Updater for your games!
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 19th Oct 2012 03:39 Edited at: 19th Oct 2012 03:40
I would tweak this a bit, and make the cylinder a bolt or slight arrow head style with the front being wider then the back end. A more plasma like style.
I would have the "laser bolt" grow out of the origin xyz until a maximum length then continue to advance at the same rapid pace without expanding until it hits the target xyz and shrinks.
Speed would need to be polished to make it look right.

Having an instantaneous/infinite/continuous beam seems sort of dull. Although it may be more realistic. I suppose if there were particle effects...

Point Object really is your best friend in a situation like this.

JamesWalker101
16
Years of Service
User Offline
Joined: 14th Apr 2010
Location: England
Posted: 19th Oct 2012 21:21


Yea, was planning on particle effects later on, but that shouldn't be too difficult to code. Thanks for help Sergey, writing a version of your code now.
Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 20th Oct 2012 02:01
Quote: "Yea, was planning on particle effects later on, but that shouldn't be too difficult to code. Thanks for help Sergey, writing a version of your code now."

k good luck.
the code supposed to be pretty easy once you understand how it works.

Advanced Updater for your games!
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 24th Oct 2012 21:35 Edited at: 24th Oct 2012 21:37
Quote: "NON of those maths that you posted arent there"


To do Arbitrary Axis Billboards you'd need to:

1. Get the planes look vector based of the rotation(r)
- method:

look.x = sin(r.z) * sin(r.x) + cos(r.z) * sin(r.y) * cos(r.x)
look.y = -cos(r.z) * sin(r.x) + sin(r.z) * sin(r.y) * cos(r.x)
look.z = cos(r.y) * cos(r.x)

2. This look vector will be used for are planes up vector, normalize it.
3. Make a temp look vector so we can work out the planes right vector
- simple:

look = camera position - plane position
nomalize look

4. To get the right vector we find the cross product of the up and are temp look vector, normalize the result.
5. Now we work out the final look vector for the plane, cross product of the right and up vectors, normalize again.
6. Set up a transformation matrix (technically you only need a rotation matrix 3x3 cause we don't use the translation when converting)
- like so:
[ right.x, up.x, look.x, planePosition.x ]
[ right.y, up.y, look.y, planePosition.y ]
[ right.z, up.z, look.z, planePosition.z ]
[ 0, 0, 0, 1 ]

7. Convert the rotation matrix to eular and the result is are Arbitrary Axis Billboard orientation.
Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 25th Oct 2012 00:29
i see you did your math homework

but non of them are still requires.. there for Point Object command that you can get most of the "look" positions.

Advanced Updater for your games!
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 25th Oct 2012 19:54 Edited at: 25th Oct 2012 19:55
They are required for arbitrary axis billboards. Since you can't lock an axis with point object nor can specify the axis that points. It simply is impossible with point object.

Unless you can prove me wrong...
Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 25th Oct 2012 20:12
Well you could always use 'point object' and then use 'xrotate object' or 'yrotate object' to lock an axis back to 0. This will work due to DBPro treating the rotation as local rather than global. I've certainly done this before.


Previously TEH_CODERER.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 25th Oct 2012 20:37 Edited at: 25th Oct 2012 20:38
Still wouldn't work for arbitrary axis billboards.

Try rotating a plane x/45 y/45 and have it face the camera around it's z axis... or exactly how arbitrary axis billboards work. Believe me, you can't with point object!
MadBit
VIP Member
Gold Codemaster
17
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 25th Oct 2012 21:29
does it go again?
http://forum.thegamecreators.com/?m=forum_view&t=196176&b=1&msg=2340215#m2340215
(sasuke is right.)

With Computers you can solve Problems that you have never befor.
Share your knowledge. It's a way to achieve immortality. (Tenzin Gyatso)
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 25th Oct 2012 21:42 Edited at: 25th Oct 2012 23:16
MadBit, we've done this before haven't we, only this time I have a refined function to prove my claims.

Here's a lovely functions I quickly whipped up (Ian M's plugin is required):



I don't have mine set up like this, but I made it this way so you can easily see everything! Just note, you only need to set the XY axis for angle!

(I'm thinking I should post this over in Code Snippets)
Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 25th Oct 2012 22:20 Edited at: 25th Oct 2012 22:33
My apologies; I definitely misunderstood the requirement.


Previously TEH_CODERER.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 25th Oct 2012 23:09 Edited at: 25th Oct 2012 23:14
No probs Andrew

I put together a little demo of my function in use (file attached), plus a shader because lack of the way DBP handles transparency and final colour control.



Note: I made one mistake... It should be 'Mouse to look around'
Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 26th Oct 2012 01:46
Well it will teach me not to read properly. Just to make myself clear though, even if this had been doable with the 'point object' command, I was only suggesting it for someone not comfortable with the math, especially when the point of Dark BASIC has always been to keep that kind of thing out of the way. In my own projects I would always do the math myself to ensure efficiency. Besides which, I've been doing a lot of work directly with DirectX in C++ where nothing is handled for you so I've had to get my head around matrix transformations. They're a nightmare initially, or at least I thought so, but they eventually just...'click'.

That is a nice looking demo though!


Previously TEH_CODERER.
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 26th Oct 2012 03:58
If it's a flat one sided mesh, one can't effectively use point object to both position the ends of the object and rotate if for the camera view.

All issues of difficulty aside, I'd still stick with my laser/plasma/particle approach where appropriate.

Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 26th Oct 2012 20:03 Edited at: 26th Oct 2012 20:07
Best thing to do is look at a laser in a current game and try to recreate it. Say Portals 2 lasers, All it is animated textures (shader based) on an arbitrary axis billboard and particles effects.

Portal 2 like lasers are my personal preference.

Quote: "They're a nightmare initially, or at least I thought so, but they eventually just...'click'."


Same. The way I learned them was creating my own math class in DBP so I could actually see the maths that was going on behind the commands.
Ramon156
14
Years of Service
User Offline
Joined: 13th Jul 2011
Location: Netherlands
Posted: 26th Nov 2012 19:48 Edited at: 26th Nov 2012 20:01
Point object does work good enough for me. I'm using Sparkys DLL to calculate the distance to the closest collision point from the player.
Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 26th Nov 2012 23:10
Coincidentally, I had a discussion with my girlfriend recently about Hollywood style "laser blasters", in that really Hollywood style lasers aren't lasers at all, but rather a tracer round, or some kind of pulse.

Hollywood lasers aren't accurate. The bad guys always miss! And the pulse/blast/round doesn't travel at the speed of light, as you can see the round move between point a and point b.

My two cents.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 27th Nov 2012 01:17
Sasuke, that looks really cool. Better than using two divided ghosted planes like I've done in the past.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 27th Nov 2012 20:51 Edited at: 27th Nov 2012 20:55
Thank Phaelax. Thanks for bringing this back up too. Forgot to put the code in Code Snippets. I will make an entry later. Cheers

Quote: "Better than using two divided ghosted planes like I've done in the past."


Also note that arbitrary axis billboards are used to align particles that are aligned by speed. So the particle will point in the direction it's going and always face the camera. This is handy for particles like sparks and extra. Hmm... maybe I should write a basic to advanced particle tutorial.
MadBit
VIP Member
Gold Codemaster
17
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 27th Nov 2012 21:04 Edited at: 27th Nov 2012 21:06
Quote: "Hmm... maybe I should write a basic to advanced particle tutorial. "

I have recently written a small particle engine. Unfortunately not a product for/from TGC.
Here is a short video.


With Computers you can solve Problems that you have never befor.
Share your knowledge. It's a way to achieve immortality. (Tenzin Gyatso)
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 27th Nov 2012 21:28 Edited at: 27th Nov 2012 21:30
Madbit. I really like it plus the other vid's of you testing it (I really like how the rotation one is done with the emitted arrows). Have you thought of key framing the particles basiced on the animations. Like a key curve for the strength of the emitters. That way the attacks would have more impact I think. I can't tell in the video but is but is the rate fixed. When I've done anything with fire, I've always added some randomness so it looks more varied.

Round about March 2011 I started my particle system for my engine Scope. It uses 3 different methods for creating particles. A advance tick the box approach for quick system builds. A flow graph similar to 3ds max's particle flow and key graph which is basically key frame animating a particle system. The reason for all these is most things in my game are particles and I needed great control over everything. About mid December there will be the mega video I've been working on for ages. But in the mean time you can look at what I produced very early on:




MadBit
VIP Member
Gold Codemaster
17
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 28th Nov 2012 11:35 Edited at: 28th Nov 2012 11:35
@sasuke:
thanks

the rotation is a simple sin/cos rotation every particle quad. but if you want more information i suggest to show here. so we can keep this topic clean of oftopic messages.

With Computers you can solve Problems that you have never befor.
Share your knowledge. It's a way to achieve immortality. (Tenzin Gyatso)

Login to post a reply

Server time is: 2026-07-07 09:28:46
Your offset time is: 2026-07-07 09:28:46