Scripted "physics" for bones! Collision detection!

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Here's another sample I've been working on. I am not posting the script yet because it is not working correctly. There are some strange offsets when the objects hit the other bones. Something to do with how I'm trying to get the "bounding box" around the bone to determine if the ball is over a bone or not.

Anyway I'm getting close. There is no collision detection on the individual bones yet. This is just another test.

http://www.lowrestv.com/moho_stuff/scri ... ounce1.mov

-vern
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

There are some strange offsets when the objects hit the other bones
That's due to the restriction you're having on the "time" parameter. For you a minimum time parameter is the frame. If the variation or collisions are produced in less time than a frame time then you find some weird results... You should live with that for that simulation system. Maybe you can change the frame rate to see if there is any improvement and then confirm if this was the problem.

Physics simulations is always getting harder when you introduce more restrictions / variables or properties (rotations, boundaries, etc.).

Take care of you friend.

-G
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

It is true that the time factor does cause trouble but I fix that by just changing the gravity to slow things down. That adds to the time. Very large fast movements will cause problems but that happens with Flash/Actionscript as well. I just adjust the values to compensate.

The specific problem with this script has to do with "reversed" coordinate space. The code I based this on is based on 0 at top left with positive numbers going down to the right. The numbers get higher as the objects fall.

Anime Studio is based on 0 in the CENTER with NEGATIVE numbers going down to the LEFT :shock: . Everything is all switched around. I just have to figure out how to flip around some of the equations.

The balls are bouncing correctly off the floor because I just reverse the gravity. In the other samples I had no issues with the reversed coordinates because it worked if I made gravity a negative value instead of positive.

In this case there is some more stuff going on. I am rotating the "angle" of the angled collision bones to "0" then applying the bounce and velocity to a "flat" surface making it easy to calculate the proper angle for the velocity, then rotating those new values back again to get the correct values for the bouncy bones new position.

What is happening is the bones are bouncing when they hit the BOTTOM of the bones instead of the TOP (the "offset" I spoke of). The stupid collision detection is UPSIDE DOWN! I can't quite figure out how to fix it. I got it to bounce at the correct angle... I just need to figure out where the crazy thing is reversing the collision detection.

There is code that checks if a bouncy bone's velocity is > or < the y position. This indicates that the bone was either above the line or below it when it "hits" the bone. It should only indicate a "hit" when it is falling from above and pass through when it is bouncing from below and hits.

Because my coordinate space is upside down... I am getting these odd results.

The other problem is finding the bounding box of the angled bones. That is probably easy I just haven't done it right. I have to figure out the "width" of the rotated bone so I can determine if the bouncing bone is within either end so it can "fall off" the edge. Currently this value is completely wrong. If the bone is rotated really far its bounding box is to wide because I used the length of the bone instead of calculating the actual width after rotation.

This is where using the mesh would come in handy. There is code to access the bounding box of a group of points. I should just get the bounding box of the points that are bound to the bone and use that as my width. There is no function for that with bones in the AS scripting.

I will figure it out eventually. ;)

-vern
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

Why don't you left the script form flash as it is (without reversing nothing and then just reverse the layer in Anime Studio to see the simulation as usual (gravity pointing down)). If also even every thing comes left/right reversed then just flip the layer.
Better don't touch the already working scripts and only make layers operations to fit the directions.

I'm sorry if this would make you redo all the scripts but I didn't know that the original scripts had that coordinate system.

BTW the AS system is the usual (positive right-up and negative left-down).

-G
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

BTW the AS system is the usual (positive right-up and negative left-down).
Yes. AS is correct

Flash IS COMPLETELY WRONG! including its dang backwards angle rotations. Flash uses a coordinate space that is different from everything else.
Why don't you left the script form flash as it is (without reversing nothing and then just reverse the layer in Anime Studio to see the simulation as usual (gravity pointing down)). If also even every thing comes left/right reversed then just flip the layer.
Pure genius once again Genete. That worked perfectly. I even put the zero point of the layer at the top left so it matches the reference code.

Do you think this is a problem using it in AS though? Would this be a silly workaround because I don't know how to flip the code? ;)

I think I will use this as a temporary solution until I figure out how to flip the coordinate system the way it should be. It is annoying trying to place things numerically because now it's all upside down and backwards. ;)

-vern
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

Making reverse the coordinate system manually would be always a night mare.

You can solve that also making a coordinate transformation (matrix operation) BEFORE you assign the calculated values to the bones. Work with "fake" bone system (an array of vectors with velocities masses and so on) and at the end when you apply the result to the real bones you put in the middle a matrix transformation to match AS coordinate system. This would complicate the translation of the script a the fort time but once done you just drop the flash script inside that flash - AS interface and every thing should work.
A secondary problem is that (I guess) every different flash script have a different structure (were written by different people) and the internal structure is different or adapted to the specific problem to solve. Ii would not have a simple solution for every situation.

I think that a final layer operation is not as ugly if people know it from the beginning. Is it possible to apply the flip layer operation inside the embedded script? I think yes.
I'm glad I can help you a little.
-G
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I started fiddling around with some "flocking" scripts!!!

This will be REALLY cool.

There are a bunch of Flash ActionScripts out there with mathematically described grouping behaviors. You can make things follow each other around but not bump into each other... uh... you know... like flocking birds or crowds.

I just thought this could be interesting if you rotated the layers in perspective to do some sort of crowd shots.

-vern
human
Posts: 688
Joined: Tue Jan 02, 2007 7:53 pm

Post by human »

heyvern wrote:I started fiddling around with some "flocking" scripts!!!

This will be REALLY cool.

There are a bunch of Flash ActionScripts out there with mathematically described grouping behaviors. You can make things follow each other around but not bump into each other... uh... you know... like flocking birds or crowds.

I just thought this could be interesting if you rotated the layers in perspective to do some sort of crowd shots.

-vern
Well, I tend to agree that flocking would be produce larger amounts of coolness than collision detection.

I've seen some anime trailers where large flocks of wheeling birds in the distance are used to give grandeur to big "set pieces."

(Definition: "set piece" -- spectacular scenery calculated to be memorable.)

Note that once you have the flocking in place, you also need something which is, in this case, actually secondary animation--flapping wings.

The most majestic flocks, however, don't bother to flap very often--they glide a lot.
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Here's what I'm thinking...

Suppose you animate one bird with an action using only point motion. You could duplicate that vector layer as many times as needed then apply the action on offset frames... maybe scale the key frames whatever... to add variety.

Then just bind those layers to each bone in the "flock" system or copy all the bird vectors to one layer and bind each bird to a bone. Copy and paste would keep the animation but it would a nightmare making changes at that point.

There is also a script for converting bone motion to point motion. So you could use a rigged bird with bones, convert the bone motion to point motion then do the same thing as I described.

Or... just thought of this. A rigged bird with bones on one bone layer with a flapping action. Duplicate the bone layer many times then bind each bone layer to bones for the flocking in the parent layer.

It doesn't have to be a bird of course. ;)

Of course I have to actually FINISH the flocking script before any of this can happen. ;)

-vern
User avatar
Mikdog
Posts: 1901
Joined: Tue Jul 05, 2005 3:51 pm
Location: South Africa
Contact:

Post by Mikdog »

Hey Heyvern,

Wondering if your script would be able to simulate a bunch of cans and bottles being thrown into a bin, then being poured out of a bin.
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Yes absolutely. That actually works now to a degree. I am trying to incorporate the "mesh shape" as part of the physics. That part is difficult. Right now the physical boundaries of collision is based on a circle around the bone. This is why I've been testing only with balls. :oops:

This is an example of what you are talking about:

http://www.lowrestv.com/moho_stuff/scri ... ounce1.mov

Basically in your scenerio you would have an initial forward motion of the bone to land in a barrel or box and then turn the box and the bones would pour out. The physics math would handle everything else.

Once I can work out a good way to determine "irregular" shapes it will be easier to set up ramps and fixed platforms or containers. I'm having trouble with that because a single bone has no thickness and objects pass through them if moving too fast.

I've been looking at an open source "Flash" based scripting library that has EVERYTHING you could imagine. If I can translate that to work with AS it should cover all the bases and allow for virtually any type of physical interaction.

-vern
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Post by chucky »

Heyvern, I'm a bit behind the times here but I have just found this thread, and I am once again gob smacked by your scripts.
That being said , of course I would want to try these physics and gravity ones out.
I downloaded and pasted in the lua but it isn't showing up in the scripts menu.
Did I put it in the wrong place? I seem to have copied and pasted both gravity and physics into every corner of 5.6 scripts, what I do wrong? :(
Is it easy enough for a complete doofus like myself to use, or am I going to need brain augmentation? :roll:
Does Mike ever talk to you about adding these features you (and Genete et al) keep developing into anime? I can image a special animators heaven for us to all frolic in. :shock:
User avatar
Rasheed
Posts: 2008
Joined: Tue May 17, 2005 8:30 am
Location: The Netherlands

Post by Rasheed »

Perhaps I can answer this question. The script is a so-called layer script. You need to set it in the General tab of the bone layer (Options -> embedded script file).

You can save a layer script anywhere you want, as long as it is accessible for the file system.
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Post by chucky »

Thanks Rasheed, much appreciated.
I have managed to recreate the billiard ball scenario by copy pasting verns names and values to a new construction of the same thing, in order to understand it, I've been reading the script also.
Some of it seems straightforward enough but then I get lost.
Is it possible to make a little ui for changing the gravity values and direction?
Also I've found the part of the gravity4 script which describes the bounce off floor and ceilings function, I'm trying to turn that off, without breaking the script any tips?

Maybe that could be in the ui too?
Also I've found the part of the gravity4 script which describes the bounce off floor and ceilings function, I'm trying to turn that off, without breaking the script any tips?
I figured this part out,COOL! not so hard, if I knew the first thing about programming that would have be a no brainer I can see that now.

Maybe that was the first thing :roll:
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I have so many things on my list of projects. I tend to spread myself too thin. I really want to get back into this at some point and create a "UI" I just don't have the motivation at the moment.

I have lots of real world paying jobs to focus on, and a few other personal animation jobs. One of these days i will get back on this one because it is pretty cool.

One thing that set me back was a crazy idea I had to off load all this kind of stuff into an external utility script. I had hoped to eliminate the layer script completely and have the bone names control everything... silly me... I did a lot of the work before being sure it would work...

... Unfortunately it won't work that way. Mike Clifton confirmed this. I spent a lot of energy on that and so I have to go back to the way I had started doing it using custom layer scripts for each type of physics.

I can still use a UI for it with some kind of pop up menu script but I just haven't gotten around to it yet.

A lot of the functionality is there in the scripts if you want to fiddle around with it. I haven't looked at them in ages so I can't tell you exactly what is doing what. I have to switch from "artist mode" to "programmer mode" and my brain is all fried from a week with the flu.

-vern
Post Reply