www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Align object to vector using Dgame framework

reply "codenstuff" <nikolapav1985 gmail.com> writes:
I've been using Dgame framework for simple simulations.

I need moving object to be aligned to their velocity vectors.

How is it possible to do that using Dgame?

I see that shape object has setRotation and setRotationCenter. 
Not sure how to use these to achieve the effect. I realize that 
default is rotation around origin. This causes objects to drift 
over time.

Sample code

struct GameObject {
Point **position;
// array of pointers to object points
Point *acceleration;
Point *velocity;
double max_speed;
double max_force;
}

shape = new Shape(Geometry.Quads,
Vertex(object.position[0].x, object.position[0].y),
Vertex(object.position[1].x, object.position[1].y),
Vertex(object.position[2].x, object.position[2].y),
Vertex(object.position[3].x, object.position[3].y))

// rotate shape to face velocity here
shape.move(object.velocity.x, object.velocity.y);
Jul 13 2015
parent reply =?UTF-8?B?Ik3DoXJjaW8=?= Martins" <marcioapm gmail.com> writes:
On Monday, 13 July 2015 at 16:11:03 UTC, codenstuff wrote:
 I've been using Dgame framework for simple simulations.

 I need moving object to be aligned to their velocity vectors.

 How is it possible to do that using Dgame?

 I see that shape object has setRotation and setRotationCenter. 
 Not sure how to use these to achieve the effect. I realize that 
 default is rotation around origin. This causes objects to drift 
 over time.

 Sample code

 struct GameObject {
 Point **position;
 // array of pointers to object points
 Point *acceleration;
 Point *velocity;
 double max_speed;
 double max_force;
 }

 shape = new Shape(Geometry.Quads,
 Vertex(object.position[0].x, object.position[0].y),
 Vertex(object.position[1].x, object.position[1].y),
 Vertex(object.position[2].x, object.position[2].y),
 Vertex(object.position[3].x, object.position[3].y))

 // rotate shape to face velocity here
 shape.move(object.velocity.x, object.velocity.y);
This is not really a game development forum but here you go: auto rads = atan2(object.velocity.y, object.velocity.x); // returns radians auto degs = angle * (180.0f / PI); // convert to degrees shape.rotate(degs); I suggest you store velocity as a Vector2f to avoid later confusion. It is really useful to learn basic linear algebra, and trigonometry as a game developer - you will be needing these tools on a daily basis.
Jul 13 2015
parent reply "codenstuff" <nikolapav1985 gmail.com> writes:
On Monday, 13 July 2015 at 19:07:55 UTC, Márcio Martins wrote:
 On Monday, 13 July 2015 at 16:11:03 UTC, codenstuff wrote:
 [...]
This is not really a game development forum but here you go: auto rads = atan2(object.velocity.y, object.velocity.x); // returns radians auto degs = angle * (180.0f / PI); // convert to degrees shape.rotate(degs); I suggest you store velocity as a Vector2f to avoid later confusion. It is really useful to learn basic linear algebra, and trigonometry as a game developer - you will be needing these tools on a daily basis.
This does not work as expected. It keeps rotating object as long as vector is at angle. Any idea how to improve on this?
Jul 13 2015
parent =?UTF-8?B?Ik3DoXJjaW8=?= Martins" <marcioapm gmail.com> writes:
On Tuesday, 14 July 2015 at 00:31:56 UTC, codenstuff wrote:
 On Monday, 13 July 2015 at 19:07:55 UTC, Márcio Martins wrote:
 On Monday, 13 July 2015 at 16:11:03 UTC, codenstuff wrote:
 [...]
This is not really a game development forum but here you go: auto rads = atan2(object.velocity.y, object.velocity.x); // returns radians auto degs = angle * (180.0f / PI); // convert to degrees shape.rotate(degs); I suggest you store velocity as a Vector2f to avoid later confusion. It is really useful to learn basic linear algebra, and trigonometry as a game developer - you will be needing these tools on a daily basis.
This does not work as expected. It keeps rotating object as long as vector is at angle. Any idea how to improve on this?
I'm sorry. You should use setRotation() instead, rotate() just adds to the rotation.
Jul 14 2015