Sign in to follow this  
BroderickCalpe

Opengl Rotate from and to a certain point

Recommended Posts

BroderickCalpe    231

Hi!

 

I'm playing around with rotations and would like to know how this could be solve in a good way.

 

I would like to rotate something from a starting angle to a and angle.

Let's say from 120 degrees to 360 degrees. So when I reach 360 it should stop rotating.

Probably simple but I can't get it right.

 

void update(float elapsedTime)
{
  //my rotation goes here

}

 

 

Share this post


Link to post
Share on other sites
jellyfishchris    300

Wrote this at work, no idea if it compiles or if my logic is even close to correct (spent like 5minutes on it lol)

 

#ifndef ROTATE_H
#define ROTATE_H

class PerformRotate
{
public:
	PerformRotate(float start, float goal);
	bool Update(float dt);
	

	float GetCurrent(){return m_current;}
private:
	float m_start;
	float m_goal;
	float m_current;
};
#endif


#include "Rotate.h"

PerformRotate::PerformRotate(float start, float goal)
{
	m_start = start;
	m_goal = goal;
	m_current = start;
}
bool PerformRotate::Update(float dt)
{
	float difference = m_goal - start;
	float other = 360 - abs(difference);
	
	if(abs(other) > abs(difference))
	{
		m_current += difference * dt;
		
		if((m_current >= m_goal) && (difference * dt > 0))
			return true;
		else if((m_current <= m_goal) && (difference * dt < 0))
			return true;
	}
	else
	{
		m_current += other * dt;
		
		if((m_current >= m_goal) && (other * dt > 0))
			return true;
		else if((m_current <= m_goal) && (other * dt < 0))
			return true;
	}
	return false;
}

Share this post


Link to post
Share on other sites
hikarihe    243

can you explain more clearly? I could help you but I don't really understand what you need to do

 

what does your code look like now?

 

in most cases you can use glRotatef( [your rotation value], [x coordinate], [y coordinate], [z coordinate] ) before you draw 

 

> where xyz are the coordinates of the point to rotate around & rotation value is in degrees

Edited by hikarihe

Share this post


Link to post
Share on other sites
incertia    787
So this is going to be pretty general, but I'm sure you can find out a way to do it in code.

You want to rotate from an angle, say theta, to a different angle, say phi, in a direction d (1 for CCW, -1 for CW) in a given time, say t, along the axis of your choice about a point (x, y, z). Your angular velocity then has to be [eqn] \omega = d \left| \dfrac{\theta - \phi}{t \cdot \mathrm{FPS}} \right| [/eqn] about that axis, which gives you the angle you must rotate per frame to reach your goal in your goal time. However, FPS can change so you must recalculate omega accordingly.

So you now know how fast you're rotating the object, but you're not quite sure how to get to rotate about a certain point. What you do know is that OpenGL provides support for rotating about the origin, so you can subtract (x, y, z) from your point P to center your rotation around the origin, rotate, and then add back (x, y, z). As such, we our transform becomes P = Rotate(P - (x, y, z), omega) + (x, y, z), and you're done.

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