Vectors

I want to somehow generate a table of random vectors where the x and y add up to 10, and the z is constant.

To put it into context: i want to add these vectors to the player’s position vector, in order to spawn entities which are 10 units away along a circular path around the player, and have the entities spawn randomly along this circle.

alternatively, if you suggest some simpler way to do this, that would be appreciated too.

well, this is what I have made but it doesn’t work quite right.


function spawnent(ply)
	local distancevar = 300
	local vector = {}
	for x=1,distancevar do
		y = distancevar-x
		vector[x] = y
	end
	
	local randomY = table.Random(vector)
	local randomX = distancevar-randomY
	local setvec = Vector(randomX, randomY,0)
	
	local spawnvec = ply:GetPos() + setvec
	print(spawnvec)
	
	local zombie = ents.Create("npc_zombie")
	zombie:SetPos(spawnvec)
	zombie:Spawn()
end
concommand.Add("spawnent", spawnent)

it just spawns them on a random point on a line, rather than in a circle.
http://img46.imageshack.us/img46/7760/gmflatgrass0001r.jpg

Wrote it up right here in the post, so I have absolutely no idea if this will work. :v:
I think it should though…

[lua]concommand.Add( “spawnent”, function( ply, cmd, args )
local dist = 300 --Would be kinda’ cool to make that random
local pos = ply:GetPos()
local random = math.rad( math.random( 1, 360 ) )
pos.x = pos.x + math.cos( random ) * dist
pos.y = pos.y + math.sin( random ) * dist

local zombie = ents.Create( "npc_zombie" )
zombie:SetPos( pos )
zombie:Spawn()

end )[/lua]

Try this:
[lua]function spawnent(ply, cmd, args)
local dist = 10;
local ang = math.rad(math.random(1, 360));
local x, y = distmath.sin(ang), distmath.cos(ang);
local spawnvec = ply:GetPos() + Vector(x, y, 0);
local zombie = ents.Create(“npc_zombie”)
zombie:SetPos(spawnvec)
zombie:Spawn()
end
concommand.Add(“spawnent”, spawnent)[/lua]

[editline]09:07PM[/editline]

Ninja’d (didn’t refresh) :ninja:

Cool, I’ll try that out, it sure does seem more promising that what I worked out, lol.
And if it doesn’t work, it will have given me some ideas on how to make it work. I didn’t know that you could manipulate a specific axis of a vector like pos.x
That is very useful :stuck_out_tongue:

math.cos and math.sin take angles in radians.

[editline]09:21PM[/editline]

I guess it wouldn’t matter though.

this one works perfectly, thanks!

http://img171.imageshack.us/img171/7190/gmflatgrass0002t.jpg

nice little ring of zombies xD

Whoops, musta removed that by accident when fixing it a bit. Fixed in my first post.