Skeletal object system?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Skeletal object system?

Post by Taehl »

So I'm messing around with a miniproject about growing plants. I made a little system so that the plants are skeleton-based, that is, they're made of pieces which each have an angle, a length, and may have a table of attached pieces. I have a drawing function which recurses down the body, drawing all the parts at their correct angle, position, and scale (based on length). For the more visually oriented, this:

Code: Select all

body = {
	a=0, l=40, i=graphics.stem,
	parts = {
		{a=1, l=20, i=graphics.stem},
		{a=-1, l=40, i=graphics.stem,
			parts = {{a=1, l=20, i=graphics.stem,},},
		},
	},
},
Equals this:

Image

So my question is, should I clean this up, maybe expand it to have some kind of animation system, and release it? Or does nobody have a use for such a thing?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Skeletal object system?

Post by TechnoCat »

User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Skeletal object system?

Post by Taehl »

Image
:ultrahappy:

Anyway... Vines? This may do the job. Would you need them to animate, or do you just need the positioning code so you can render them to spritebatches or something?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Skeletal object system?

Post by tentus »

This is actually something that has been on my Todo list for a few weeks: a skeletal system that I can use in the place of massive prerendered animations.

Also, please call your system Boner. Please.
Kurosuke needs beta testers
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Skeletal object system?

Post by TechnoCat »

tentus wrote:Also, please call your system Boner. Please.
:crazy:
Taehl wrote:Anyway... Vines? This may do the job. Would you need them to animate, or do you just need the positioning code so you can render them to spritebatches or something?
I would want them to grow across the screen with predetermined stem and non-predetermined branches.
I would like to replace this frame by frame animation with a procedural one.
Attachments
animated-logo.love
(68.15 KiB) Downloaded 115 times
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Skeletal object system?

Post by Taehl »

No animation, then (as in, a way to manage joint rotations). What I have now should be what you need:

Code: Select all

graphics = {
	-- index 1 is the image, w is the width, and h is the height
	stem = {love.graphics.newImage("stem.png"),w=16,h=29},
}

-- here's a demonstration bone structure
flower = {
	-- x and y are treated as coordinate overrides - you'll likely only want them on the first bone.
	x=400, y=500,
	-- a, l, and i are angle (in radians), length (in pixels), and image (see above for image declaration)
	a=math.pi, l=60, i=graphics.stem,
	-- child bones are contained in the parts table
	parts = {
		-- child bones work exactly the same way as the parent bone, with the same variables
		{a=1, l=30, i=graphics.stem},
		{a=-1, l=40, i=graphics.stem,
			-- child bones can have their own child bones (which can have their own child bones, etc.)
			parts = {{a=1, l=20, i=graphics.stem,},},
		},
	},
}

function love.draw()
	-- draw the bone structure (you could also draw any individual branch of it with this same function)
	bonedraw(flower)
end

-- don't screw with this part unless you want to declare bones in a different format or something...
function bonedraw(bone, x,y, a)
	x,y,a = bone.x or x, bone.y or y, (a or 0) + (bone.a or 0)
	local s = bone.l / bone.i.h
	love.graphics.draw(bone.i[1], x,y, a, s,s, bone.i.w/2,0)
	x,y = x-bone.l*math.sin(a), y+bone.l*math.cos(a)
	if bone.parts then for k,v in pairs(bone.parts) do bonedraw(v, x,y, a) end end
end
bonedraw is responsible for drawing a bone structure. You can add more bones to a joint by simply using table.insert(bone.parts, {a=angle, l=length, i=image}) (you can name bones, if you prefer). Please note from above that image needs to be a table containing an image in index 1, and image width and height at indices "w" and "h" (tip: by using a height that's less than the full height of the image, you can have image overlap on joints). As for the graphic, the top-center of the image is the origin of the bone, and the bottom-center is its joint (the end where child bones will attach).

To do the growing thing you want, just increase a bone's length over time and add a new segment when it's full length.

EDIT) With a bit of playing around, you can easily get something like this:
Image

If you can't figure it out yourself, let's start a new thread, since it would be off-topic for this one.
Last edited by Taehl on Thu Jan 27, 2011 9:00 am, edited 1 time in total.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Skeletal object system?

Post by tentus »

I started playing around with this a bit, but it made me feel all intrusive to be modifying your code without talking about it in public. So, first question: why do you define scale, but never use it?
Kurosuke needs beta testers
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Skeletal object system?

Post by Taehl »

Because I do use a scaling variable in my plant game, and I apparently didn't do a good enough job cleaning my plant-game-related stuff from that snippet. Sorry. Rectified.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 9 guests