Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:53:40+00:00 2026-06-18T15:53:40+00:00

I wrote a generic matrix class for all of my 3d objects, but the

  • 0

I wrote a generic matrix class for all of my 3d objects, but the translations seem to be really off.

Here is my class:

class GenericMatrix
{
public:
    GenericMatrix();

    virtual void Identity();

    virtual void Scale( float x, float y, float z );
    virtual void Scale( const DirectX::XMFLOAT3& s );
    virtual DirectX::XMFLOAT3 Scaling( void );

    virtual void Translate( float x, float y, float z );
    virtual void Translate( const DirectX::XMFLOAT3& t );
    virtual DirectX::XMFLOAT3 Translations( void );

    virtual void Rotate( float x, float y, float z );
    virtual void Rotate( const DirectX::XMFLOAT3& r );
    virtual DirectX::XMVECTOR Rotations( void );
    virtual DirectX::XMFLOAT3 RotationsPYR( void );

    virtual DirectX::XMMATRIX Matrix( bool process = true );

    virtual operator DirectX::XMMATRIX()
    {
        return this->Matrix();
    }

    virtual void UpdateMatrix( void );

    DirectX::XMMATRIX   matrix;
    DirectX::XMFLOAT3   scale;
    DirectX::XMFLOAT3   translation;
    DirectX::XMVECTOR   rotation;
    bool                matrixNeedsUpdate;

protected:
    float               yaw, pitch, roll;
};

And the source:

#include "pch.h"
#include "GenericMatrix.h"

GenericMatrix::GenericMatrix()
    : matrixNeedsUpdate( true )
{
    this->Identity();
    this->pitch = 90.0f;
    this->yaw = 0.0f;
    this->roll = 0.0f;
}

void GenericMatrix::Identity()
{
    this->scale = DirectX::XMFLOAT3( 1.0f, 1.0f, 1.0f );
    this->translation = DirectX::XMFLOAT3( 0.0f, 0.0f, 0.0f );
    this->rotation = DirectX::XMQuaternionIdentity();
    this->pitch = 90.0f;
    this->yaw = 0.0f;
    this->roll = 0.0f;
    matrixNeedsUpdate = true;
}

void GenericMatrix::Scale( float x, float y, float z )
{
    this->scale.x += x;
    this->scale.y += y;
    this->scale.z += z;
    this->matrixNeedsUpdate = true;
}

void GenericMatrix::Scale( const DirectX::XMFLOAT3& s )
{ Scale( s.x, s.y, s.z ); }

DirectX::XMFLOAT3 GenericMatrix::Scaling( void )
{ return this->scale; }

void GenericMatrix::Translate( float x, float y, float z )
{
    this->translation.x += x;
    this->translation.y += y;
    this->translation.z += z;
    this->matrixNeedsUpdate = true;
}

void GenericMatrix::Translate( const DirectX::XMFLOAT3& t )
{ Translate( t.x, t.y, t.z ); }

DirectX::XMFLOAT3 GenericMatrix::Translations( void )
{ return this->translation; }

void GenericMatrix::Rotate( float x, float y, float z )
{
    pitch = x;
    yaw = y;
    roll = z;
    this->rotation = DirectX::XMQuaternionRotationRollPitchYaw( pitch, yaw, roll );
    this->matrixNeedsUpdate = true;
}

void GenericMatrix::Rotate( const DirectX::XMFLOAT3& r )
{ Rotate( r.x, r.y, r.z ); }

DirectX::XMVECTOR GenericMatrix::Rotations( void )
{ return this->rotation; }

DirectX::XMFLOAT3 GenericMatrix::RotationsPYR( void )
{
    return DirectX::XMFLOAT3( pitch, yaw, roll );
}

DirectX::XMMATRIX GenericMatrix::Matrix( bool process )
{
    if ( process && this->matrixNeedsUpdate )
    {
        UpdateMatrix();
        this->matrixNeedsUpdate = false;
    }
    return this->matrix;
}

void GenericMatrix::UpdateMatrix( void )
{
    DirectX::XMVECTOR scaleVec, translateVec;
    scaleVec = DirectX::XMLoadFloat3( &this->scale );
    translateVec = DirectX::XMLoadFloat3( &this->translation );
    this->matrix = DirectX::XMMatrixScalingFromVector( scaleVec ) * DirectX::XMMatrixRotationQuaternion( this->rotation ) * DirectX::XMMatrixTranslationFromVector( translateVec );
}

When I use this as a model matrix, after doing Identity(), then Translate( 0.0f, 0.0f, 5.0f ), my model becomes only visible when I position my camera at [0,0,5], and even then, it’s just a flicker. The last bit of 3d programming I have done was with OpenGL 1.x, so there is a good chance I’m doing something weird with the matrices, but haven’t found anything that would tell me how to do this otherwise. If there is more information anyone needs, just ask.

update: Some more details – if I set the matrix to Identity() and leave it, i can move the camera and see the model without any problem from any position, meanwhile if I move it anywhere, it messes up the visibility.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:53:41+00:00Added an answer on June 18, 2026 at 3:53 pm

    I discovered the problem after a few tests. It seems I was using a left-handed coordinate system for my camera, but right-handed for my perspective. In between z 0.9 to -0.9, both left and right handed systems were able to agree that the model should be visible, but when I moved them out of that space, neither matrix could agree on what was visible unless my camera was exactly at the same coordinates.

    Moral of the story – remember to consistently use the same coordinate system throughout your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a generic EventArgs class in my VB.NET solution: Public Class EventArgs(Of T)
I need to write a generic NamedQuery; such as, find me all the objects
How I can return generic type from method? I wrote like this, but it
I wrote a non-static Generic class instantiator for my Abstract Factory design, and use
I am using a System.Collections.Generic , which contains instances of a class I wrote.
I wrote a really simple ViewModel Locator : public static readonly DependencyProperty LocateForProperty =
I wrote a generic class and want to crate its instance from a static
To get fully qualified path of application I have wrote a function: public class
I wrote a quick generic linked list, simple stuff. But I have a bug
I found myself needing a simple generic interface, and I wrote it, but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.