Jump to content

  • Log In with Google      Sign In   
  • Create Account

DrunkKong

Member Since 03 May 2011
Offline Last Active Apr 29 2012 04:33 PM

Posts I've Made

In Topic: Found the problem, but no the solution, help again

29 April 2012 - 02:49 AM

Thanks again ApochPiQ, the thing is that this is a test.. they have give me a non working game that needs to be repair.
I get stuck into this point... everything is working well till the m_Contact is call (that's what show in the watch), then all values comes wrong.
So i was thiniking maybe there is a problem with that value.

In Topic: Found the problem, but no the solution, help again

28 April 2012 - 04:41 PM

#include "Entity.h"
#include <wchar.h>
#include "float.h"
#include "Graphics/RenderItem.h"
using Sfas::Game::Entity;
Entity::Entity( int id ) : m_Position( 0.0f, 0.0f, 0.0f ),
	   m_Velocity( 0.0f, 0.0f, 0.0f ),
	   m_Acceleration( 0.0f, 0.0f, 0.0f ),
	   m_LastFrameAcceleration( 0.0f, 0.0f, 0.0f ),
	   m_ForceAccumulator( 0.0f, 0.0f, 0.0f ),
	   m_Scale( 1.0f, 1.0f, 1.0f ),
	   m_ContactNormal( 0.0f, 0.0f, 0.0f ),
	   m_ContactPoint( 0.0f, 0.0f, 0.0f ),
	   m_Movement( 0.0f, 0.0f, 0.0f ),
	   m_Contact( 0 ),
	   m_Radius( 0.0f ),
	   m_Damping(0.99f),
	   m_InverseMass(0.0f),
	   m_TimeActive(0.0f),
	   m_Restitution(0.0f),
	   m_Penetration(0.0f),
	   m_Friction(0.0f),
	   m_Tolerance(0.0f),
	   m_ID( id ),
	   m_Active(false),
	   m_Collision(false)
{
}
Entity::Entity( int id, float scale, float damping ) : m_Position( 0.0f, 0.0f, 0.0f ),
			  m_Velocity( 0.0f, 0.0f, 0.0f ),
			  m_Acceleration( 0.0f, 0.0f, 0.0f ),
			  m_LastFrameAcceleration( 0.0f, 0.0f, 0.0f ),
				   m_ForceAccumulator( 0.0f, 0.0f, 0.0f ),
			  m_Scale( scale, scale, scale ),
			  m_ContactNormal( 0.0f, 0.0f, 0.0f ),
			  m_ContactPoint( 0.0f, 0.0f, 0.0f ),
			  m_Movement( 0.0f, 0.0f, 0.0f ),
			  m_Contact( 0 ),
			   m_Radius( 0.0f ),
			  m_Damping(damping),
			  m_InverseMass(0.0f),
			  m_TimeActive(0.0f),
			  m_Restitution(0.0f),
			  m_Penetration(0.0f),
			  m_Friction(0.0f),
			  m_Tolerance(0.0f),
			  m_ID( id ),
			  m_Active(false),
			  m_Collision(false)
{
}
Entity::Entity( int id, const D3DXVECTOR3& pos, float scale, float damping ) : m_Position(pos),
				  m_Velocity( 0.0f, 0.0f, 0.0f ),
				  m_Acceleration( 0.0f, 0.0f, 0.0f ),
				  m_LastFrameAcceleration( 0.0f, 0.0f, 0.0f ),
				  m_ForceAccumulator( 0.0f, 0.0f, 0.0f ),
				  m_Scale( scale, scale, scale ),
					 m_ContactNormal( 0.0f, 0.0f, 0.0f ),
				  m_ContactPoint( 0.0f, 0.0f, 0.0f ),
				  m_Movement( 0.0f, 0.0f, 0.0f ),
				  m_Contact( 0 ),
				  m_Radius( 0.0f ),
				  m_Damping(damping),
				  m_InverseMass(0.0f),
				  m_TimeActive(0.0f),
				  m_Restitution(0.0f),
				  m_Penetration(0.0f),
				  m_Friction(0.0f),
				  m_Tolerance(0.0f),
				  m_ID( id ),
				  m_Active(false),
				  m_Collision(false)
{
}
Entity::Entity( int id, const D3DXVECTOR3& pos, const D3DXVECTOR3& scale, float damping ) : m_Position(pos),
				  m_Velocity( 0.0f, 0.0f, 0.0f ),
				  m_Acceleration( 0.0f, 0.0f, 0.0f ),
				  m_LastFrameAcceleration( 0.0f, 0.0f, 0.0f ),
				  m_ForceAccumulator( 0.0f, 0.0f, 0.0f ),
				  m_Scale( scale ),
				  m_ContactNormal( 0.0f, 0.0f, 0.0f ),
				  m_ContactPoint( 0.0f, 0.0f, 0.0f ),
				  m_Movement( 0.0f, 0.0f, 0.0f ),
				  m_Contact( 0 ),
				  m_Radius( 0.0f ),
				  m_Damping(damping),
				  m_InverseMass(0.0f),
				  m_TimeActive(0.0f),
				  m_Restitution(0.0f),
				  m_Penetration(0.0f),
				  m_Friction(0.0f),
				  m_Tolerance(0.0f),
				  m_ID( id ),
				  m_Active(false),
				  m_Collision(false)
{
}

Entity::~Entity(void)
{
}
void Entity::Render( Engine::RenderItem * drw )
{
if( m_Active )
{
  D3DXMATRIX world;
  D3DXMATRIX move;
  D3DXMATRIX scale;
  D3DXMatrixTranslation( &move, m_Position.x, m_Position.y, m_Position.z );
  D3DXMatrixScaling( &scale, m_Scale.x, m_Scale.y, m_Scale.z );
 
  world = scale * move;
  drw->Draw( &world );
}
}
void Entity::Update( float dt )
{
if( m_Active )
{
  m_ForceAccumulator.y -= ( kGravity * GetMass() );
  LinearIntegration( dt );
  // Count this frame
  m_TimeActive += dt;
}
}
bool Entity::CheckForCollision( const Entity& other )
{
    float left1 = m_Position.x - ( m_Scale.x * 0.5f );
    float left2 = other.m_Position.x - ( other.m_Scale.x * 0.5f );
    float right1 = left1 + m_Scale.x;
    float right2 = left2 + other.m_Scale.x;
    float top1 = m_Position.y + ( m_Scale.y * 0.5f );
float top2 = other.m_Position.y + ( other.m_Scale.y * 0.5f );
    float bottom1 = top1 - m_Scale.y;
    float bottom2 = top2 - other.m_Scale.y;
    if (bottom1 > top2) return false;
    if (top1 < bottom2) return false;
    if (right1 < left2) return false;
    if (left1 > right2) return false;
// Create the contact information ready for resolution
D3DXVECTOR3 direction = m_Position - other.m_Position;
D3DXVec3Normalize( &direction, &direction );
m_ContactPoint = other.m_Position + ( direction * other.m_Radius );
if( other.m_Collision && top1 >top2 && bottom1 < top2 )
{
   m_Penetration = top2 - bottom1;
  m_ContactNormal = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
}
return true;
}
void Entity::OnCollision( Entity& other )
{
// Default behavour
SetActive( false );
}
void Entity::LinearIntegration( float dt )
{
// Don't Integrate invalid particles
if (m_InverseMass <= 0.0f)
{
  return;
}
// Update current position with velocity
m_Position += ( m_Velocity * dt );
// Calculate acceleration
    m_LastFrameAcceleration = m_Acceleration;
    m_LastFrameAcceleration += ( m_ForceAccumulator * m_InverseMass );
// Calculate velocity
m_Velocity += ( m_LastFrameAcceleration * dt );
// Damp the velocity to slow the object over time
m_Velocity *= pow(m_Damping, dt);
// Save and clear forces
m_ForceAccumulator.x = m_ForceAccumulator.y = m_ForceAccumulator.z = 0.0f;
}
void Entity::SetMass( float mass)
{
    m_InverseMass = ( 1.0f / mass );
}
float Entity::GetMass() const
{
    if( m_InverseMass == 0 )
    {
	    return FLT_MAX;
    }
    else
    {
	    return (1.0f / m_InverseMass );
    }
}
float Entity::CalculateSeparatingVelocity( Entity& other )
{
    D3DXVECTOR3 relativeVelocity = m_Velocity;
    relativeVelocity -= other.GetVelocity();
return D3DXVec3Dot(&relativeVelocity, &m_ContactNormal);
}
void Entity::Resolve(Entity& other, float duration)
{
m_Contact = &other;
    ResolveVelocity(duration);
    ResolveInterpenetration(duration);
}
void Entity::ResolveVelocity(float duration)
{
float separatingVelocity = 0.1f;
if( m_Contact != 0 )
{
  // Find the velocity in the direction of the contact
  separatingVelocity = CalculateSeparatingVelocity(*m_Contact);
}
    // Check if it needs to be resolved
    if (separatingVelocity > 0)
    {
	    // The contact is either separating, or stationary - there's
	    // no impulse required.
	    return;
    }
    // Calculate the new separating velocity
    float newSepVelocity = -separatingVelocity * m_Restitution;
    // Check the velocity build-up due to acceleration only
    D3DXVECTOR3 accCausedVelocity = m_LastFrameAcceleration;
    if (m_Contact != 0)
    {
	    accCausedVelocity -= m_Contact->GetAcceleration();
    }
		   
D3DXVECTOR3 normalThisFrame = m_ContactNormal * duration;
    float accCausedSepVelocity = D3DXVec3Dot( &accCausedVelocity, &normalThisFrame );
    // If we've got a closing velocity due to acelleration build-up,
    // remove it from the new separating velocity
    if (accCausedSepVelocity < 0)
    {
	    newSepVelocity += m_Restitution * accCausedSepVelocity;
	    // Make sure we haven't removed more than was
	    // there to remove.
	    if (newSepVelocity < 0)
	    {
		    newSepVelocity = 0;
	    }
    }
    float deltaVelocity = newSepVelocity - separatingVelocity;
    // We apply the change in velocity to each object in proportion to
    // their inverse mass (i.e. those with lower inverse mass [higher
    // actual mass] get less change in velocity)..
    float totalInverseMass = m_InverseMass;
    if (m_Contact != 0)
    {
	    totalInverseMass += m_Contact->m_InverseMass;
    }
    // If all particles have infinite mass, then impulses have no effect
    if (totalInverseMass <= 0)
    {
	    return;
    }
    // Calculate the impulse to apply
    float impulse = deltaVelocity / totalInverseMass;
    // Find the amount of impulse per unit of inverse mass
    D3DXVECTOR3 impulsePerIMass = m_ContactNormal * impulse;
    // Apply impulses: they are applied in the direction of the contact,
    // and are proportional to the inverse mass.
    m_Velocity = ( m_Velocity + impulsePerIMass * m_InverseMass );
    if( m_Contact != 0 )
    {
	    // Particle 1 goes in the opposite direction
	    m_Contact->m_Velocity = ( m_Contact->m_Velocity + impulsePerIMass * -m_Contact->m_InverseMass );
    }
}
void Entity::ResolveInterpenetration(float duration)
{
    // If we don't have any penetration, skip this step.
    if (m_Penetration <= 0)
    {
	    return;
    }
    // The movement of each object is based on their inverse mass, so
    // total that.
    float totalInverseMass = m_InverseMass;
    if ( m_Contact != 0 )
    {
	    totalInverseMass += m_Contact->m_InverseMass;
    }
    // If all particles have infinite mass, then we do nothing
    if (totalInverseMass <= 0)
    {
	    return;
    }
    // Find the amount of penetration resolution per unit of inverse mass
    D3DXVECTOR3 movePerIMass = m_ContactNormal * (m_Penetration / totalInverseMass);
    // Calculate the the movement amounts
    m_Movement = movePerIMass * m_InverseMass;
    if( m_Contact != 0 )
    {
  m_Contact->m_Movement = movePerIMass * -m_Contact->m_InverseMass;
    }
    // Apply the penetration resolution
    m_Position = ( m_Position + m_Movement );
    if( m_Contact != 0 )
    {
	    m_Contact->SetPosition(m_Contact->m_Position + m_Contact->m_Movement);
    }	   
}

Really, really appreciate all this help..

In Topic: Found the problem, but no the solution, help again

28 April 2012 - 04:23 PM

Entity::Entity( int id ) : m_Position( 0.0f, 0.0f, 0.0f ),
	   m_Velocity( 0.0f, 0.0f, 0.0f ),
	   m_Acceleration( 0.0f, 0.0f, 0.0f ),
	   m_LastFrameAcceleration( 0.0f, 0.0f, 0.0f ),
	   m_ForceAccumulator( 0.0f, 0.0f, 0.0f ),
	   m_Scale( 1.0f, 1.0f, 1.0f ),
	   m_ContactNormal( 0.0f, 0.0f, 0.0f ),
	   m_ContactPoint( 0.0f, 0.0f, 0.0f ),
	   m_Movement( 0.0f, 0.0f, 0.0f ),
	   m_Contact( 0 ),
	   m_Radius( 0.0f ),
	   m_Damping(0.99f),
	   m_InverseMass(0.0f),
	   m_TimeActive(0.0f),
	   m_Restitution(0.0f),
	   m_Penetration(0.0f),
	   m_Friction(0.0f),
	   m_Tolerance(0.0f),
	   m_ID( id ),
	   m_Active(false),
	   m_Collision(false)
{

This the Entity.cpp

Thanks very much..

In Topic: What's wrong in here please help

26 April 2012 - 03:16 PM

So the code looks ok?


there is a way to find the problem from null pointer

Thanks for super fast response

In Topic: Debug Assertion Failed:(L"Buffer is too small"&&0)

25 April 2012 - 02:56 AM

Thanks, wrong size of the buffer, as u said.

Really appreciate the super fast response.

PARTNERS