• Create Account

We're offering banner ads on our site from just \$5!

# too_many_stars

Member Since 29 May 2013
Offline Last Active Nov 05 2014 12:45 PM

### continous velocity aligned rotation problem

05 November 2014 - 12:31 AM

Hi Guys,

I am having some problems with my enemy class. The world is 2d, with enemy agents running around. I need every update step for the enemies to orient themselves to their normalized velocity vector called "heading".

here is the rotation algorithm

```void rot(float angle){
for(int i=0;i<points.size();i++){
float x=((points[i].x-center.x)*cos(PI/180.0f*angle))-((center.y-points[i].y)*sin(PI/180.0f*angle))+center.x;
float y=((points[i].y-center.y)*cos(PI/180.0f*angle))-((points[i].x-center.x)*sin(PI/180.0f*angle))+center.y;
points[i].x=x;
points[i].y=y;
}
}

```

Here is part of the enemy constructor

```velocity.x*=speed;
velocity.y*=speed;
rot(rotation);
```

```void setHeading(){
}
```

and lastly, the enemy update

```void update(double dt){

for(int i=0;i<3;i++)//each enemy is a triangle
points[i]+=velocity*dt/1000.0f;
center+=velocity*dt/1000.0f;//center of the enemy

//NEED TO HANDLE AUTONOMOUS ROTATION HERE BASED ON NEW VELOCITY
//currently the velocity is constant to keep the func simple to read

}
```

Everything is set correctly in the constructor, with respect to the initial rotation, however, the issue is in the update method. Since the roation algo is a function of center, all vertecis, and angle, if it's constantly updated the object keeps spinning except for the case of 0 angle.

What i need is for the enemy update method to as smoothly as possible rotate the enemy if I apply impulses, forces etc to the object in the direction of the new normalized velocity, preferably polling each update step for the most accurate result.

Could someone please describe, and or show how I can implement a rotation algo which will in real time align the orientation of the enemy object with velocity?

Thanks,

Mike

22 September 2014 - 11:35 AM

Hi guys,

Having some issues with vertex rotation. Each vertex is just a 2d vector with an x,y component. Currently the shape is a simple rectangle, which is meant to rotate aroun its center

```void update(float dt,float angle){

/*
x = ((x - x_origin) * cos(angle)) - ((y_origin - y) * sin(angle)) + x_origin
y = ((y_origin - y) * cos(angle)) - ((x - x_origin) * sin(angle)) + y_origin
*/

v1.x=((v1.x-center.x)*cos(angle))-((center.y-v1.y)*sin(angle))+center.x;
v1.y=((center.y-v1.y)*cos(angle))-((v1.x-center.x)*sin(angle))+center.y;

v2.x=((v2.x-center.x)*cos(angle))-((center.y-v2.y)*sin(angle))+center.x;
v2.y=((center.y-v2.y)*cos(angle))-((v2.x-center.x)*sin(angle))+center.y;

v3.x=((v3.x-center.x)*cos(angle))-((center.y-v3.y)*sin(angle))+center.x;
v3.y=((center.y-v3.y)*cos(angle))-((v3.x-center.x)*sin(angle))+center.y;

v4.x=((v4.x-center.x)*cos(angle))-((center.y-v4.y)*sin(angle))+center.x;
v4.y=((center.y-v4.y)*cos(angle))-((v4.x-center.x)*sin(angle))+center.y;

}
```

When I run it, it just hangs up, and displays nothing. Any help would be greatly appreciated.

Thanks

### SDL 1.2, moving window, objects falling through tiles

12 June 2014 - 10:08 AM

Hi Guys,

As the tilte suggests, I am using SDL 1.2, with a tiled world. Whenever I move the game window around however, especially up and down, all my game entities start falling through the tiles. Does anyone have any idea how to solve this problem? It's been bothering me for a long time and I really need to learn how to deal with it.

Thanks,

Mike

### openGL, rotation, collision

03 June 2014 - 02:19 PM

Hi Guys,

Have a bit a canudrum. I have lines made up of a vector called 'start' and 'end'

These lines are in an array which form a rectangle, which is then rotated about the center of the screen thus...

```void showLine(){
glTranslatef(SCREEN_WIDTH/2,SCREEN_WIDTH/2,0.0f);
glRotatef(rot,0.0f,0.0f,1.0f);
glTranslatef(-SCREEN_WIDTH/2,-SCREEN_WIDTH/2,0.0f);
glBegin(GL_LINES);
glColor3f(1.0f,0.5f,0.3f);
glVertex2f(start.x,start.y);
glVertex2f(end.x,end.y);

glEnd();

}//this is only one line segment
```

I have a collision function (line segment vs circle) all figured out, but as I loop through the simulation, i am not sure how to update my two line vectors, start.x, start.y, end.x, end.y with GL's rotation algorithm. I need to find some kind of relationship.

If you know how to solve this problem please let me know.

Thanks,

Mike

### Old OpenGL movement problem

27 May 2014 - 10:00 PM

Hi Guys,

Having an issue with OpenGL.

Here's my update function

```	void update(float dt){

glTranslatef(pos.x,pos.y,pos.z);

pos.z+=vel.z*dt/1000.0f;

glCallList(object);
}
```

The idea is simple, move a ball, along the z axis with the help of a V3 class I created wich has float x,y,z with all the requisite operations such as addition, normailization etc.

The issue is, when I initialize vel.z=20.0f, the ball does not move, however when I do the following...

```	void update(float dt){

glTranslatef(pos.x,pos.y,pos.z);

pos.z+=20*dt/1000.0f;//change here

glCallList(object);
}
```

the ball does move. I am completely baffled as I have done this numerous times in SDL and the objects move just fine.

I think the issue has to do something with either glLoadIdentity() or glTranslatef().

If anyone has any ideas please let me know.

Thanks,

Mike

PARTNERS