[HELP] How can I set rotation dynamically?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Edsploration
Posts: 104
Joined: Wed Sep 19, 2012 4:32 pm

[HELP] How can I set rotation dynamically?

Post by Edsploration »

I've been trying to figure out how to set an object's rotation with in-game code. Like having a gauge rotate based on # of monsters killed. Or a globe which snaps to different positions dynamically.

Things that don't work well:
  • Items dropped in the game just have a 'facing' property, and a random rotation is assigned +/- 10 degrees or so. So that won't do.
  • Alcoves work great for a single position. But I'd have to create hundreds or thousands to choose from if I wanted to be able to set the rotation on-the-fly to any angle.
  • Custom Item assets can have specific rotations when "thrown" with the shootProjectile function. But again, countless objects would have to be defined for each possible desired angle.
So basically, I'm looking into modeling and animation now, which I am still a novice at. Is it possible to set each possible angle to a (static) "animation"? Or is there some other way to do this?

I may end up using the alcove or shootProjectile methods. Although I would need 900 copies or so of the same objects. It seems like someone out there knows a better way to do this.
Open Project -> Community FrankenDungeon: viewtopic.php?f=14&t=4276
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: [HELP] How can I set rotation dynamically?

Post by Komag »

Yeah, I would love this too, but I suspect animation is the only way to go. I dunno, maybe there are a few tweaks under the hood that the devs could do for the next update, whenever that is ;)
Finished Dungeons - complete mods to play
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: [HELP] How can I set rotation dynamically?

Post by Batty »

You can rotate & translate objects in the GMT so maybe create 30 or so differently positioned clones of one object then you can spawn/destroy to create movement.
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: [HELP] How can I set rotation dynamically?

Post by Komag »

good idea, although its sorta the same situation, although perhaps an easier implementation (although all those model files might start to add up!)
Finished Dungeons - complete mods to play
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: [HELP] How can I set rotation dynamically?

Post by Batty »

Komag wrote:good idea, although its sorta the same situation, although perhaps an easier implementation (although all those model files might start to add up!)
It is similar but anything is better than 1,000 alcoves :lol: (including a hudPrint that says "ok, rotate your monitor now!")
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: [HELP] How can I set rotation dynamically?

Post by Komag »

...and I realize now that I typed way too many "althoughs"! :roll:
Finished Dungeons - complete mods to play
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: [HELP] How can I set rotation dynamically?

Post by JohnWordsworth »

Have just spent about 20 minutes trying to come up with a solution for this in the Grimrock editor, but nothing that really works. However, I assume that when the game passes through the lua scripts to build the entities, it's actually running Lua, so you could presumably do something like this to define 360 alcoves, each with it's own anchor rotation...
SpoilerShow

Code: Select all

for i=1,360 do
  defineObject{ ... }
end
I'm dubious about this as a solution though, as I don't know how the engine would take having thousands of defined objects sitting there in the game's object list. It would be cool though if it were possible just to update the anchorRotation value of alcoves while the game is running.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: [HELP] How can I set rotation dynamically?

Post by Komag »

Holy crap, that WORKS! I tested with my custom note items, with this code in my objects.lua:

for i=1,20 do
cloneObject{
name = "note_entity_"..i,
baseObject = "script_entity",
editorIcon = 32,
}
end

and when I opened the dungeon in the editor there are now 20 note entities to choose from!

So it would be possible to define 3600 alcoves all with a tiny rotation, then set a script to fast-as-lightning spawn/destroy each successive alcove pre-filled with the object. No idea the effect on performance or memory usage or what, but it should theoretically work!

EDIT - worked with 1200 notes
EDIT - 120,000 (no typo) works, but has brought the editor to an unbearable crawl
EDIT - 3600 works and is workable, but makes the editor a bit sluggish to do things
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: [HELP] How can I set rotation dynamically?

Post by Komag »

IT WORKS!

I can't believe it, it works.

But if you have too much, it taxes the system, so there is some judgement or moderation.

But it's easy to set up!

Here is my sample code:
objects.lua:

Code: Select all

for i=1,360 do
defineObject{
	name = "machine_socket_west_"..i,
	class = "Alcove",
	anchorPos = vec(0.08, 1.765, 0.5),
	anchorRotation = vec(90, 0, i),
	targetPos = vec(0.08, 1.765, 0.5),
	targetSize = vec(0.3, 0.3, 0.3),
	placement = "wall",
	onInsertItem = function(self, item)
		return item.name == "machine_part_west_not" and self:getItemCount() == 0
	end,
	editorIcon = 92,
}
end
In the dungeon:

Code: Select all

gearTick = 1
function gearAnim()
  if gearTick < 361 then
     if findEntity("gearSock") ~= nil then
        for i in gearSock:containedItems() do
            i:destroy()
        end
        gearSock:destroy()
     end
     spawn("machine_socket_west_"..gearTick, 1, 2, 24, 1, "gearSock")
      :addItem(spawn("machine_part_west_not"))
  end
  if gearTick == 360 then
     gearTick = 0
  end
  gearTick = gearTick + 1
end
This is using a gear object and socket set up for my custom Cube, but it could work for anything I suppose.
I also just have a pressure plate linked to activate a timer set to 0.01 that ticks the script forever (once it gets to 360, it does the 360, then sets it to 0, then it immediately is set to 1 for the next round)

In the game when I step on the plate I see the gear appear and start to slowly spin around, very smooth and nice!

PS, there is a speed limit on the timers, as 0.01 seems no faster than 0.03, but 0.03 is definitely faster than 0.1

----------------------
EDIT - I've found 120 works smoother (at least for this small gear), with the objects.lua code using i*3 (so i=1,120 gets you from 3 degrees to 360 degrees), then set the ticker timer to 0.1, quite smooth
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: [HELP] How can I set rotation dynamically?

Post by Komag »

I've got the huge giant dream gears rotating now, 20fps, with 1440 alcoves! It works very well.

One advice - maybe name the things something weird or numeric, so it doesn't fill up the asset browser too much when typing in for other things
Finished Dungeons - complete mods to play
Post Reply