T2D, Box2D, and you...
by Michael Branin · in Torque 2D Beginner · 02/13/2013 (12:19 pm) · 17 replies
How does T2D MIT handle gravity and collisions now? I am converting the old Ninja Platormer Tutorial over and I am having trouble figuring out how T2D determines if the object is on the ground or in the air when you press the jump key. Look at this old function
function playerJump()
{
%yVelocity = $pGuy.getLinearVelocityY();
%xVelocity = $pGuy.getLinearVelocityX();
$pGuy.setLinearVelocityY(100);
$pGuy.setLinearVelocityX(0);
%collision = $pGuy.castCollision(0.005);
if(!(%collision $= ""))
{
$pGuy.setLinearVelocityY(-225);
}
else
{
$pGuy.setLinearVelocityY(%yVelocity);
}
$pGuy.setLinearVelocityX(%xVelocity);
}
#2
So would contact count be 1 when the oject is on the ground? For instance .. the truck in the trucktoy is on the ground the entire time so ... if I did that check on trucktoy is would be 1 the entire time
02/13/2013 (12:43 pm)
Excellent. Bookmarked that for future reference...So would contact count be 1 when the oject is on the ground? For instance .. the truck in the trucktoy is on the ground the entire time so ... if I did that check on trucktoy is would be 1 the entire time
#3
02/13/2013 (12:44 pm)
Contact count will be whatever collision shapes the object is currently touching. It' possible to launch the truck off a ramp at a speed where it would have zero contact count for a brief time.
#4
02/13/2013 (12:45 pm)
Check out engine/source/2d/sceneObject/SceneObject_ScriptBinding.h. You will find a lot of notes about how it works.
#6
Like if I run off a platform and start to fall the player objects starts rolling.
02/15/2013 (10:59 am)
How Do you keep an object from Rotating when it collides with something? For instance I have a square collision shape that when it collides with another square collision shape it wants to try and roll off of it. But.. the Main object is a player so he needs to Stay upright at all times instead of rolling.Like if I run off a platform and start to fall the player objects starts rolling.
#7
or
02/15/2013 (11:04 am)
When you create the object, set it's fixed angle to true:%object.FixedAngle = 1;
or
%object.setFixedAngle(1);
#9
I have earthlike gravity set up
Then on my player object I have
But my player still floats down to the ground like hes on the moon. I have Tried all sorts of numbers but he still just floats down. How Do I increase his weight to make him fall like normal?
02/15/2013 (1:54 pm)
Ok I have played with fields and played with fieldsI have earthlike gravity set up
// Set the scene gravity. SandboxScene.setGravity(0, -9.8);
Then on my player object I have
%player.setDefaultDensity( 6 ); %player.setDefaultFriction( 1 );
But my player still floats down to the ground like hes on the moon. I have Tried all sorts of numbers but he still just floats down. How Do I increase his weight to make him fall like normal?
#10
but my player is running and jumping and falling just like the old NinjaPlatform Tutorial.
02/15/2013 (9:03 pm)
ok I had to go with this to get more realistic gravity// Set the scene gravity. SandboxScene.setGravity(0, -29.8);
but my player is running and jumping and falling just like the old NinjaPlatform Tutorial.
#11
Melv just posted a full physics guide for T2D yesterday: Physics Guide
02/16/2013 (4:22 am)
Nice! Great job Michael. If you want to do something really cool that the old tech did not handle, create some platforms that are one-sided. In other words, you could jump up and through a platform, but land on the surface. Use an edge collision shape.Melv just posted a full physics guide for T2D yesterday: Physics Guide
#12
02/16/2013 (9:24 am)
Ok maybe I am missing something but I do not see how you can allow one side pass throughs of Edge or chain collision shapes?
#13
02/16/2013 (5:20 pm)
Edge collision shapes only collide from one side.
#14
02/16/2013 (5:44 pm)
I have one created in my level the length of a patform and I collide on it from both above and below. I can not pass throgh it from either side.// Create the ground
%ground3 = new Scroller();
%ground3.setBodyType("static");
%ground3.Image = "NinjaDemoToy:NinjaDemoMiniTileMap";
%ground3.Frame = 3;
%ground3.setPosition(0, -10);
%ground3.setSize(50, 7.5);
%ground3.setRepeatX(4);
%ground3.createEdgeCollisionShape(50/-2, 3.75, 50/2, 3.75);
%ground3.setAwake( false );
%ground3.setActive( true );
SandboxScene.add(%ground3);
#15
I suppose you could fiddle with finding the collision normal and then handling it specifically, but I can't think of a good way to handle it off hand....
02/16/2013 (9:32 pm)
Ah - I mis-read the Box2D documentation - they can't collide with other edge or chain shapes (at least one of two objects must have volume to collide) but they do provide two-sided collisions.I suppose you could fiddle with finding the collision normal and then handling it specifically, but I can't think of a good way to handle it off hand....
#16
02/16/2013 (10:33 pm)
Quote:%player.setDefaultDensity( 6 );I'm not sure quite how density calculations work in Box2D, but for reference, the density of water (close to that of the human body) is 1000kg/m^3. So you might want to go a bit higher! Maybe try calling getMass() on your player object to see what the engine has actually calculated its mass to be.
#17
02/18/2013 (10:54 am)
How do you check in torquescript the Direction of a collision? I have a player with a collision box and I need to determine whether of not he is getting collided with from above, below, to the left or to the right.
Employee Michael Perry
ZombieShortbus
Now, as for the T2D specific side of things. My first inclination would be to replace castCollision with getContactCount.
%contactCount = $pGuy.getContactCount(); if (%contactCount) { // Colliding with someone, so react } else { // Not colliding with anything }