• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Rotating mesh till it look at another mesh

21 posts in this topic

How can I make a mesh rotate till it LOOK at another mesh?

For example: If I have a tank mesh and I want it till it look at a human or a building, how can I do that?

I am using DirectX 9 (C++)
0

Share this post


Link to post
Share on other sites
edit: missed the question completly lol

Get the position of the human and and the position of the tank, subtract the two to get a vector pointing from the human to the tank, use that vector to rotate the human. Edited by Tispe
0

Share this post


Link to post
Share on other sites
I have to only rotate the tank on "Yaw", here is what I got:

D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);
D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);
D3DXVECTOR3 distance = dest - src;

Now, how can I use D3DXMatrixRotationYawPitchRoll() to do the actual rotation towards the other mesh?
0

Share this post


Link to post
Share on other sites
Assume the X and Y component of the vector calculated is the horizontal plane simply use the tangent on the two components and you should get the Yaw angle of the vector.
0

Share this post


Link to post
Share on other sites
@Tispe: I am not sure about what you mean, can you please complete the following code to make the rotation:

[left][background=rgb(250, 251, 252)]D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);[/background][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 distance = dest - src;[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXMatrixRotationYawPitchRoll(&matRotation, Yaw, Pitch, Roll); [/background][/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]// Change this line for rotation[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]Thanks,[/background][/size][/font][/color][/left] Edited by Medo3337
0

Share this post


Link to post
Share on other sites
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXMatrixRotationYawPitchRoll(&matRotation, [b]atan(distance.y/distance.x)[/b], Pitch, Roll);[/background][/size][/font][/color]

[color=#282828][font=helvetica,arial,verdana,tahoma,sans-serif][size=3][background=rgb(250, 251, 252)]make sure distance.x is greater then zero unless you want to divide by zero [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img][/background][/size][/font][/color] Edited by Tispe
0

Share this post


Link to post
Share on other sites
[left][background=rgb(250, 251, 252)]I tried:[/background][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 distance = dest - src;[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]D3DXMatrixRotationYawPitchRoll(&matRotation, atan(distance.y/distance.x)[/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3], 0.0f, [/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]0.0f[/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]);[/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][background=rgb(250, 251, 252)][size=3]It doesn't rotate at all.[/size][/background][/font][/color][/left] Edited by Medo3337
0

Share this post


Link to post
Share on other sites
Do you device->SetTransform(D3DTS_WORLD, &[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]matRotation[/size][/font][/color]); ?

Are the meshes moving? Might be that it rotates but you dont see it as the model might be 90 degrees off or something.

btw, you gotta check which quadrant the [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]distance vector is in and apply rules if your using this method.[/background][/size][/font][/color]
0

Share this post


Link to post
Share on other sites
Of course I am using SetTransform(), and both meshes could be moving or standing.

The code is similar to the following:
D3DXVECTOR3 dest(m_entity[3]->posX, m_entity[3]->posY, m_entity[3]->posZ); // Box
D3DXVECTOR3 src(m_entity[5]->posX, m_entity[5]->posY, m_entity[5]->posZ); // Tank
D3DXVECTOR3 distance = dest - src;
rotX = atan(distance.y / distance.x);
D3DXMatrixRotationYawPitchRoll(&matRotation, D3DXToRadian(rotX), D3DXToRadian(rotY), D3DXToRadian(rotZ));

The tank doesn't even rotate no matter where I move the box.

The tank is not moving, I am only moving the box and trying to make the tank rotate till it look at the box. Edited by Medo3337
0

Share this post


Link to post
Share on other sites
Hi.

What I found I needed to do in this case was I first needed a direction vector for how the mesh was facing when exported this is your refference direction.
d3vector3 refdir(1.0f, 0.0f, 0.0f);//the direction the mesh is exported in(in this case facing the +x direction);


What you do next is using the objects current position in the world and the target(what you want to look at).

d3vector3 heading = obj->pos - target;//play with this it may need to be the other way around.

[size="2"]D3DXVec3Normalize(&heading, &heading);[/size]

[size="2"][size="2"][color="#008000"][size="2"][color="#008000"]//first determine the angle between the heading vector and the target[/color][/size][/color][/size][/size]
[size="2"][size="2"]FLOAT dot = D3DXVec3Dot(&heading, &target);[/size][/size]
[size="2"][size="2"]dot = Clamp( dot, - 1.0f, 1.0f ); //just clamps it with in 2 ranges -1 and 1[/size][/size]

[size="2"][size="2"][color="#0000ff"][size="2"][color="#0000ff"]double[/color][/size][/color][/size][size="2"] Angle = acos(dot);[/size][/size]

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]int [/color][/size][/color][/size][size="2"]s = Sign(heading, target);[/size]

[size="2"] //im rotating on the Y[/size]
[size="2"]D3DXMatrixRotationY(&Rotation, Angle * s);[/size]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]int [/color][/size][/color][/size][size="2"]Sign([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]const[/color][/size][/color][/size][size="2"] D3DXVECTOR3& v1, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]const[/color][/size][/color][/size][size="2"] D3DXVECTOR3& v2)[/size]
[size="2"]{[/size]
//only good in 2d
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"] (v1.z*v2.x > v1.x*v2.z)[/size]
[size="2"]{ [/size]

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] -1;[/size]
[size="2"]}[/size]

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]else[/color][/size][/color][/size]
[size="2"]{[/size]

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] 1;[/size]
[size="2"]}[/size]

}
[size="2"]this is one way, may not be the best but it should get you going in the right directions.[/size]
0

Share this post


Link to post
Share on other sites
damn it cat edit my post there is a error in this line
[size="2"]FLOAT dot = D3DXVec3Dot(&heading, &target);[/size]
[size="2"]change it to[/size]
[size="2"][size="2"]FLOAT dot = D3DXVec3Dot(&heading, &reffdir);[/size][/size]
0

Share this post


Link to post
Share on other sites
Unfortuantely, I didn't get it to work well, I am using the following code:


D3DXVECTOR3 dest(m_entity[3]->posX, m_entity[3]->posY, m_entity[3]->posZ); // Box
D3DXVECTOR3 src(m_entity[5]->posX, m_entity[5]->posY, m_entity[5]->posZ); // Tank

D3DXVECTOR3 refdir(1.0f, 0.0f, 0.0f); //the direction the mesh is exported in(in this case facing the +x direction);

D3DXVECTOR3 heading = src - dest; //play with this it may need to be the other way around.

D3DXVec3Normalize(&heading, &heading);

//first determine the angle between the heading vector and the target
FLOAT dot = D3DXVec3Dot(&heading, &refdir);
dot = clamp( dot, - 1.0f, 1.0f ); //just clamps it with in 2 ranges -1 and 1

double Angle = acos(dot);

int s = Sign(heading, dest);

//im rotating on the Y
D3DXMATRIX matCRot;
float rot = Angle * s;
D3DXMatrixRotationY(&matCRot, rot);
0

Share this post


Link to post
Share on other sites
that should face the tank to the target whats it doing.
you will need to clap the angle so you can increament it, if you want to rotate slowll towards the target
0

Share this post


Link to post
Share on other sites
The problem is it doesn't rotate the tank towards the box at all, I tried to move the box but the tank doesn't look at it.
0

Share this post


Link to post
Share on other sites
Make sure rotations are working by setting the rotation to a value that increases every frame

[code]
float f = 0;

void EveryFrame()
{
f += 0.05f;
D3DXMATRIX matrix;
D3DXMatrixRotationY(&matrix, f);
pd3dDevice->SetTransform(D3DTS_WORLD, &matrix);
pd3dDevice->Present();
}
[/code]

Then you know whether the problem is to do with updating the wrong matrix or something
0

Share this post


Link to post
Share on other sites
@hupsilardee: I am pretty sure that I don't have that problem

The problem is with the rotation code not with giving the device the matrix, If I put the line 3DXMatrixRotationY(&matrix, 100.0f); it WILL rotate, and with the code that ankhd provided it DOES rotate but not looking directly at the other mesh.
0

Share this post


Link to post
Share on other sites
If my code rotates the tank and your using the refangle I set then may be the tank is not exported facing the x + direction first find what way the tank is exported first. That should be where the error is.
0

Share this post


Link to post
Share on other sites
@ankhd: The tank is exported as .x file and it's looking at the "front" direction, I am trying to make the tank look at the box DIRECTLY no matter where is the box located, maybe you haven't tested the code.
-1

Share this post


Link to post
Share on other sites
I dont think there is a front I only know X Y Z find what wat the mesh is and then get back dont just say fron , back left or right.
0

Share this post


Link to post
Share on other sites
@ankhd: In 3Ds Max were the mesh was created, there are 4 viewports, when I look at the "Front" viewport, I see the tank looking at me, when I look at left or right viewport I see the left and the right sides of the tank and when I look at the "Back" viewport I see the back side of the tank, thats what I am trying to explain. Edited by Medo3337
0

Share this post


Link to post
Share on other sites
ok now do this for us so we can help you.

first take all rotations from the tank, place some markers in the +x and + z and +y within your own 3d world, load your tank see what direction its pointing.
And yes its working in my code, my unit faces the target.(my world plane is x z, y up) if your worlds coord not match change the x,z, y params around(and thats enough guessing for me).
0

Share this post


Link to post
Share on other sites
I am not sure what you mean, but I think my 3D world is working right, here is what happens:

Camera position:
m_vEyePt = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
m_vLookAtPt = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
m_vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
m_vRight = D3DXVECTOR3(0.0f, 1.0f, 0.0f);

X, Y and Z:

Y+ move the mesh up towards the sky
Y- move the mesh down towards the ground

X+ move the mesh to the left
X- move the mesh to the right

Z+ move the mesh to back
Z- move the mesh to front

Of course if I moved the camera position the above will not be the same.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0