Question

Current Code:

//calculating View Project matrix
abfw::Matrix44 view_proj_matrix_ = camera_.GetMatrix(PROJ) * camera_.GetMatrix(VIEW);

//3D position
abfw::Vector3 ball_position_ =  balls_[look_at_index_].GetPosition();
abfw::Vector3 sprite_position;

    //transform world to screen ratio in rang -1 to 1
    ball_position_ = ball_position_.Transform(view_proj_matrix_);

    //move to range 0 - 2
    ball_position_.x += 1;

    //scale to range 0 -1
    ball_position_.x /= 2;

    //scale to screen size
    ball_position_.x *= platform_.width();

    // same as above
    ball_position_.y -= 1;
    ball_position_.y /= -2;
    ball_position_.y *= platform_.height();

    //set sprite position
    sprite_position = ball_position_;

    // z is discounted
    sprite_position.z = 0;

I'm getting x and y values that are way out of range of the screen but I'm not seeing why. I have broken the algorithm down as much as possible to try and see where I'm going wrong, but I can't.

Edit: when the camera is at (0,0,7) switching between balls on the same y value give the same sprite position.

GetMatrix() uses this code:

view_matrix_.LookAt(camera_eye_, camera_lookat_, camera_up_);
projection_matrix_ = platform_.PerspectiveProjectionFov(camera_fov_, (float)platform_.width() / (float)platform_.height(), near_plane_, far_plane_);

which is what I'm also using to draw with so I'm assuming it to be right.

using row vectors I believe.

Transform Code:

Vector3 result = Vector3(0.0f, 0.0f, 0.0f);

result.x = x*_mat.m[0][0]+y*_mat.m[1][0]+z*_mat.m[2][0]+_mat.m[3][0];
result.y = x*_mat.m[0][1]+y*_mat.m[1][1]+z*_mat.m[2][1]+_mat.m[3][1];
result.z = x*_mat.m[0][2]+y*_mat.m[1][2]+z*_mat.m[2][2]+_mat.m[3][2];

No correct solution

OTHER TIPS

I have no idea what the namespace abfw stands for, but if it handles vectors and matrices in the same way as DirectX, you have to swap projection and view transformations:

abfw::Matrix44 view_proj_matrix_ = camera_.GetMatrix(VIEW) * camera_.GetMatrix(PROJ);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top