All pastes #344581 Raw Edit

Turrets and 3d Rendering

public lua v1 · immutable
·published 2007-02-07 21:15 UTC
rendered paste body
#Global Hooks$GameInit:[--function defssetscreenvars = function()		--scalers and constants for multi res support	local w = gr.getScreenWidth() 	local h = gr.getScreenHeight()	local cx = w/2	local cy = h/2	local wf = 0	local hf = 0	local awh = 0	local hires = false	if w >= 1024 then		wf = w / 1024		hf = h / 768		awh = (wf + hf) / 2		hires = true	else		wf = w / 640		hf = h / 480		awh = ((wf + hf) / 2) * 0.625		hires = false	end	return w,h,cx,cy,wf,hf,awh,hiresendmousejoy = function(deadzone)		--mousejoy function, returns a value from -1 to 1 depending on the mouse's position	local X = io.getMouseX()	local Y = io.getMouseY()	if hires then		X = (X / 511.5) - 1 		Y = (Y / 383.5) - 1 	else		X = (X / 319.5) - 1 		Y = (Y / 239.5) - 1 	end		local Mx = X			--theese are for the menu, -1 to 1 without deadzone applied	local My = Y	local tweak = 1 / (1 - deadzone)	if X < deadzone and X > -deadzone then		X=0	else		if X > 0 then			X = (X - deadzone) * tweak		elseif X < 0 then			X = (X + deadzone) * tweak		end	end	if Y < deadzone and Y > -deadzone then		Y=0	else		if Y > 0 then			Y = (Y - deadzone) * tweak		elseif Y < 0 then			Y = (Y + deadzone) * tweak		end	end	return X, Y, Mx, Myendmjtonormal = function(X,Y)				--converts mousejoy coords into a 3d normal, i think	local Z = ( 1 - math.abs(X) ) * ( 1 - math.abs(Y) )	local N = ba.createVector(X,Y,Z)	N = normalize(N)	return Nendnormalize = function(norm)				--takes a vector and scales it to a length of 1	local len = math.sqrt( (math.abs(norm.x)^2) + (math.abs(norm.y)^2) + (math.abs(norm.z)^2) )	local nf = 1 / len	norm = norm * nf	return normendmultmat = function(mat1,mat2)			--multiply a couple 3x3 matricies	local mat3 = {}	mat3[1] = (mat1[1] * mat2[1]) + (mat1[2] * mat2[4]) + (mat1[3] * mat2[7])	mat3[2] = (mat1[1] * mat2[2]) + (mat1[2] * mat2[5]) + (mat1[3] * mat2[8])	mat3[3] = (mat1[1] * mat2[3]) + (mat1[2] * mat2[6]) + (mat1[3] * mat2[9])	mat3[4] = (mat1[4] * mat2[1]) + (mat1[5] * mat2[4]) + (mat1[6] * mat2[7])	mat3[5] = (mat1[4] * mat2[2]) + (mat1[5] * mat2[5]) + (mat1[6] * mat2[8])	mat3[6] = (mat1[4] * mat2[3]) + (mat1[5] * mat2[6]) + (mat1[6] * mat2[9])	mat3[7] = (mat1[7] * mat2[1]) + (mat1[8] * mat2[4]) + (mat1[9] * mat2[7])	mat3[8] = (mat1[7] * mat2[2]) + (mat1[8] * mat2[5]) + (mat1[9] * mat2[8])	mat3[9] = (mat1[7] * mat2[3]) + (mat1[8] * mat2[6]) + (mat1[9] * mat2[9])	return mat3endmatxvec = function(mat,vec)			--multiply a vextor by a matrix	local x = (mat[1] * vec[1]) + (mat[2] * vec[2]) + (mat[3] * vec[3])	local y = (mat[4] * vec[1]) + (mat[5] * vec[2]) + (mat[6] * vec[3])	local z = (mat[7] * vec[1]) + (mat[8] * vec[2]) + (mat[9] * vec[3])	vec[1],vec[2],vec[3] = x,y,z	return vecendrotnorm = function(norm,angs)			--rotates a normal, in theory	local matx = {1,0,0,0,1,0,0,0,1}	local maty = {1,0,0,0,1,0,0,0,1}	--create 3x3 matrix arrays for x y and z and fill it in with an identity matrix	local matz = {1,0,0,0,1,0,0,0,1}	matx[5] = math.cos(angs[1])	matx[6] = math.sin(angs[1])	matx[8] = -math.sin(angs[1])	matx[9] = math.cos(angs[1])	maty[1] = math.cos(angs[2])	maty[3] = -math.sin(angs[2])		--fill in the nessicary parts of the matrix for each axis	maty[7] = math.sin(angs[2])	maty[9] = math.cos(angs[2])	matz[1] = math.cos(angs[3])	matz[2] = math.sin(angs[3])	matz[4] = -math.sin(angs[3])	matz[5] = math.cos(angs[3])	local mat = multmat(matx,maty)	--multiply our matrixies	mat = multmat(mat,matz)			--to get total matrix	norm = matxvec(mat,norm)		--apply the matrix	return normendcompnorm = function(norm1,norm2)			--measures the radial distance, in radians, between 2 normals	norm1 = normalize(norm1)	norm2 = normalize(norm2)	local ori1 = norm1:getOrientation()	local ori2 = norm2:getOrientation()	local offset = ba.createVector(ori1['p'] - ori2['p'], ori1['h'] - ori2['h'], ori1['b'] - ori2['b'])	return offsetendmousegauge = function(eks,why)			--just a bunch of loops to draw the various hashes for the gauges	gr.setColor(150,255,150,200)	--big hashes	for i=cx-(54*awh), cx+(54*awh), 12*awh do		gr.drawLine(i,cy - (108*awh),i,cy - (96*awh))	end	for i=cy-(54*awh), cy+(54*awh), 12*awh do		gr.drawLine(cx - (108*awh),i,cx - (96*awh),i)	end	gr.setColor(50,255,50,150)	--small hashes	for i=cx-(48*awh), cx+(48*awh), 12*awh do		gr.drawLine(i,cy - (108*awh),i,cy - (100*awh))	end	for i=cy-(48*awh), cy+(48*awh), 12*awh do		gr.drawLine(cx - (108*awh),i,cx - (100*awh),i)	end	gr.setColor(200,200,255,255)	--indicators	gr.drawCircle(7.5*awh,cx+(eks*54*awh),cy-(104*awh))	gr.drawCircle(7.5*awh,cx-(104*awh),cy+(why*54*awh))endprintmatrix = function(ori,v)			--function to help understand the mystyries of the orientation matrix	gr.setColor(200,200,200,200)	gr.drawString(ori[1],150,100+v)	gr.drawString(ori[2],300,100+v)	gr.drawString(ori[3],450,100+v)	gr.drawString(ori.p, 600,100+v)	gr.drawString(ori.p*180/math.pi,800,100+v)	gr.drawString(ori[4],150,115+v)	gr.drawString(ori[5],300,115+v)	gr.drawString(ori[6],450,115+v)	gr.drawString(ori.h, 600,115+v)	gr.drawString(ori.h*180/math.pi,800,115+v)	gr.drawString(ori[7],150,130+v)	gr.drawString(ori[8],300,130+v)	gr.drawString(ori[9],450,130+v)	gr.drawString(ori.b, 600,130+v)	gr.drawString(ori.b*180/math.pi,800,130+v)endgetturretnorm = function(player,tsub,bottom)		--gets turret firing normal	local gnorm = ba.createVector(0,1,0)	if bottom then gnorm = gnorm *-1 end 	--flip inital normals for underside gun	gnorm = tsub.GunOrientation:unrotateVector(gnorm)	--unrotate the up vector to the gun	gnorm = tsub.Orientation:unrotateVector(gnorm)		--then unrotate the gun normal to the base	gnorm = player.Orientation:unrotateVector(gnorm)	--then to the ship	return gnorm							--were doneendgetturretgunpos = function(player,tsub)		--find the turret firing point	local pos = player.Position + player.Orientation:unrotateVector(tsub.Position) + tsub.GunPosition	return posendfireturret = function(plr,wep,pos,dir)		--fires the guns, simple for now	if io.isMouseButtonDown(MOUSE_LEFT_BUTTON) then		mn.createWeapon(wep,dir:getOrientation(),pos,plr,-1)	endendrcturret = function(player,turret,ugun,wep,dis,tvec)		--ship player, str turret name, bool underside gun, weaponclass	if dis == nil then dis = wep.Speed end	local pgun = player[turret]	offset = compnorm(tvec, getturretnorm(player, pgun, true))	local bori = pgun.Orientation	bori.h = bori.h + offset.x	--bori.h = bori.h + X * ba.getFrametime()	pgun.Orientation = bori	local gori = pgun.GunOrientation	gori.p = gori.p + offset.y	--gori.p = gori.p + Y * ba.getFrametime()	--if gori.p < 0 then gori.p = 0 elseif gori.p > math.pi/2 then gori.p = math.pi/2 end	pgun.GunOrientation = gori	local gpt = getturretgunpos(player, pgun)		--gunpoint vector	local aim = getturretnorm(player, pgun, true)	--aim normal	local tgt = (aim*dis) + gpt				--line of fire	fireturret(player,wep,gpt,aim)	return tgtendtransmodel = function(m, t, isforrot)	local points = m[1]	if isforrot then points = points + 1 else points = points + 2 end	for i=2, points, 1 do		local pnt = m[i]		pnt[1] = pnt[1] + t[1]		pnt[2] = pnt[2] + t[2]		pnt[3] = pnt[3] + t[3]		m[i] = pnt	end	return mendrotmodel = function(m, r)	local points = m[1]	local pos = m[points + 2]	m = transmodel(m, {-pos[1],-pos[2],-pos[3]}, true )	for i=2, points + 1, 1 do		m[i] = rotnorm(m[i],r)	end	m = transmodel(m, pos, true)	return mendcamdof = function(vec)	cam = {0,0,0}	vec[1] = vec[1] - cam[1]	vec[2] = vec[2] - cam[2]	vec[3] = vec[3] - cam[3]	local X = cx + ( (vec[1] / vec[3]) * cx )	local Y = cy + (-(vec[2] / vec[3]) * cy  * (w/h) ) 	return X,Yenddrawmodel = function(m)	local points = m[1]	for i=2, points, 1 do		local x1,y1 = camdof( m[i] )		local x2,y2 = camdof( m[i+1] )		gr.drawLine(x1,y1,x2,y2)	endend--function calls and variablesw,h,cx,cy,wf,hf,awh,hires = setscreenvars()tnorm = ba.createVector(0,1,0)amodel = {9, {0,0,-15}, {12,-2,-10}, {0,0,20}, {-12,-2,-10}, {0,0,-15}, {0,0,20}, {0,3,-10}, {0,2,-16}, {0,0,-15}, {0,0,0} }--model starts with the pointcount, lists the points, and tags the position/origin at the end. transmodel(amodel, {0,0,100} )]$Simulation:[player = mn.Ships["Alpha 1"]X,Y,U,V = mousejoy(0.1)tnorm = mjtonormal(U * 0.2,V * 0.2)		--stick a minus on that last 0.2 to make the guns aim in the oither directiontnorm = player.Orientation:unrotateVector(tnorm)if player:isValid() then 	target = player.Target	if target:isValid() then 		dist = player.Position:getDistance(target.Position)	else		dist = nil	end	desrot = player.Physics.RotationalVelocityMax	desrot.x = desrot.x * -Y	--get rid of that minus if you think the mouse works backwards	desrot.y = desrot.y * X	desrot.z = 0	player.Physics.RotationalVelocity = desrot + player.Physics.RotationalVelocityDamping * ba.getFrametime()	--thingy = rcturret(player, "turret01-base", true, tb.WeaponClasses["gatling"],dist, tnorm)	--thingi = rcturret(player, "turret02-base", true, tb.WeaponClasses["gatling"],dist, tnorm) 	gun = tb.WeaponClasses["Maxim"]	--you can change your gun	gpoint1 = player.Position + player.Orientation:unrotateVector(ba.createVector(2.1158,0.4791,1.8430))		--you gan edit theese	gpoint2 = player.Position + player.Orientation:unrotateVector(ba.createVector(-2.1853,0.4791,1.8430))		--gunpoints	fireturret(player, tb.WeaponClasses["Maxim"], gpoint1 , tnorm)	fireturret(player, tb.WeaponClasses["Maxim"], gpoint2 , tnorm)	if dist == nil then dist = gun.Speed end	tnorm = player.Position + (tnorm * dist)end]$HUD:[mousegauge(X, Y)if tnorm:getScreenCoords() and player:isValid() then 	local b,e = tnorm:getScreenCoords()	gr.setColor(0,100,200,150)	gr.drawCircle(9,b*wf,e*hf)end--[[if thingy:getScreenCoords() then 	local b,e = thingy:getScreenCoords()	gr.setColor(0,100,200,150)	gr.drawCircle(9,b*wf,e*hf)endif thingi:getScreenCoords() then 	local b,e = thingi:getScreenCoords()	gr.setColor(0,100,200,150)	gr.drawCircle(9,b*wf,e*hf)end--]]gr.setColor(255,150,150,255)trans = {0,0,0}rot = {0,0,0}if io.isMouseButtonDown(MOUSE_MIDDLE_BUTTON) then 	trans[1], trans[2] = X * 1,Y * -1	if io.isMouseButtonDown(MOUSE_RIGHT_BUTTON) then trans[3] = 1 end	if io.isMouseButtonDown(MOUSE_LEFT_BUTTON) then trans[3] = -1 endelse	rot[1],rot[2] = Y * 0.05, X * -0.05	if io.isMouseButtonDown(MOUSE_RIGHT_BUTTON) then rot[3] = 0.05 end	if io.isMouseButtonDown(MOUSE_LEFT_BUTTON) then rot[3] = -0.05 endendamodel = rotmodel(amodel, rot)amodel = transmodel(amodel, trans)drawmodel(amodel)]#End