• 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
Doublefris

Simple question concering rotation.

13 posts in this topic

Hello fellows of gamedev.net. I've been struggling with a bit of a problem on my AI.

 

Me and a friend are working on a space game. Our use-case is a basic kind of AI that will stop and turn to the player when the player comes near.
The problem as follows:

How do I find the (target) angular velocity that will rotate a ship towards a given point?

 

The knee-jerk solution would be to create a look-at rotation matrix, but this isn't an option. The ship must be rotated indirectly by changing it's target velocity. 

I have looked in the book "Artificial Intelligence for Games, Second Edition", from page 180 and on, however this example use quaternions, not matrices like we do.

 

I guess the basic idea would be to align the ships z-axis with the vector between the two ships, however I don't know how to achieve this( my math skills are lacking sad.png )

 

thanks in advance! 

Edited by Doublefris
0

Share this post


Link to post
Share on other sites
This should probably be moved to "Math & Physiscs".

Why don't you use quaternions for this computation? It's much easier to do that way.
0

Share this post


Link to post
Share on other sites

 I wrote this function for an aimbot project for a popular online game over a decade ago:

 

void VecToAngles( const float *forward, float *angles )
{
	float	tmp, yaw, pitch;
	
	if (forward[1] == 0 && forward[0] == 0)
	{
		yaw = 0;
		if (forward[2] > 0)
			pitch = 90;
		else
			pitch = 270;
	}
	else
	{
		yaw = (atan2(forward[1], forward[0]) * 180 / 3.14159);

		if (yaw < 0)
			yaw += 360;

		tmp = sqrt (forward[0]*forward[0] + forward[1]*forward[1]);
		
		pitch = (atan2(forward[2], tmp) * 180 / 3.14159);
	}
	
	angles[0] = pitch;
	angles[1] = yaw;
	angles[2] = 0;
}

 

 

 

 The rest is history.

0

Share this post


Link to post
Share on other sites

This should probably be moved to "Math & Physiscs".

Why don't you use quaternions for this computation? It's much easier to do that way.

 

The main reason is our code base doen't have support for quaternions as of now. Furthermore I have never used quaternions before and therefore am not really comfortable with them. Anyway, do you really think the only suitable solution would be quaternions?

0

Share this post


Link to post
Share on other sites

The main reason is our code base doen't have support for quaternions as of now. Furthermore I have never used quaternions before and therefore am not really comfortable with them. Anyway, do you really think the only suitable solution would be quaternions?

No, I don't think it's the only solution, but it's probably the easiest. If you stick to representing attitudes as matrices, figuring out the most natural transition between two attitudes involves some heavy math.
0

Share this post


Link to post
Share on other sites

How do I find the (target) angular velocity that will rotate a ship towards a given point?

You can calculate the target angular velocity vector by taking the cross product of the current direction and the desired direction, and scaling according to the desired rotation speed.  That leaves the question of the ship's roll when it reaches the destination, though - if you care about that, this problem becomes more complicated.

0

Share this post


Link to post
Share on other sites

How do I find the (target) angular velocity that will rotate a ship towards a given point?

You can calculate the target angular velocity vector by taking the cross product of the current direction and the desired direction, and scaling according to the desired rotation speed.  That leaves the question of the ship's roll when it reaches the destination, though - if you care about that, this problem becomes more complicated.

 

this is a very nice solution smile.png seems to be working well so far. As for the roll, I think it's fine to have spaceship gravitate towards an 'up' vector, or I'll just figure something out :)

Edited by Doublefris
0

Share this post


Link to post
Share on other sites

The main reason is our code base doen't have support for quaternions as of now. Furthermore I have never used quaternions before and therefore am not really comfortable with them. Anyway, do you really think the only suitable solution would be quaternions?

No, I don't think it's the only solution, but it's probably the easiest. If you stick to representing attitudes as matrices, figuring out the most natural transition between two attitudes involves some heavy math.

 

Say, if we were to implement quatornions, what would be the basic steps used to get an angular velocity? Find the angular displacement vector that is the lowest?

0

Share this post


Link to post
Share on other sites

Say, if we were to implement quatornions, what would be the basic steps used to get an angular velocity? Find the angular displacement vector that is the lowest?

You would take the logarithm of the ratio of the target attitude and the current attitude. This is a quaternion whose real part is 0; the other three components give you the angular velocity that, when applied for 1 second, will convert the current attitude into the target attitude.
#include <iostream>
#include <boost/math/quaternion.hpp>

typedef boost::math::quaternion<double> Q;

int main() {
  Q a(0.0, 1.0, 0.0, 0.0);
  Q b(0.0, 0.0, 1.0, 0.0);

  Q ratio = b / a;
  double scale = std::acos(ratio.R_component_1());
  Q log_of_ratio(0.0,
                 scale * ratio.R_component_2(),
                 scale * ratio.R_component_3(),
                 scale * ratio.R_component_4());

  std::cout << log_of_ratio << '\n';
}

Edited by Álvaro
0

Share this post


Link to post
Share on other sites

EDIT: actually nvm it, something else about my code was faulty ^^

Edited by Doublefris
0

Share this post


Link to post
Share on other sites

So I got it to work in XNA and it works beatifully, however using my own quaternion class or directx's quaternions the spaceship keeps 'flipping over' when you get behind on the Z axis it seems.  Is there something specific I'm doing wrong?

 

my basic algorithm is : 

 

 

Quaternion q1 = (shipforward, 0.0f)

Quaternion q2 = (shipposition - LookAtPosition, 0.0f)

 

then get the angular velocity using the method you described. weird thing is it works very well in XNA but trying to port it to my c++ engine keeps bringing this annoying bug.

 

A reason I thought of is that my division operator is wrong? what is the exact implementation of the division operator here?

Edited by Doublefris
0

Share this post


Link to post
Share on other sites

0.0f is simply the scalar( I guess real part) of the quaternion. 

 

the f suffix means its a float.

0

Share this post


Link to post
Share on other sites

What problems with flipping are you having?

 

If you set up a quaternion with just a vector part and the w part being 0 you get a 180 degree rotation around the axis specified in the imaginary part... probably not what you want.

 

If flipping means you interpolate "the long way round" you can get rid of that by doing a dot product (in 4d) of the 2 quaternions and negating one if the dot product is negative.

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