Newgrounds.com — Everything, By Everyone.

Checking login status…

USERNAME:

PASSWORD:

Logging in…

Logged in as:
.
Logging out…
Inbox My Account Log Out


Forum Topic: Can't understand IK...

(139 views • 10 replies)

This topic is 1 page long.

<< < > >>
Sad

Wolfears2

Reply To Post Reply & Quote

Posted at: 5/14/08 03:07 PM

Wolfears2 EVIL LEVEL 08

Sign-Up: 10/20/07

Posts: 556

Ok, to cut things short:
I know what inverse kinematics are
I just about (sort of) understand the math.
I just want to keep some points a set distance apart, don't care about rotation. I can do all the instances and movieclip names myself, I'm not a complete noob =P
Any help?


None

Wolfears2

Reply To Post Reply & Quote

Posted at: 5/14/08 03:57 PM

Wolfears2 EVIL LEVEL 08

Sign-Up: 10/20/07

Posts: 556

wow, are they that complicated?


None

Coaly

Reply To Post Reply & Quote

Posted at: 5/14/08 04:02 PM

Coaly FAB LEVEL 20

Sign-Up: 08/11/04

Posts: 2,472

I'm not sure what you're asking, but the basic idea is to find the angle from point A to point B, and then adjust point B's position using the angle and whatever distance you choose.

profile | pm me | sig by bomtumes

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 5/14/08 04:13 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 4,942

At 5/14/08 04:02 PM, Coaly wrote: I'm not sure what you're asking, but the basic idea is to find the angle from point A to point B, and then adjust point B's position using the angle and whatever distance you choose.

although if all you are wanting is a distance constraint, then that is totally unnecesary, and its much faster executing to use a bit of vector math, take the vector between the two points, and rescale it to match the required distance.

My social worker says im special!

BBS Signature

None

greenkube

Reply To Post Reply & Quote

Posted at: 5/14/08 04:53 PM

greenkube NEUTRAL LEVEL 12

Sign-Up: 10/24/05

Posts: 534

I was playing around with IK stuff about a month ago and found a good example in some AS2 code, so I converted it to a couple of classes and updated it to AS3.

Here is what I use as my Document Class:

package {
	import flash.display.MovieClip;

	public class IKChain extends MovieClip {
		
		protected var numOfNodes:Number = 10;
		
		public function IKChain() {
			CreateNodes();
		}
		
		protected function CreateNodes():void
		{
			for (var c = 0; c < numOfNodes; c++)
			{
				var myNode:Node = new Node(stage.stageWidth, stage.stageHeight, c);
				this.addChild(myNode);
			}
			
			var firstNode = this.getChildByName("node0");
			firstNode.nodes.push(this.getChildByName("node1"));
			
			for ( var b = 1; b < numOfNodes - 1; b++)
			{ 
				myNode = Node(this.getChildByName("node" + b));
				myNode.nodes.push(this.getChildByName("node" + (b - 1)));
				myNode.nodes.push(this.getChildByName("node" + (b + 1)));
			}
		
			var lastNode = this.getChildByName("node" + (numOfNodes - 1));
			lastNode.nodes.push(this.getChildByName("node" + (numOfNodes - 2)));
			
			firstNode.Move(firstNode, null);
		}
	}
}

And here is what I refer to as my Node class for each node. You need to link the items you want chained together in your linkage to the Node class:

package {
	import flash.display.MovieClip;

	public class IKChain extends MovieClip {
		
		protected var numOfNodes:Number = 10;
		
		public function IKChain() {
			CreateNodes();
		}
		
		protected function CreateNodes():void
		{
			for (var c = 0; c < numOfNodes; c++)
			{
				var myNode:Node = new Node(stage.stageWidth, stage.stageHeight, c);
				this.addChild(myNode);
			}
			
			var firstNode = this.getChildByName("node0");
			firstNode.nodes.push(this.getChildByName("node1"));
			
			for ( var b = 1; b < numOfNodes - 1; b++)
			{ 
				myNode = Node(this.getChildByName("node" + b));
				myNode.nodes.push(this.getChildByName("node" + (b - 1)));
				myNode.nodes.push(this.getChildByName("node" + (b + 1)));
			}
		
			var lastNode = this.getChildByName("node" + (numOfNodes - 1));
			lastNode.nodes.push(this.getChildByName("node" + (numOfNodes - 2)));
			
			firstNode.Move(firstNode, null);
		}
	}
}

Hope this helps you get a jump start on the idea.

BBS Signature

None

greenkube

Reply To Post Reply & Quote

Posted at: 5/14/08 04:54 PM

greenkube NEUTRAL LEVEL 12

Sign-Up: 10/24/05

Posts: 534

Sorry, I just realized I posted the IKChain class twice, here is the node class:

package {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Node extends MovieClip {
		
		public var nodes:Array = new Array();
		public var distance:Number = 50;
		
		public function Node(stageWidth:Number, stageHeight:Number, nodeNumber:Number) {
			this.x = Math.random() * stageWidth;
			this.y = Math.random() * stageHeight;
			this.name = "node" + nodeNumber;
			LoadEvents();
		}	
		
		protected function LoadEvents():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN, NodeMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, NodeMouseUp);
		}
		
		protected function NodeMouseDown(e:MouseEvent):void
		{
			this.addEventListener(Event.ENTER_FRAME, MouseMove);	
		}
		
		protected function NodeMouseUp(e:MouseEvent):void
		{
			this.removeEventListener(Event.ENTER_FRAME, MouseMove);
		}
		
		public function MouseMove(e:Event):void
		{	
			this.x = stage.mouseX;
			this.y = stage.mouseY;
			Move(this, null);
		}
		
		public function Move(myNode:Node, parentNode:Node):void
		{
			for (var c = 0; c < myNode.nodes.length; c++)
			{
				if (parentNode == null || Node(myNode.nodes[c]).name != parentNode.name)
				{
					MakeChildMove(Node(myNode.nodes[c]), myNode);
					Move(Node(myNode.nodes[c]), myNode);
				}
			}
		}
		
		public function MakeChildMove(childNode:Node, parentNode:Node)
		{
			var dx = childNode.x - parentNode.x;
			var dy = childNode.y - parentNode.y;
			var alpha = Math.atan2(dy, dx);
			childNode.x = parentNode.x + Math.cos(alpha) * distance;
			childNode.y = parentNode.y + Math.sin(alpha) * distance;
			childNode.rotation = (Math.PI + alpha) * 180 / Math.PI;
		}
	}
}
BBS Signature

None

Wolfears2

Reply To Post Reply & Quote

Posted at: 5/15/08 12:48 AM

Wolfears2 EVIL LEVEL 08

Sign-Up: 10/20/07

Posts: 556

At 5/14/08 04:54 PM, greenkube wrote: Sorry, I just realized I posted the IKChain class twice, here is the node class:

package {
import flash.display.MovieClip;...

Is all that AS3? I'm using a older version of flash, so I can't use that if it is.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 5/15/08 08:07 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 9,116

It is obviously and you don't need to. Use it as reference. Though really I'd say you're probably too stupid to use IK if you can't port a hundred lines of code.

haXe AliceML Box2dLite Learn AS
#ngprogramming at irc.freenode.net
OVER NINE THOUSAAAAND!!!


None

greenkube

Reply To Post Reply & Quote

Posted at: 5/15/08 11:08 AM

greenkube NEUTRAL LEVEL 12

Sign-Up: 10/24/05

Posts: 534

At 5/15/08 08:07 AM, GustTheASGuy wrote: It is obviously and you don't need to. Use it as reference. Though really I'd say you're probably too stupid to use IK if you can't port a hundred lines of code.

Damn thats harsh. IK can be a tricky thing to grasp, especially if you are looking at for the first time.

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 5/15/08 11:23 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 9,116

To a beginner probably. My issue is when people briefly state that the suggested that is clearly sufficient is not a working solution therefore they need something better.

haXe AliceML Box2dLite Learn AS
#ngprogramming at irc.freenode.net
OVER NINE THOUSAAAAND!!!


None

greenkube

Reply To Post Reply & Quote

Posted at: 5/15/08 11:55 AM

greenkube NEUTRAL LEVEL 12

Sign-Up: 10/24/05

Posts: 534

At 5/15/08 11:23 AM, GustTheASGuy wrote: To a beginner probably. My issue is when people briefly state that the suggested that is clearly sufficient is not a working solution therefore they need something better.

True, I can agree with that.

BBS Signature

All times are Eastern Daylight Time (GMT -4) | Current Time: 08:50 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!