Jump to content

  • Log In with Google      Sign In   
  • Create Account


DgekGD

Member Since 01 Feb 2012
Offline Last Active Mar 08 2014 01:50 PM
-----

#5043464 Creating a simple 2d engine (C++), best way to create an Sprite class?

Posted by DgekGD on 15 March 2013 - 12:26 PM

Create separate classes for Shaders, Vertex Buffers etc. 

 

 For example i have the following class for vertex shader:

class VertexShaderDX11
	{
	protected:

		ID3D11VertexShader*	m_pShader;
		InputAssemblerStateDX11*	m_pState;

	public:
		VertexShaderDX11()  { m_pState = new InputAssemblerStateDX11(); }
		~VertexShaderDX11() {	SAFE_RELEASE(m_pShader);
								SAFE_DELETE(m_pState); }

		ABOOL Create(BlobDX11 & shaderbuffer, INPUT_LAYOUT* layout, AUINT8 num,
										 AUINT16 topology);
		AVOID Set() const;
	};

 

Note that it has pointer to the InputAssembler structure (it stores information about Input Assembler stage as for vertex shader you have to have right input topology). 

 

 In your sprite class you can reference shader for rendering and bind it to the pipeline before you render your sprite. Don't recompile shader all the time! Compile it one time and then just have a pointer to an object of your shader class which stores compiled shader.  Multiple sprites can use single shader for rendering just having pointer to it.




#5011237 Debug pixel shader

Posted by DgekGD on 16 December 2012 - 06:14 AM

Actually, there are a lot of things that can go wrong in your code and i don't see any explicit error here. In pixel shader try
     return float4(1.0f, 1.0f, 1.0f, 1.0f);

If you see your object white, probably problem lies in binding texture resource (DefaultTexture in your shader code). If still nothing appears, check that you update constant buffers with matrices correctly for the vertex shader and bind right render target for your pixel shader. Or maybe you placed vertices in the wrong order and rasterizer culls your geometry, try D3D11_CULL_NONE.


#5009152 BulletPhysics Question

Posted by DgekGD on 10 December 2012 - 01:03 PM

Hi!

When using Bullet Physics in your game you implement some BulletInternalTickCallback( btDynamicsWorld * const world, btScalar const timeStep ) method bullet uses as internal callback.
dynamicsWorld->setInternalTickCallback( BulletInternalTickCallback );
dynamicsWorld declared as btDynamicsWorld*
In this method you want to make loop like that:

// look at all contacts
btDispatcher * const pDispatcher = world->getDispatcher();
for ( int manifoldInd=0; manifoldIdx<pDispatcher->getNumManifolds(); manifoldInd++ )
{
  //get manifold
  btPersistentManifold const * const pManifold = pDispatcher->getManifoldByIndexInternal( manifoldInd );
  assert( manifold );

  // get colliding bodies
  btRigidBody const * const pBody0 = static_cast<btRigidBody const *>(pManifold->getBody0());
  btRigidBody const * const pBody1 = static_cast<btRigidBody const *>(pManifold->getBody1());

  ...


So you acquired pointers to your colliding bodies. The next thing you want to do is: make an external list of colliding pairs of objects and update it every frame. So you can compare if the pair of colliding objects is already in the list, so you have continuous contact, otherwise they are just collided. Other data about collision you can get from btPersistentManifoldobject. For keeping material data of the object you can create external wrapper for every physics body like:

struct PhysicsBody
{
     btRigidBody const * const pBody;
     enum Material                    m_Mat;
};



#5007120 Calculating In-Game map positions

Posted by DgekGD on 04 December 2012 - 10:45 AM

One of the approaches is to make two more render passes for your scene. The first new one would render to texture everything from the camera which is located in the sky. In the second new render pass you render this texture to the place on the screen you want.


#5005824 Accessing other game objects

Posted by DgekGD on 30 November 2012 - 02:22 PM

You probably need some event manager in your game.
Just consider there is global instance of Event Manager, which can send different sorts of events in your game. Every game object has corresponding listener for the events (all of them are inherited from some abstract EventListener class).

If your enemy moved for example you send event(this is how it looks in my engine):
     g_EventManager::CallEvent( Event_Enemy_Moved(enemyId, new_position));

All listeners added to the manager will try to handle this event type. If some listener cares about the event, you can write your code to handle it. You can differentiate event types by unique static field in each event type.


#5005444 RigidBody Rotation

Posted by DgekGD on 29 November 2012 - 02:42 PM

For example, if you use xna math library, you can create XMMATRIX using XMMatrixRotationRollPitchYaw method.

Get current physics object transform:
XMMATRIX transform = btTransform_to_XMMATRIX(m_pRigidBody->getWorldTransform() ); //get basis of world transformation

Create rotation you need:
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(angle, angle, angle);

//Get final transform
btTransform finalTransform = XMMATRIX_to_btTansform(transform * rotation);

Finally, apply your transform
m_pRigidBody->setWorldTransform(finalTransform);

Here, take a look at conversion methods:
static btTransform XMMATRIX_to_btTransform( XMMATRIX const & mat )
{
// convert from XMMATRIX to btTransform (Bullet Physics)
btMatrix3x3 bulletRotation;
btVector3 bulletPosition;

// copy rotation matrix
for ( int row=0; row<3; ++row )
  for ( int column=0; column<3; ++column )
   bulletRotation[row][column] = mat.m[column][row];

// copy position
for ( int column=0; column<3; ++column )
  bulletPosition[column] = mat.m[3][column];
 
return btTransform( bulletRotation, bulletPosition );
}
static XMMATRIX btTransform_to_XMMATRIX( XMMATRIX const & trans )
{
XMMATRIX returnValue;
// convert from btTransform (BulletPhysics) to XMMATRIX
btMatrix3x3 const & bulletRotation = trans.getBasis();
btVector3 const & bulletPosition = trans.getOrigin();

// copy rotation matrix
for ( int row=0; row<3; ++row )
  for ( int column=0; column<3; ++column )
   returnValue.m[row][column] = bulletRotation[column][row];

// copy position
for ( int column=0; column<3; ++column )
  returnValue.m[3][column] = bulletPosition[column];
 
return returnValue;
}


Take a note, that XMMATRIX is in row-major format and btTransform is column-major, that's why you swap column/row in these methods!


#5005293 RigidBody Rotation

Posted by DgekGD on 29 November 2012 - 08:16 AM

I'm using bullet physics for my game and this is how I rotate character in the game.

  btMatrix3x3 orn = m_pRigidBody->getWorldTransform().getBasis(); //get basis of world transformation
  orn *= btMatrix3x3(btQuaternion(axis, radians));     //Multiply it by rotation matrix
 m_pRigidBody->getWorldTransform().setBasis(orn); //set new rotation for the object

m_pRigidBody is an object of btRigidBody*. To represent rotation all you need is 3x3 matrix (4th row and column are to enable translation). So you get basis of world transform and then multiply it by rotation matrix( created with the help of quaternion, where the axis is axis of rotation and radians is angle in radians). After that all you have to do is to update basis of your world transform.
For more information about quaternions I would recommend visiting this link.

I hope it helps;)


#5002962 Two rectangles colliding

Posted by DgekGD on 21 November 2012 - 12:00 PM

You can use Dot Product for this purpose.

Consider PlayerDir as a vector pointing in direction the player looks and EnemyDir is the direction in which an enemy moves.

Using following calculation :
     (PlayerDir.x * EnemyDir.x + PlayerDir.y * EnemyDir.y);


you get the value of cosine between their directions. Importantly, make sure that PlayerDir and EnemyDir are normalized! Otherwise you have to divide your result by length of both vectors!

If result was positive it means, that player and enemy moved in the same direction, so the player was hit in the back. If negative than, they moved in opposite directions. If you get value really close to 0, it means the player was hit from the side.


PARTNERS