示例#1
0
void Box2DPhysics::AddPolygon(std::shared_ptr<Actor> actor, std::vector<glm::vec2> vertices, float density, bool dynamic, bool fixedRotation)
{
    b2BodyDef bodyDef;
    bodyDef.position.Set(PixelsToMeters(actor->GetPosition().x + actor->GetScale().x * 0.5), PixelsToMeters(actor->GetPosition().y + actor->GetScale().y * 0.5)); // position should be center of object instead of top-left
    if(dynamic)
        bodyDef.type = b2_dynamicBody;
    bodyDef.fixedRotation = fixedRotation;
    b2Body* body = m_World->CreateBody(&bodyDef);
    
    BodyUserData *userData = new BodyUserData;
    userData->Physics = this;
    userData->Type = actor->GetType();
    body->SetUserData((void*)userData);

    // create 8 vertices 
    b2Vec2 verts[8];
    for(unsigned int i = 0; i < vertices.size(); ++i)
        verts[i].Set(PixelsToMeters(vertices[i].x * actor->GetScale().x * 0.5), PixelsToMeters(vertices[i].y * actor->GetScale().y * 0.5));
    b2PolygonShape shape;    
    shape.Set(verts, 8);
   
    b2FixtureDef fixture;
    fixture.shape = &shape;
    fixture.density = density;
    
    body->CreateFixture(&fixture);

    m_BodyToActorID[body] = actor->GetID();
    m_ActorIDToBody[actor->GetID()] = body;
}
示例#2
0
void Box2DPhysics::AddBox(std::shared_ptr<Actor> actor, float density, std::string type, bool fixedRotation, bool isSensor, float hitboxScale)
{
    b2BodyDef bodyDef;
    bodyDef.position.Set(PixelsToMeters(actor->GetPosition().x + actor->GetScale().x * 0.5), PixelsToMeters(actor->GetPosition().y + actor->GetScale().y * 0.5)); // position should be center of object instead of top-left
    if (type == "dynamic")
        bodyDef.type = b2_dynamicBody;
    else if (type == "kinematic")
        bodyDef.type = b2_kinematicBody;
    bodyDef.fixedRotation = fixedRotation;
    b2Body* body = m_World->CreateBody(&bodyDef);

    BodyUserData *userData = new BodyUserData;
    userData->Physics = this;
    userData->Type = actor->GetType();
    body->SetUserData((void*)userData);

    b2PolygonShape shape;
    shape.SetAsBox(PixelsToMeters(hitboxScale * actor->GetScale().x * 0.5), PixelsToMeters(hitboxScale * actor->GetScale().y * 0.5));

    b2FixtureDef fixture;
    fixture.shape = &shape;
    fixture.density = density;
    fixture.isSensor = isSensor;

    body->CreateFixture(&fixture);

    m_BodyToActorID[body] = actor->GetID();
    m_ActorIDToBody[actor->GetID()] = body;
}
示例#3
0
glm::dvec4 MercatorProjection::TileBounds(const TileID _tileCoord) const {
    glm::dvec2 boundMin, boundMax;
    glm::dvec4 bounds;
    boundMin = PixelsToMeters(glm::dvec2(_tileCoord.x*m_TileSize, _tileCoord.y*m_TileSize), _tileCoord.z);
    boundMax = PixelsToMeters(glm::dvec2((_tileCoord.x+1)*m_TileSize, (_tileCoord.y+1)*m_TileSize), _tileCoord.z);
    bounds = glm::dvec4(boundMin.x, boundMin.y, boundMax.x, boundMax.y);
    return bounds;
}
示例#4
0
	//-------------------------------------------------------------------------
	//
	// Convert tile center plus pixel offset to lat/lon. Offset increases right and down
	//
	LonLat OpenStreetMapSource::tileCenterToLonLat( const int tileSize, const MapTileCoordinate& tile, const double offsetX, const double offsetY )
	//-------------------------------------------------------------------------
	{
		double meterX, meterY;
		double pixelX, pixelY;
		
		TileCenterPixels( tileSize, tile.getX( ), tile.getY( ), pixelX, pixelY );
		pixelX += offsetX;
		pixelY += offsetY;
		PixelsToMeters( pixelX, pixelY, tile.getMagnification( ), meterX, meterY );
		double lon, lat;
		MetersToLonLat( meterX, meterY, lon, lat );
		return LonLat( lon, lat );
	}
示例#5
0
	//-------------------------------------------------------------------------
	//
	// Convert tile center plus pixel offset to lat/lon. Offset increases right and down
	//
	LonLat GoogleMapSource::tileCenterToLonLat( const int tileSize, const MapTileCoordinate& tile, const double offsetX, const double offsetY )
	//-------------------------------------------------------------------------
	{
		double meterX, meterY;
		TileCenter( getTileSize( ), tile.getX( ), tile.getY( ), tile.getMagnification( ), meterX, meterY );
		
		double pixelX, pixelY;
		MetersToPixels( meterX, meterY, tile.getMagnification( ), pixelX, pixelY );
		pixelX += offsetX;
		pixelY -= offsetY; // google maps y coords increase up
		PixelsToMeters( pixelX, pixelY, tile.getMagnification( ), meterX, meterY );
		
		double lon, lat;
		MetersToLonLat( meterX, meterY, lon, lat );
		
		return LonLat( lon, lat );
	}
示例#6
0
	//-------------------------------------------------------------------------
	//
	// Returns center of the given tile in meters
	//
	inline static void TileCenterMeters( const int tileSize, const int tileX, const int tileY, const int magnification, double& meterX, double& meterY )
	//-------------------------------------------------------------------------
	{
		PixelsToMeters( (tileX + 0.5f ) * tileSize, ( tileY + 0.5f ) * tileSize, magnification, meterX, meterY );
	}
示例#7
0
void Box2DPhysics::SetLinearVelocity(unsigned int ActorID, glm::vec2 velocity)
{
    FindBody(ActorID)->SetLinearVelocity(b2Vec2(PixelsToMeters(velocity.x), PixelsToMeters(velocity.y)));
}
示例#8
0
void Box2DPhysics::ApplyImpulse(unsigned int ActorID, glm::vec2 force, glm::vec2 center)
{
    m_ActorIDToBody[ActorID]->ApplyLinearImpulse(b2Vec2(PixelsToMeters(force.x), PixelsToMeters(force.y)), b2Vec2(PixelsToMeters(center.x), PixelsToMeters(center.y)), true);
}
示例#9
0
void Box2DPhysics::ApplyForce(unsigned int ActorID, glm::vec2 force, glm::vec2 center)
{
    m_ActorIDToBody[ActorID]->ApplyForceToCenter(b2Vec2(PixelsToMeters(force.x), PixelsToMeters(force.y)),  true); 
}
示例#10
0
void Box2DPhysics::AddCharacter(std::shared_ptr<Actor> actor, float density)
{
    b2BodyDef bodyDef;
    bodyDef.position.Set(PixelsToMeters(actor->GetPosition().x + actor->GetScale().x * 0.5), PixelsToMeters(actor->GetPosition().y + actor->GetScale().y * 0.5)); // position should be center of object instead of top-left
    bodyDef.type = b2_dynamicBody;
    bodyDef.fixedRotation = true;
    bodyDef.allowSleep = false;
    b2Body* body = m_World->CreateBody(&bodyDef);
    body->SetBullet(true);

    BodyUserData *userData = new BodyUserData;
    userData->Physics = this;
    userData->Type = actor->GetType();
    body->SetUserData((void*)userData);

    // Create box top-shape
    b2PolygonShape boxShape;
    b2Vec2 vertices[4];
    vertices[0].Set(PixelsToMeters(-actor->GetScale().x * 0.2), PixelsToMeters(-actor->GetScale().y * 0.5));
    vertices[1].Set(PixelsToMeters(-actor->GetScale().x * 0.2), PixelsToMeters(actor->GetScale().y * 0.2));
    vertices[2].Set(PixelsToMeters(actor->GetScale().x * 0.2), PixelsToMeters(actor->GetScale().y * 0.2));
    vertices[3].Set(PixelsToMeters(actor->GetScale().x * 0.2), PixelsToMeters(-actor->GetScale().y * 0.5));
    boxShape.Set(vertices, 4);
    b2FixtureDef fixture;
    fixture.shape = &boxShape;
    fixture.density = density;
    fixture.friction = 0.95f;
    body->CreateFixture(&fixture);
    // Create circle bottom-shape
    b2CircleShape circleShape;
    circleShape.m_p.Set(0.0f, PixelsToMeters(actor->GetScale().y * 0.46 - actor->GetScale().x * 0.20));
    circleShape.m_radius = PixelsToMeters(actor->GetScale().x * 0.23);
    b2FixtureDef fixture2;
    fixture2.shape = &circleShape;
    fixture2.density = density;
    fixture2.friction = 1.9f;
    body->CreateFixture(&fixture2);

    // Create bottom sensor to detect floor-collisions
    b2PolygonShape sensorShape;
    b2Vec2 origin = b2Vec2(0.0, PixelsToMeters(actor->GetScale().y * 0.44));
    sensorShape.SetAsBox(PixelsToMeters(actor->GetScale().x * 0.05), PixelsToMeters(actor->GetScale().y * 0.05), origin, 0.0f);
    b2FixtureDef fixture3;
    fixture3.shape = &sensorShape;
    fixture3.isSensor = true;
    body->CreateFixture(&fixture3);


    m_BodyToActorID[body] = actor->GetID();
    m_ActorIDToBody[actor->GetID()] = body;
}