Jump to content

Random Rotation


Paul Thomas
 Share

Recommended Posts

Was posting this in a locked topic right when it was locked.

 

Not sure if it'll help anyone but you can randomly set the rotation of the tree's when the object is created, with Lua. The only flaw is that the rotation will always rotate whenever it's loaded. Probably not always wanted but you could easily make a property grid for a "Random Rotation: True/False" if you wanted.

 

function class:CreateObject(model)
 local object = self.super:CreateObject(model)
 object.model:SetRotation(Vec3(0, math.random(0, 359), 0), 1)
end

 

I just found it annoying to change the rotation each time I placed down a tree or any other vegetation. Not as easy as painting the tree's but figured I'd share anyways.

Link to comment
Share on other sites

Edited Again: Thanks to macklebee for adding to this.

Edit: Code revised

 

Here is a script to do just that, with a property grid. Thanks to macklebee for the help with my confusion. My second day of learning Lua after-all, lol.

 

require("scripts/class")

local class = CreateClass(...)

function class:InitDialog(grid)
       --self.super:InitDialog(grid)
       group=grid:AddGroup("Positioning")
       group:AddProperty("Rotation", PROPERTY_VEC3)
       group:AddProperty("randomize", PROPERTY_BOOL, "0", "Random Rotation")
       group:Expand(1)
end

function class:CreateObject(model)
       local object = self.super:CreateObject(model)
object.randomize = "1"
       object.model:SetKey("randomize", "1")

       function object:Reset()
               if object.randomize == "1" then
                       object:randomRotate()
               end
       end

       function object:SetKey(key, value)
               if key == "randomize" then
                       object.randomize = value
                       if value == "1" then
                               object:randomRotate()
                       end
               else
                       return self.super:SetKey(key, value)
               end
               return 1
       end

       function object:GetKey(key, value)
               if key == "randomize" then
                       return object.randomize
               else
                       return self.super:GetKey(key, value)
               end
               return 1
       end

       function object:randomRotate()
               object.model:SetRotation(Vec3(0, math.random(0, 359), 0), 1)
       end

       function object:Free(model)
               self.super:Free()
       end

end

 

I have this with all my tree models. However, you may have to set default values for collision, mass, and so forth.

Link to comment
Share on other sites

The above was edited again because if the object was selected to not randomize it wouldn't save the rotation once you've saved the scene. Multiple attempt to fix this lead to "C stack overflow" errors including when attempting to use; object.model:Fix(). Not exactly sure what the deal with that is.

Link to comment
Share on other sites

This script is virtually useless because the additional added tree's won't save with the scene and it won't add the tree's to scene object list. However, it was just for a challenge to learn Lua, and it works.

 

Place down a tree, right-click -> properties, mark "Create Batch" true, set how many additional tree's ("Batches"), hit Apply and then Ok. Then translate the tree.

 

require("scripts/class")

local class = CreateClass(...)

function class:InitDialog(grid)
       --self.super:InitDialog(grid)

groups = grid:AddGroup("Groups")
groups:AddProperty("batching", PROPERTY_BOOL, "0", "Batch Creation")
groups:AddProperty("batches", PROPERTY_INTEGER, "5", "Batches")
groups:Expand(1)

       group=grid:AddGroup("Positioning")
group:AddProperty("Rotation", PROPERTY_VEC3)
       group:AddProperty("randomize", PROPERTY_BOOL, "0", "Random Rotation")
       group:Expand(1)

end

function class:CreateObject(model)
       local object = self.super:CreateObject(model)
object.batches = 5
object.model:SetKey("batches", "5")
object.randomize = "1"
       object.model:SetKey("randomize", "1")
object.batching = "0"
object.model:SetKey("batching", "0")
object.batched = {}
object.batchCreated = 0

       function object:Reset()
               if object.randomize == "1" then
                       object:randomRotate()
               end
       end

function object:UpdateMatrix()
	if object.batching == "1" then
		if object.batchCreated == 1 then
			for i = 0, tonumber(object.batches) do
					if object.batched[i].xt == 1 then
						object.batched[i].model:SetPosition(Vec3(object.model.position.x + object.batched[i].x, object.model.position.y, 0))
					else
						object.batched[i].model:SetPosition(Vec3(object.model.position.x - object.batched[i].x, object.model.position.y, 0))
					end

					if object.batched[i].zt == 1 then
						object.batched[i].model:SetPosition(Vec3(object.batched[i].model.position.x, object.model.position.y, object.model.position.z + object.batched[i].z))
					else
						object.batched[i].model:SetPosition(Vec3(object.batched[i].model.position.x, object.model.position.y, object.model.position.z - object.batched[i].z))	
					end

					if object.randomize == "1" then
						object.batched[i].model:SetRotation(Vec3(0, math.random(0, 359), 0),1)
					else
						object.batched[i].model.rotation.y = object.model.rotation.y
					end
			end
		else
			object:batch()
		end
	end
end

       function object:SetKey(key, value)
               if key == "randomize" then
                       object.randomize = value
                       if value == "1" then
                               object:randomRotate()
                       end
	elseif key == "batches" then
		object.batches = tonumber(value)
	elseif key == "batching" then
		object.batching = value
               else
                       return self.super:SetKey(key, value)
               end
               return 1
       end

       function object:GetKey(key, value)
               if key == "randomize" then
                       return object.randomize
	elseif key == "batches" then
		return tonumber(object.batches)
	elseif key == "batching" then
		return object.batching
               else
                       return self.super:GetKey(key, value)
               end
               return 1
       end

function object:batch()
	for i = 0, tonumber(object.batches) do
		object.batched[i] = {}
		local bx = {}
		local bz = {}
		bx.t = math.random(1, 2)
		bx.r = math.random(2, tonumber(object.batches))
		bz.t = math.random(1, 2)
		bz.r = math.random(2, tonumber(object.batches))
		object.batched[i].x = bx.r
		object.batched[i].xt = bx.t
		object.batched[i].z = bz.r
		object.batched[i].zt = bz.t
		object.batched[i].model = LoadModel(class.modelreference.path)

			if bx.t == 1 then
				object.batched[i].model:SetRotation(Vec3(object.model.position.x + bx.r, object.model.position.y, 0), 1)
			else
				object.batched[i].model:SetRotation(Vec3(object.model.position.x - bx.r, object.model.position.y, 0), 1)
			end

			if bz.t == 1 then
				object.batched[i].model:SetRotation(Vec3(object.batched[i].model.position.x, object.model.position.y, object.model.position.z + bz.r), 1)
			else
				object.batched[i].model:SetRotation(Vec3(object.batched[i].model.position.x, object.model.position.y, object.model.position.z - bz.r), 1)
			end

	end
	object.batchCreated = 1
end

       function object:randomRotate()
	object.model:SetRotation(Vec3(0, math.random(0, 359), 0), 1)
       end

       function object:Free(model)
               self.super:Free()
	for i = 0, tonumber(object.batches) do
		if object.batched[i] ~= nil then
			FreeEntity(object.batched[i].model)
			object.batched[i] = nil
		end
	end
       end

end

 

Again, was just for a learning experience. If anyone knows of the correct way to force create the same model correctly, please let me know. I couldn't figure out a way.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

 Share

×
×
  • Create New...