Beams

Blitz3D Forums/Blitz3D Beginners Area/Beams

mrmango(Posted 2005) [#1]
Hi,

I am trying to create a beam based on a cylinder object. I want to be able to create a cylinder object to be created from the camera out to another cube.

Basically I want to use this to create a laser/phaser beam. I can then start playing around effects on this object.

So when I press a mouse button (left), a cylinder is created between camera and a cube that is moving in a circle around the Z axis..

I cannot figure it out and some examples I found looked a little complicate ( simple that I am! )

Cheers

Mango


jfk EO-11110(Posted 2005) [#2]
I would try to
cyl=createcylinder()
fitmesh cyl,-1,0,-1,2,2,2
hideentity cyl
...
while (endofgame=0) and keydown(1)=0
 if mousehit(1)
  showentity cyl
  pointentity cyl,targetmesh
  scaleentity cyl,1,entitydistance(camera,targetmesh),1
 endif
 ...
wend


But there's a problem when there is no target mesh. So you first should do a camerapick. (Entitypickmode for all meshes must be set correctly). If there is a mesh behind the crosshair or so, you can use it as targetmesh. If there is nothing, you may position a dummy pivot at that location and use it as targetmesh.

Please note when you texturemap the cylinder, you may alter the ScaleTexture dynamicly to make it fit the beam lenght.

EDIT:

To limit the max beam range yoiu may use an invisible sphere that will give you the position for a dummy pivot if you didn't hit anything:

helperpivot=createpivot()
s=createsphere()
scaleentity s,100,100,100
flipmesh s
entitypickmode s,2
entityalpha s,0
entityparent s,camera
...
; then somewhere inside the mainloop, right before you point the beam to the target:
 pick=camerapick(camera,graphicwidth()/2,graphicsheight()/2)
 if pick=s ; didn't hit a real mesh
  positionentity helperpivot,pickedx(),pickedy(),pickedz(),1
  targetmesh = helperpivot
 endif



Rook Zimbabwe(Posted 2005) [#3]
Excellent... you could modify this to cast a point onto a target like a lasersight I think...

Let me play with it

RZ


mrmango(Posted 2005) [#4]
Ahh fantastic.. will have a play with this. Many thanks.

Luckily there has to be a target in my game, you must lock on first before firing. Although eventually I want the player to be able to fire manually if their targeting systems are damaged, but that is for the future :-)

Mango


_PJ_(Posted 2005) [#5]
the laser code in the archives does something similar by stretching a primitive cube between two entities.

http://www.blitzbasic.com/codearcs/codearcs.php?code=429


mrmango(Posted 2005) [#6]
Erk.. not sure about this. Tried the first example and get the below screenshot... Any ideas where I went wrong?

Whilst I am here, how do you post code. So if need be it will be easier to understand?



It seems the beam is pointing along the wrong axis? It does turn toward the cube (borg cube), but not on the right axis.

Cheers

Mango


RiverRatt(Posted 2005) [#7]
Did you try rotating the original cylinder?
And the code for posting is {code}your code here...{/code}only with [ instead of }.


_PJ_(Posted 2005) [#8]
Hmm maybe you need a RotateMesh command in there when the 'beam' is created...


Stevie G(Posted 2005) [#9]
Yup, you need the cylinder mesh to be pointing along the z axis initially before you point it as pointentity aligns on the z axis.

Set up the beam entity like this ...

Beam = createcylinder()
Rotatemesh Mesh , 90, 0, 0
Positionmesh Beam,0,0,1
Scalemesh Beam,1,1,.5

Then ...

Pointenity Beam , Target
Scaleentity beam , 1 , 1, entitydistance( Beam , Target )

Note that Scaling on the Y axis is wrong ... should be the z axis.

Stevie


jfk EO-11110(Posted 2005) [#10]
uh, sorry thought the "point-to-axis" of y cylinder is it's y-axis. yes, simply use Rotatemesh cyl,90,0,0 once after creation, then use the scaling with the z-axis, this should work.

This happens when you pseudo-code all the time :) .


mrmango(Posted 2005) [#11]
Ok,

Seems to work better now.

But my beam seems to hit beyond the cube. Not sure what I have done here.

Is there something I have done wrong? Sorry about the lack of comments!

Mango

	;==================================================================
		
	Global graphic_x$=1600
 	Global graphic_y$=900
 	Global graphic_depth$=32
	Global graphic_window$=1
	Global started$
	Global fntarial$=LoadFont("Arial",8,False,False,False)
	Global temp$
	Global screendebug = 1
	Global movement = 0
	Global Collision$
	Global torplive#
	Global wp_id$
	Global appname$="Face Off"
	Global enemy1col=1
	Global torpcol=1
	Global title$ = appname+" - Build "+versionlong+" - Version "+versionshort
	Global specs$ = "Available Memory: "+AvailPhysicalRAM()+"  Total Ram Used: "+TotalPhysicalRAM()+"  CPU Speed: "+CPUSpeed()
		
	AppTitle (title)
	
;	create screen

	SetBuffer BackBuffer()
	graphic_window=screendebug
	Graphics3D graphic_x, graphic_y, graphic_depth, graphic_window
	
;	Creation of standard game variables
	Global ship_power# = 5000
	Global shield# = 100
	Global torp_capacity# = 100
	Global torp_max# = 100
	Global torp_recharge# = 0.25
	Global torp_yield# = 20
	Global phaser_capacity# = 100
	Global phaser_max# = 100
	Global phaser_yield#= 5
	Global phaser_recharge# = 1.25
	
	Global enemy1_shippower#
	Global enemy1_shield# = 1000
	Global enemy1_torp_recharge#
	Global enemy1_phaser_recharge#

;	setup 3d components

	Global tex2 = LoadTexture("beam.bmp",3)

 	Global enemy1=CreateCube()
 	Global tex=LoadTexture( "borg.jpg",1 )
 	EntityTexture enemy1,tex
 	EntityFX enemy1,2
 	PositionEntity enemy1,+4,+1,+4
 	EntityRadius enemy1,1.5
 	EntityPickMode enemy1,2 ; check if pick mode is quicker on rectangle
 	EntityRadius (enemy1,1)
 	EntityType (enemy1, 2)
 	
 	Global shieldsph=CreateSphere()
 	ScaleEntity shieldsph,0.01,0.01,0.01
 	EntityFX shieldsph,1
 	EntityTexture shieldsph, tex2
 	
 	Global torp=CreateSphere(8)
 	EntityRadius (torp,1)
 	EntityType (torp,1)
 	ScaleEntity torp,0.25,0.25,0.25
	HideEntity torp
	
	Global beam=CreateCylinder(8)
	;FitMesh beam,-1,0,-1,2,2,2
	RotateMesh (beam,90,0,0)
	PositionMesh beam,0,0,1
	ScaleEntity beam,0.5,0.5,1
	HideEntity beam
	
	EntityFX beam,1
	EntityTexture beam, tex2
	
	Global camera=CreateCamera()
	PositionEntity camera,0,0,-4
	
	Global light=CreateLight(1)
	PositionEntity light,0,-4,0
	
	Global crosshair=LoadImage ("crosshair.bmp")
	Global conftorp=LoadSound("torp-fired.wav")
	Global torpsnd=LoadSound("torp.wav")
	Global locked=LoadSound("locked.wav")
	Global explosion=LoadSound("explosion.wav")
	Global phasersnd=LoadSound("phaser.wav")
	
	Collisions 1,2,2,1
	
;	Main Loop
	
While Not KeyHit(1)
  
Locate 0,850:Print"System Information:  "+specs
Locate 10,700:Print "Ship Power: "+ship_power
Locate 10,710:Print "Torp Capacity: "+torp_capacity
Locate 10,720:Print "Phaser Capacity: "+phaser_capacity
Locate 10,730:Print "Enemy Shield: "+enemy1_shield
Locate 10,750:Print "Enemy Distance: "+EntityDistance(camera,shieldsph)
  	
  	
  DebugLog("Main loop")
	
	enemymove()
	PositionEntity (shieldsph,EntityX (enemy1),EntityY (enemy1), EntityZ (enemy1))
	locktarget()

	If wp_id = enemy1 And  MouseHit(1) Then
		fire_torp()
	EndIf 
	If torplive = 1 Then
		torp()
	EndIf 
	
	If MouseDown(2) And phaser_capacity => 0
  		PointEntity beam,enemy1
  		ScaleEntity beam,0.10,0.10,EntityDistance(camera,shieldsph)
 		ShowEntity beam
 		phaser_capacity = phaser_capacity - 2
 		PlaySound phasersnd
 	EndIf
	
	If phaser_capacity =< 0 Then HideEntity beam
	If MouseDown(2) = False Then HideEntity beam
	
	If torplive = 1 Then MoveEntity torp, 0, 0, 2
	
	collide()
	energy()
			
	UpdateWorld
	
	
	RenderWorld
		
	DrawImage crosshair,MouseX(),MouseY() ; Draw the image!
	


	Flip

Wend

; FUNCTIONS

Function torp()

	
		If EntityX(torp) > 300 Then HideEntity torp:torplive=0
		If EntityY(torp) > 300 Then HideEntity torp:torplive=0
		If EntityZ(torp) > 300 Then HideEntity torp:torplive=0
		
	;If torplive = 1 Then MoveEntity torp, 0, 0, 2

End Function
Function fire_torp()

	If torp_capacity => torp_max
	
		Locate 10,30:Print"Target Locked"
		ShowEntity torp
		PositionEntity torp,0,0,0
		PointEntity torp, enemy1
		PlaySound(conftorp)
		PlaySound(torpsnd)
		torplive=1
		torp_capacity = torp_capacity - 100
	
	EndIf
	
End Function
Function enemymove()

		TurnEntity enemy1,.0,-0.1,.0
		MoveEntity enemy1,-0.05,0,0

End Function
Function locktarget()

		wp_id=CameraPick ( camera,MouseX(),MouseY())
		Locate 10,10:Print wp_id	
		If wp_id = enemy1 Locate 10,30:Print"Target Locked"
		
		
End Function 
Function collide()
	
	If EntityCollided(enemy1,torpcol)
		Text 30,40,"Hit2"
		HideEntity torp
		torplive=0
		PlaySound(explosion)
		enemy1_shield=enemy1_shield - torp_yield
	EndIf
	
End Function
Function energy()
	
	If enemy1_shield < 0 Then enemy1_shield = 0
	If torp_capacity < 0 Then torp_capacity = 0
	If torp_capacity => torp_max Then torp_capacity = torp_max
	;If phaser_capacity <= Then  phaser_capacity = 0
	If phaser_capacity => phaser_max Then phaser_capacity = phaser_max
	
	torp_capacity =  torp_capacity + torp_recharge
	phaser_capacity = phaser_capacity + phaser_recharge
	
	If torp_capacity =< 100 
		ship_power=ship_power - torp_recharge
	EndIf
		
End Function



Stevie G(Posted 2005) [#12]
Change this line

ScaleEntity beam,0.5,0.5,1

to

ScaleEntity beam,0.5,0.5,.5

Remember the distance it's scaled will be doubled as it's already of length 2 units.


I did mention this in my previous post.

Stevie


mrmango(Posted 2005) [#13]
tried that, that's what I thought the problem was, but the beam still appears through the cube.

Mango


RiverRatt(Posted 2005) [#14]
Could it be that your collision check is failing, and the beam is hiding when it's at > 300?


mrmango(Posted 2005) [#15]
No the torp should be hiding at > 300. No collision has been created yet. I have scaled the cube to a larger size, which covers the beam sticking out the other end, I do think Stevie is right by scaling, just not sure whether that is the solution to scale every enemy I would be firing at.

Mango


DJWoodgate(Posted 2005) [#16]
Your camera is at Z-4 and the beam is at Z=0