AS3 2.1 Port (Alchemy), WCK Update and Kinematic Demos

PaulGene:

I set up a simple project to test contact event reporting. It appears to be working as advertised (I only get the events I'm interested in). If you can PM me a full project that demonstrates your bug, I'll take a look at it.

As sketchbook mentioned, begin contact does not have impulses.

You were right about world.CreateJoint not completely working. There are two ways to create joints and that method is only to keep things consistent with the normal Box2D api. I prefer this method:

new b2WeldJoint(world, jointDef);

Because it allows you to extend the joint classes. I've fixed the world.CreateJoint method though. I think this may fix the debug drawing as well.
sketchbookgames:

Code:
   //////the following commented items cause errors when i try them ///////
   //thisRevJoint.b2body1 = dev.world.b2world.m_groundBody;
   //thisRevJoint.b2body2 = dev.hero.body.b2body;
   //thisRevJoint.createRevoluteJoint();
   //thisRevJoint.type = "Revolute";
   
   dev.world.addChild(thisRevJoint);
}


You don't need the call to createRevoluteJoint(). When you add the joint to the world (addChild) it will call that automatically. Since you are mid-timestep (inside a contact callback) it will delay the creation until the timestep is over. You also don't need to set b2body1 and b2body2 explicitly. It will find them based on target1Object and target2Object.
sketchbookgames wrote:
allowing specific bodies to have different intensities of gravity.
i added it to make my flame sensor bodies accelerate upward -0.20 gravity. (from a flame thrower)
just using linearVelocityY looked bad, but gradual acceleration looked good, i realize there are other ways to do it, however i think things that rise up like fire and balloons would always be the opposite direction of gravity. and there are probably many applications for various gravity values... imitating wind resistance on items etc... and it does't seem like a change that adds much (if any) weight.

to be backwards compatable, you could make a setter for applyGravity, when set to false, modifyGravity gets set to 0.

looking forward to your opinion either way.


You can already extend b2World.getGravityFor() to check the type of your bodies and apply different gravity. Or you can add a time step listener to your flame-particle class that increments linearVelocity.

I thought I could just change applyGravity to a numeric value (false = 0, true = 1), but no, that breaks all existing components.
RyuMaster: Not sure why saving the demo corrupts it. Flash CS4? For a while CS4 had problems with big projects, but they released a free patch that fixed it. Might be the issue.

sketchbook: Haven't figured out the raycast thing yet. I'm starting to think it might be a Box2D bug. If you need ray casting pronto, a thin sensor object may get the job done.
Mayo, thanks a lot for fixing the Pulleys. It rocks now!

I have two suggestios for the BodyShape.as

Here is an example of how i have to modify the values to make sense for my project:
Code:
      /// Base strength of the drag mouse joint.
      public static var dragJointStrength:Number = 2;
      
      /// Added to the strength of the mouse joint - multiplied by the mass of the body being dragged.
      public static var dragJointMassFactor:Number = -1;

It would be cool if those statics were component parameters of the World rather than code, however i haven't figured how to change that yet.





Also I noticed if maxForce is negative, it throws an error. As I want a negative dragJointMassFactor (so that heavier objects cant be dragged as easily), i have to include a < 0 check at createDragJoint.


Code:
public function createDragJoint() {
         dragJoint = new dragJointClass() as Joint;         
         
         /// *CHANGE* test if maxForce is negative and set it to 0
         var testMaxForce:Number = (dragJointStrength + (b2body.m_mass * dragJointMassFactor));         
         
         if ( testMaxForce < 0)
         {
            dragJoint.maxForce = 0;
         }
         else
         {
             dragJoint.maxForce = testMaxForce;
         }         
         
         dragJoint.frequencyHz = 999999;
         dragJoint.dampingRatio = 0;
         dragJoint.collideConnected = true;
      }
There's one area of my level where performance drops down a lot.
Usually it stays above 60fps, but here it goes down to 42fps.

I am using 2 large vectors in the Background in that scene, each with Parallax scrolling (misc.Parallax). Everything else is also drawn in Flash and therefore vectors and not Bitmaps. Maybe i should cache all those as Bitmaps ? Or are those large BG objects anyways overkill for flash ?
*Update*
I noticed the presence of the distance joint caused the performance drop. Its a simple box on a chain and even if it doesnt move, the framerate goes from 60 down to 45. Once the joint is removed, performance goes back to 60 fps.


Cheers
Hey guys. I'm a recent convert from the Java side of things and an even more recent convert to this particular port. I've started to add a few things to the Wiki (on GitHub) as I run into things and solve them. Since lack of documentation was one of the shortcomings mentioned earlier, perhaps we can work together to get a fuller set of documents up, so that newcomers can jump into it more easily.

Since this is one of the first major Alchemy-based projects I've seen used in production, it seems like we ought to work to educate users more about Alchemy, why we're using it and what its various consequences and quirks are. For example, the need to initialize the library caught me off guard, as did the idea of having two sets of base math classes. I've documented both inside a FAQ page.
I've discovered a small issue with the Platform.as, the player or other objects sometimes fall through when running from one platform to the next. Haven't figured out why yet.



Also, after building my world with the World class, i tried switching it to extras.WorldClickAdd to get the extra functionality.

However that threw 4 errors with name conflicts:
1152: A conflict exists with inherited definition wck:World.pull1 in namespace public

And about 20+ errors like this:
5000: The class 'extras.BodyShapeClickRemove' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
5000: The class 'extras.Transport' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

etc...
@Frnk,
Quote:
fall through when running from one platform to the next.


if you use regular physics shapes, you may notice that a rectangle would catch the edge from one platform to the next. because it registers a side collision.
*even when 2 ground pieces are registered at the exact same Y position. probably thanks to gravity or other forces as you approach the edge.
*when writing my own physics a few years ago i could remove this issue by changing the order of the time-step, but then moving platforms etc would act up. Box2d is far more stable than any physics i have written and probably has the best order of time-step already, so i recommend working around this issue.

when using a platform (which only applys collisions to the top, and not to sides or bottom) that side collision doesn't count and there is not top collision.


a simple solution to either situation should be rounded edges. typically i use a circle at the feet and a rectangle for the rest of the upper part of my character... that way it doesn't get snagged on tiled edges. or in your case, fall through.
sketchbookgames wrote:
@Frnk,
Quote:
fall through when running from one platform to the next.


a simple solution to either situation should be rounded edges. typically i use a circle at the feet and a rectangle for the rest of the upper part of my character... that way it doesn't get snagged on tiled edges. or in your case, fall through.


Thanks, sketch. I tried a circle and that fixed it for the player, thanks dude !!

However the boxes are still falling through and i don't wanna add circles to all their corners yet.
I thought about trying to modify the platform code with an extra check line in the middle of the platform.
If an object would fall through the middle of the platform and it's vector points down (object is falling through the platform), it would get bounced back. Hopefully objects then should be able to pass only from bottom to top.


Update:
I tried tracing the n.y which is usually -1. Though i noticed that the fallthrough happens if n.y is 0. This is often the case if you jump onto a platform with greater downspeed or quickly change directions.

I changed the (n.y > -0.8) to (n.y > 0) and this fixed the issue. The drawback is that if platforms are not perfectly aligned (can be avoided), a box shape may stop at the edge, but at least they don't fall through 🙂

Code:
      public function handleBeginContact(e:ContactEvent):void {
         if(e.normal) {
            var n:V2 = b2body.GetLocalVector(e.normal);            
            e.contact.SetSensor(n.y > 0);
         }


Update
Now there's a drawback with jumping sideays onto the platform. You can get stuck and bounce off the sides (as n.y is also 0 in this case) which is also akward. Hmm... somehow these normals aren't precise enough, gotta find something else.

This kinda fixes the sidecollision issue (at least when jumping upwards)
Code:
e.contact.SetSensor(n.y > 0 || (e.relatedObject.linearVelocityY < -0.1));



Cheers

Original thread: https://box2d.org/forum/viewtopic.php?t=4105