Page 1 of 1

Skeletal object system?

Posted: Wed Dec 15, 2010 2:06 am
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?

Re: Skeletal object system?

Posted: Wed Dec 15, 2010 3:04 am
by TechnoCat

Re: Skeletal object system?

Posted: Wed Dec 15, 2010 3:34 am
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?

Re: Skeletal object system?

Posted: Wed Dec 15, 2010 4:14 am
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.

Re: Skeletal object system?

Posted: Wed Dec 15, 2010 4:30 am
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.

Re: Skeletal object system?

Posted: Wed Dec 15, 2010 5:24 am
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.

Re: Skeletal object system?

Posted: Wed Jan 26, 2011 5:16 pm
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?

Re: Skeletal object system?

Posted: Thu Jan 27, 2011 8:59 am
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.