Game Maker's Garage Forum

Game Maker's Garage => Announcements => Topic started by: Gan on May 04, 2012, 11:19:34 PM

Title: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Gan on May 04, 2012, 11:19:34 PM
(http://cl.ly/272t1q2a0F2G0a1B3m20/Screen%20Shot%202012-05-04%20at%2011.11.20%20PM.png)
http://gamemakersgarage.com/html5/HTML5-ShareGame.php?gameID=315

This is the new physics engine. Here is the code:
Code: [Select]
//This activates when your game starts. Remember! All commands and variables are case sensitive.
//world = new b2World(new b2Vec2(0, 10),  true);
LoadBox2DPhysicsEngine()

world = new b2World(new b2Vec2(0, 10),  true);
//world.SetContinuousPhysics(true);

var fixDef = new b2FixtureDef;
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;

var bodyDef = new b2BodyDef;

//create ground
bodyDef.type = b2Body.b2_staticBody;

// positions the center of the object (not upper left!)
bodyDef.position.x = GetGameWidth() / 2 / SCALE;
bodyDef.position.y = (GetGameHeight()-50) / SCALE;

fixDef.shape = new b2PolygonShape;

// half width, half height. eg actual height here is 1 unit
fixDef.shape.SetAsBox((600 / SCALE) / 2, (10/SCALE) / 2);
world.CreateBody(bodyDef).CreateFixture(fixDef);

//create circle
bodyDef.type = b2Body.b2_dynamicBody;
//bodyDef.bullet = true;
fixDef.shape = new b2CircleShape(20 / SCALE);
bodyDef.position.x = 250 / SCALE;
bodyDef.position.y = 250 / SCALE;
bike1 = world.CreateBody(bodyDef);
bike1.CreateFixture(fixDef)
bodyDef.position.x = 300 / SCALE;
bodyDef.position.y = 250 / SCALE;
bike2 = world.CreateBody(bodyDef);
bike2.CreateFixture(fixDef)

var joint = new b2DistanceJointDef();
joint.Initialize(bike1, bike2, new b2Vec2(250 / SCALE, 250 / SCALE), new b2Vec2(300 / SCALE, 250 / SCALE));
this.world.CreateJoint(joint);


//setup debug draw
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(GetMainContext());
debugDraw.SetDrawScale(SCALE);
debugDraw.SetFillAlpha(0.3);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);

myTimer = new Timer("GameTimer()", 1.0/60.0)

This code first initializes the physics engine. Then it creates the world, adds the ground, makes the two wheels of the bike and connects them by a distance joint. Then it sets up the debug drawing and starts the game timer.

You guys think this is too complex?
I do.

I'm gonna need some helping thinking of how to turn this complexity into fluff commands that still allow the great customizability.

Now when you create an object in the physics world you must:
(1) Define a body definition. Includes type and position.
(2) Define a fixture definition. Fixtures contain the attributes like density, friction, and restitution.
(3) Add shapes to the fixture definition. These shapes can be simple like a circle or square or complex like a bunch of lines or a polygon. Fixtures can hold as many shapes as you want to add.
(4) Build the completed object by running the body definition to the world.
(5) Run the fixture definition through the completed object and thus giving your object a form.

And that's how you make an object. This method allows lots of customizability.
Then you can make joints and connect objects with joints.
And there's also ways to detect collisions with objects and even to specify if objects can collide with certain other objects.

And you can even apply force and torque to objects to make them behave differently.


Now...
If you wanted to have the simplest yet most customizable commands to accomplish all of the above, what would those commands look like?
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Connors on May 05, 2012, 12:12:59 AM
Well you've got me interested now. I personally would like to see learn these commands myself, and then I could make my own methods or classes to streamline things later. Where are the docs you used to write that program? I feel like I'd find it a LOT easier after learning more about OOP by looking at Java.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Gan on May 05, 2012, 02:03:46 AM
The Javascript Box2D is actually a port from the Flash version. So here's the Flash manual, should work with the Javascript:
http://www.box2dflash.org/docs/2.0.2/manual

And here's the API, just a list of commands:
http://www.box2dflash.org/docs/2.1a/reference/

I made the example Box2D game into a template so you guys can check out the code.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Connors on May 05, 2012, 11:07:07 PM
Cool, they're the same link! That should save tons of time.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Gan on May 05, 2012, 11:28:14 PM
My bad, here's the API:
http://www.box2dflash.org/docs/2.1a/reference/
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Connors on May 07, 2012, 01:45:27 PM
Now this gets tricky guys:
I wanted to attempt a basic platformer, which I can do, and I may not try to implement this feature described below but man it's interesting.

If a person walks they exert an equal and opposite force on the floor and themselves. How could you simulate a player doing the same? If Player moves, he might be pushing the ball he's standing on the other way for example. He would do the same when jumping! However, for this to work realistically you have to distribute the net force between both objects based on their mass, position, inertia, friction and all kinds of factors that make this whole idea seem pretty daunting...

Also, basic example of applying force to only the player:
http://gamemakersgarage.com/html5/HTML5-ShareGame.php?gameID=331

The important line:
box1.ApplyForce(leftSpeed, box1.GetPosition());
ApplyForce accepts two 2D vector objects (the object is called b2Vec2). Two are declared in Global:
var leftSpeed = new b2Vec2(-0.1, 0);
var rightSpeed = new b2Vec2(0.1, 0);
The other is a method that gets the box's position.

Again, many thanks to Gan for setting all of this up.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Connors on May 07, 2012, 11:02:38 PM
Gan I looked up some stuff on how to use contact listeners and it only leads to looking up how to do more things in Javascript, can you help me out?
EDIT:
that manual. Always start with the manual. 8)
Also, I may be getting the old delayed key down glitch by putting code in Key Down.
Always put it in the Game Loop.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Gan on May 07, 2012, 11:41:24 PM
Ah, to do contact listeners I believe you'll have to set up some methods for them to call.
I'll have to research into this some. Never actually used them.
I'm sure they're super useful though.
Then again, I believe there's a way to use the contact listeners without setting up methods, just looping through contacting objects. Though I'm sure you'll want to use those methods as well. They probably will be handy.

As for the delay key glitch, it's not a glitch. It's how it's suppose to work. To get rid of the delay, put key detection in the game loop.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Connors on May 08, 2012, 09:12:21 AM
In that case what did you do to check contacts in Silver Creator Box2D? I remember that was all fluff commands but one made the program check contacts for a specific shape.

I would also love to try drawing graphics.
EDIT:
http://gamemakersgarage.com/html5/HTML5-ShareGame.php?gameID=332
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Gan on May 08, 2012, 12:37:35 PM
That's awesome! And kinda entertaining.

I see you figured out how to draw. I'll spend some time working on the contact listener.

The manual up above is an old version. Here are the changes:
http://www.box2dflash.org/docs/2.1a/updating
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Gan on May 08, 2012, 01:31:45 PM
I've update the HTML5 GameMaker to have all commands enabled! So now you can do everything.

I also figured out how to use the Contact Listener.
Quote
//Create contact listener
var m_contactListener=new b2ContactListener();
world.SetContactListener(m_contactListener);
m_contactListener.BeginContact = BeginContact;
m_contactListener.EndContact = EndContact;
You see the last two lines? In order for those to work, you need to create 2 new methods. BeginContact and EndContact.
Though you can name the methods differently if you want.

Then every time a contact happens it calls BeginContact and whenever it ends, it contacts EndContact.
Alternatively, if you add (contact) as a perimeter to the  BeginContact and EndContact methods, you'll be able to get the contact variable, from the contact variable you can get the fixtures that are touching, and from the fixtures you can get the actual bodies that are touching. And from the contact variable you can also get the contact manifold which will give you the exact X/Y location of contact points between the bodies.

If you look in the Box2D template, I have a basic example of the contact listener.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Circuit on May 08, 2012, 04:09:22 PM
This is cool.  Nice work Gan.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Connors on May 08, 2012, 04:27:16 PM
The listener you set up does nothing to prove that it's responding and I can't get it to do anything. Could you be a bit more specific? And what is the function console.log(string) doing?
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Gan on May 08, 2012, 04:31:11 PM
Oh. console.log(string) writes a message to the browser's console. That's how I make sure things work. By looking in the browser's console.

I'll edit the template to show it doing something.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Connors on May 09, 2012, 08:49:23 PM
When you press jump and hold it and then press up again it waits until you've hit left or right.
Trying to figure out why...

EDIT when it comes to rest SetVelocity doesn't take effect. ;)
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Gan on May 09, 2012, 09:33:12 PM
Quote
When you press jump and hold it and then press up again it waits until you've hit left or right.
Trying to figure out why...
Sounds like you need to reset it.

Quote
EDIT when it comes to rest SetVelocity doesn't take effect.
I think there's some function to wake up the object. I know there's a function to make it so objects never fall asleep.
Title: Re: HTML5 Gets a Physics Engine - Need Help With Docs
Post by: Connors on May 09, 2012, 09:40:22 PM
I figured it out though, it's that same link from before check it out. I'm going to try the contact listener soon.
Title: Choose waist tough foundations neglect passengers, diagnosis?
Post by: uweleyori on March 23, 2024, 12:38:46 AM
Good pub, levitra (https://yourdirectpt.com/drugs/levitra/) lowest viagra prices (https://pureelegance-decor.com/viagra/) www.prednisone.com (https://primerafootandankle.com/www-prednisone-com/) tadalafil 20 mg (https://rozariatrust.net/tadalafil-20-mg/) slimex in usa (https://altavillaspa.com/product/slimex/) micogel online uk (https://shilpaotc.com/micogel/) lasipen (https://usctriathlon.com/lasipen/) xenical without pres (https://coachchuckmartin.com/drugs/xenical/) lasix lowest price (https://primerafootandankle.com/item/lasix-lowest-price/) 25mg fildena no rx (https://americanazachary.com/drug/fildena/) pharmacy (https://center4family.com/canadian-pharmacy-online/) online pharmacy no prescription flavoxate generic canada (https://rrhail.org/pill/flavoxate/) www.kamagra.com (https://center4family.com/kamagra/) prices for cytotec (https://youngdental.net/cytotec/) discount nizagara (https://yourbirthexperience.com/nizagara/) tadalafil without prescription (https://pureelegance-decor.com/tadalafil-without-prescription/) purchase xalacom (https://racelineonline.com/xalacom/) tretinoin (https://bayridersgroup.com/tretinoin/) flagyl lowest price (https://primerafootandankle.com/low-price-flagyl/) prednisone buy in canada (https://ossoccer.org/drugs/prednisone/) resurface, anti-insulin precariously <a href="https://yourdirectpt.com/drugs/levitra/">generic levitra online</a> levitra online usa <a href="https://pureelegance-decor.com/viagra/">viagra no prescription</a> <a href="https://primerafootandankle.com/www-prednisone-com/">www.prednisone.com</a> <a href="https://rozariatrust.net/tadalafil-20-mg/">cialis</a> <a href="https://altavillaspa.com/product/slimex/">slimex no prescription</a> <a href="https://shilpaotc.com/micogel/">micogel</a> <a href="https://usctriathlon.com/lasipen/">lasipen</a> <a href="https://coachchuckmartin.com/drugs/xenical/">xenical without pres</a> xenical lowest price <a href="https://primerafootandankle.com/item/lasix-lowest-price/">lasix</a> <a href="https://americanazachary.com/drug/fildena/">fildena generic pills</a> <a href="https://center4family.com/canadian-pharmacy-online/">canadian pharmacy cialis 20mg</a> <a href="https://rrhail.org/pill/flavoxate/">flavoxate overnight</a> flavoxate <a href="https://center4family.com/kamagra/">kamagra oral jelly</a> <a href="https://youngdental.net/cytotec/">best price 200 mcg cytotec</a> <a href="https://yourbirthexperience.com/nizagara/">nizagara in usa</a> nizagara <a href="https://pureelegance-decor.com/tadalafil-without-prescription/">canadian tadalafil</a> <a href="https://racelineonline.com/xalacom/">buy xalacom w not prescription</a> xalacom without dr prescription <a href="https://bayridersgroup.com/tretinoin/">tretinoin commercial</a> <a href="https://primerafootandankle.com/low-price-flagyl/">flagyl without pres</a> <a href="https://ossoccer.org/drugs/prednisone/">prednisone without dr prescription</a> consultations: metastatic usurps https://yourdirectpt.com/drugs/levitra/ https://pureelegance-decor.com/viagra/ https://primerafootandankle.com/www-prednisone-com/ https://rozariatrust.net/tadalafil-20-mg/ https://altavillaspa.com/product/slimex/ https://shilpaotc.com/micogel/ https://usctriathlon.com/lasipen/ https://coachchuckmartin.com/drugs/xenical/ https://primerafootandankle.com/item/lasix-lowest-price/ https://americanazachary.com/drug/fildena/ https://center4family.com/canadian-pharmacy-online/ https://rrhail.org/pill/flavoxate/ https://center4family.com/kamagra/ https://youngdental.net/cytotec/ https://yourbirthexperience.com/nizagara/ https://pureelegance-decor.com/tadalafil-without-prescription/ https://racelineonline.com/xalacom/ https://bayridersgroup.com/tretinoin/ https://primerafootandankle.com/low-price-flagyl/ https://ossoccer.org/drugs/prednisone/ recession; removes catabolism.