Jump to content
  • Advertisement
Sign in to follow this  
GroZZleR

2D Rotations [Updated: Full Source - Unsolved]

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Matrices represent transformations. You can think of them for your purposes like a pair of functions that take in your (x,y) and spit out (x',y'), which you can think of as an equivalent point in a different coordinate space.

It sounds to me like you are giving the screen coordinates of the sprite's center and attempting to rotate about that. However, you want to define a transform from the sprite's coordinate space to screen space, and if your sprite is 32x32, its center is at (16,16) in its coordinate space.

Share this post


Link to post
Share on other sites
Hey all,

Still having the issues with my 2D rotations. I'm going to include full source this time, and hopefully someone will be able to peg the issue. I sure as hell can't, I've moved the source all around, tried setting all sorts of starting matrix values - nothing seems to work.

Here's the most relevant source:

_device.Clear(Direct3D.ClearFlags.Target, Color.Aquamarine, 1.0F, 0);
_device.BeginScene();

_device.RenderState.CullMode = Direct3D.Cull.None;


DirectX.Matrix ortho = new DirectX.Matrix();
DirectX.Matrix identity = new DirectX.Matrix();

ortho = DirectX.Matrix.OrthoOffCenterLH(0.0f, 640.0f, 480.0f, 0.0f, 1.0f, 100.0f);
identity = DirectX.Matrix.Identity;

_device.SetTransform(Direct3D.TransformType.Projection, ortho);
_device.SetTransform(Direct3D.TransformType.World, identity);
_device.SetTransform(Direct3D.TransformType.View, identity);

DirectX.Matrix mat = new DirectX.Matrix();
mat = DirectX.Matrix.Transformation2D(DirectX.Vector2.Empty, 0, DirectX.Vector2.Empty, new DirectX.Vector2(16, 16), 1f, DirectX.Vector2.Empty);

// Uncomment below line to see polygons stop rendering.
//_device.SetTransform(Direct3D.TransformType.World, mat);

_device.SetStreamSource( 0, _vertices, 0);
_device.VertexFormat = Direct3D.CustomVertex.PositionColoredTextured.Format;
_device.DrawPrimitives(Direct3D.PrimitiveType.TriangleList, 0, 2);

_device.EndScene();
_device.Present();



Here's the whole thing:
Form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GroZZleR.GroZ2D
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

this.ClientSize = new Size(640, 480);

this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(0, 0);

VideoTask.Instance.SetRenderWindow(this);
VideoTask.Instance.Start();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
VideoTask.Instance.Update();
}
}
}



VideoTask.cs

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Collections;
using DirectX = Microsoft.DirectX;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace GroZZleR.GroZ2D
{
public sealed class VideoTask
{
public static readonly VideoTask Instance = new VideoTask();

private Direct3D.Device _device = null;
private Control _renderWindow = null;

private Direct3D.VertexBuffer _vertices = null;

private VideoTask() {}

public void SetRenderWindow(Control renderWindow) { _renderWindow = renderWindow; }

public void Start()
{
Direct3D.PresentParameters parameters = new Direct3D.PresentParameters();
parameters.Windowed = true;
parameters.SwapEffect = Direct3D.SwapEffect.Discard;
parameters.AutoDepthStencilFormat = Direct3D.DepthFormat.Unknown;
parameters.EnableAutoDepthStencil = false;
parameters.BackBufferFormat = Direct3D.Format.Unknown;

_device = new Direct3D.Device(0, Direct3D.DeviceType.Hardware, _renderWindow, Direct3D.CreateFlags.HardwareVertexProcessing, parameters);

_device.VertexFormat = Direct3D.CustomVertex.PositionColoredTextured.Format;
Direct3D.CustomVertex.PositionColoredTextured[] verts = new Direct3D.CustomVertex.PositionColoredTextured[6];

verts[0] = new Direct3D.CustomVertex.PositionColoredTextured(0, 32, 10, Color.FromArgb(255, 255, 255).ToArgb(), 0.0f, 1.0f);
verts[1] = new Direct3D.CustomVertex.PositionColoredTextured(0, 0, 10, Color.FromArgb(255, 255, 255).ToArgb(), 0.0f, 0.0f);
verts[2] = new Direct3D.CustomVertex.PositionColoredTextured(32, 0, 10, Color.FromArgb(255, 255, 255).ToArgb(), 1.0f, 0.0f);
verts[3] = new Direct3D.CustomVertex.PositionColoredTextured(0, 32, 10, Color.FromArgb(255, 255, 255).ToArgb(), 0.0f, 1.0f);
verts[4] = new Direct3D.CustomVertex.PositionColoredTextured(32, 0, 10, Color.FromArgb(255, 255, 255).ToArgb(), 1.0f, 0.0f);
verts[5] = new Direct3D.CustomVertex.PositionColoredTextured(32, 32, 10, Color.FromArgb(255, 255, 255).ToArgb(), 1.0f, 1.0f);

_vertices = new Direct3D.VertexBuffer(typeof(Direct3D.CustomVertex.PositionColoredTextured), verts.Length, _device, 0, Direct3D.CustomVertex.PositionColoredTextured.Format, Direct3D.Pool.Default);

DirectX.GraphicsStream stream = _vertices.Lock(0, 0, 0);
stream.Write(verts);
_vertices.Unlock();
}

public void Update()
{
if(_device == null)
return;

_device.Clear(Direct3D.ClearFlags.Target, Color.Aquamarine, 1.0F, 0);
_device.BeginScene();

_device.RenderState.CullMode = Direct3D.Cull.None;
_device.RenderState.Lighting = false;
_device.RenderState.AlphaBlendEnable = true;
_device.RenderState.SourceBlend = Direct3D.Blend.SourceAlpha;
_device.RenderState.DestinationBlend = Direct3D.Blend.InvSourceAlpha;

DirectX.Matrix ortho = new DirectX.Matrix();
DirectX.Matrix identity = new DirectX.Matrix();

ortho = DirectX.Matrix.OrthoOffCenterLH(0.0f, 640.0f, 480.0f, 0.0f, 1.0f, 100.0f);
identity = DirectX.Matrix.Identity;

_device.SetTransform(Direct3D.TransformType.Projection, ortho);
_device.SetTransform(Direct3D.TransformType.World, identity);
_device.SetTransform(Direct3D.TransformType.View, identity);

DirectX.Matrix mat = new DirectX.Matrix();
mat = DirectX.Matrix.Transformation2D(DirectX.Vector2.Empty, 0, DirectX.Vector2.Empty, new DirectX.Vector2(16, 16), 1f, DirectX.Vector2.Empty);

// Uncomment below line to see polygons stop rendering.
//_device.SetTransform(Direct3D.TransformType.World, mat);

_device.SetStreamSource( 0, _vertices, 0);
_device.VertexFormat = Direct3D.CustomVertex.PositionColoredTextured.Format;

_device.DrawPrimitives(Direct3D.PrimitiveType.TriangleList, 0, 2);

_device.EndScene();
_device.Present();
}
}
}



I'm just at wit's end with this thing. I don't understand why it's not rotating the square. Anyone have any ideas at all? I'm open to any crazy suggestions right now because clearly my fairly sane ones aren't working. ;)

Share this post


Link to post
Share on other sites
I don't understand .NET code, but perhaps you have an RHW component in your vertices? (meaning it's already transformed, so world/projection/view matrices doesn't affect it)

Share this post


Link to post
Share on other sites
It's because you set scaling to Vector2.Empty, which means it has a scaling factor of 0,0. You want a scaling factor of 1,1, because it's multiplicative, not additive.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!