The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

DirectX Camera issues (Coding issue)

AgemAgem Registered User regular
edited December 2006 in Help / Advice Forum
Apparently, I'm retarded.

Long story short: I'm learning how to make DirectX, and making a sprite-based platformer (with C#) as a testing ground. I've made some other simple games before, but this is the first that needs a moving camera. I've been reading tutorials on how to use the camera for the past few hours. Although I would love to be able to understand the subject matter just by reading it, I usually find it helpful to test it as I go along to make sure I know what's happening. Unfortunately, I can't seem to get the camera to do anything.

Specifically, if I do this:
device.Transform.View = Matrix.LookAtLH(new Vector3(400, 400, 400), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
it does the exact same thing as if I do this:
device.Transform.View = Matrix.LookAtLH(new Vector3(),new Vector3(),new Vector3());
which does the exact same thing as if I do this:
device.Transform.View = new Matrix();
Which is to say, nothing.

The line of code is definitely taking place before the device renders - in fact, I've put them everywhere from right after the creation of the device to right before it renders to right after it renders just in case something different happened on the second frame, and nowhere has the code made a difference - so I'm puzzled about why nothing changes no matter how I change the view matrix. At first I thought it might be the Sprite class playing shenanigans on me somehow (because I already know the Sprite class makes the sprites tilt to face the camera), but even after inserting the code into an older project that rendered primitives more directly with a TriangleFan, there's still nothing that changed.

Either there's something I don't know about but have to set - somewhere - to make the D3D device use the damned View matrix when it renders, or I seriously misunderstand how this is supposed to work.

If someone could set me straight either way, that would be great.

Note: I could post the source code if you really wanted me too, but because I saved this camera thing for just about last the source is huge right now, confusing because it's my first sidescroller, and completely uncommented. Basically, it's completely illegible. But I'm sure that this doesn't anything have to do with the fact that my code for this project isn't in very good shape - as I said, I can't get the camera to move in any older project I paste any of those lines of code in.

But if you don't have enough to go on based what I told you, I'm obviously not getting something about the tutorials. As I said, though, I learn much better if I'm actually testing things and changing values to see what exactly does what. But I also can't find any working sample code that is written in C# and not already so complex I don't understand it, which isn't helping.

This is the first real roadblock I've come across since I started this, and because I just spent three hours failing at making the camera just pan, I'm a little bit frustrated. Any help would be greatly appreciated and I will love you forever.

Agem on

Posts

  • blincolnblincoln Registered User regular
    edited December 2006
    So what *is* happening when you make that call? Do you see anything in your rendering target?

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • AgemAgem Registered User regular
    edited December 2006
    I'm see the exact same thing as if I hadn't changed the view matrix at all, which is why I'm so confused.

    I'll try to whip up a simple program to show what I'm talking about (and I suppose if I end up making a small program for this that works I should be able to figure out my problem from there).

    Agem on
  • AgemAgem Registered User regular
    edited December 2006
    Okay:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace CameraTest
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Form1 form = new Form1();
                form.Show();
    
                for (int i = 0; i < 1000; i++)
                {
                    form.Render();
                    Application.DoEvents();
                }
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    
    namespace CameraTest
    {
        public partial class Form1 : Form
        {
            private Device device;
            private Texture tex;
            private Sprite sprite;
            private Vector3 spritePosition;
            private Vector3 spriteCenter;
    
            public Form1()
            {
                InitializeComponent();
                InitializeDevice();
                InitializeSprite();
            }
    
            public void InitializeDevice()
            {
                PresentParameters presentParameters = new PresentParameters();
                presentParameters.Windowed = true;
                presentParameters.SwapEffect = SwapEffect.Discard;
                device = new Device(Manager.Adapters.Default.Adapter, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParameters);
            }
    
            public void InitializeSprite()
            {
                tex = TextureLoader.FromFile(device, "whiteTexture.jpg", 16, 16, 1, 0, Format.A8R8G8B8, Pool.Managed, Filter.Point, Filter.Point, (unchecked((int)0xff0000ff)));
                sprite = new Sprite(device);
                spritePosition = new Vector3(30, 30, 0);
                spriteCenter = new Vector3(0, 0, 0);
            }
    
            public void Render()
            {
                //device.Transform.View = Matrix.LookAtLH(new Vector3(400, 400, 400), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
                device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
                //device.Transform.View = Matrix.LookAtLH(new Vector3(400, 400, 400), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
                device.BeginScene();
                //device.Transform.View = Matrix.LookAtLH(new Vector3(400, 400, 400), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
                sprite.Begin(SpriteFlags.AlphaBlend);
                sprite.Draw(tex, new Rectangle(0, 0, 16, 16), spriteCenter, spritePosition, Color.FromArgb(255, 255, 255, 255));
                sprite.End();
                //device.Transform.View = Matrix.LookAtLH(new Vector3(400, 400, 400), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
                device.EndScene();
                //device.Transform.View = Matrix.LookAtLH(new Vector3(400, 400, 400), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
                device.Present();
                //device.Transform.View = Matrix.LookAtLH(new Vector3(400, 400, 400), new Vector3(0, 0, 1), new Vector3(0, 1, 0));
            }
            protected override void OnPaint(PaintEventArgs e)
            {
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
            }
        }
    }
    
    whiteTexture.jpg is just a 16x16 white jpeg, so the program just makes a tiny white square a little bit off from the origin, surrounded by blue.

    Those changes to device.Transform.View seem to have no effect (when uncommented, of course).

    Why am I so stupid that I can't figure out how to make this work

    Agem on
  • blincolnblincoln Registered User regular
    edited December 2006
    There's a lot of things about Direct3D that I've found unintuitive. I'll take a look at it at work today.

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • blincolnblincoln Registered User regular
    edited December 2006
    Okay, tentatively it looks like the problem is that the sprite drawing method you're using means that the camera position is irrelevant - the sprite will be drawn relative to where the camera is pointing, not an absolute position.

    Let me see if I can verify that, and/or provide another method.

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • blincolnblincoln Registered User regular
    edited December 2006
    Yeah, as far as I can tell, the camera position doesn't matter for this method. So if you want to use it, I'd think you'd be going with the method where you simulate the camera moving by moving everything else in relation to it.

    I tried throwing together the other option, where you use point sprites in 3D space, but I couldn't make it happen for some reason and I'm out of time for looking at this today. That would have been a harder method anyway.

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • AgemAgem Registered User regular
    edited December 2006
    It's drawn where the camera is pointing? Damn. I wonder why they made it that way; it seems to me that it would be quicker to just move the camera than to iterate through every sprite being drawn and change their positions manually. Ah well - this solves my problem, anyway.

    Thank you very much for helping me out. I probably would've been stuck on this for weeks if I was left to figure it out on my own. I'll have to change the way I did a few things, but I know where to go from here. Thanks again! :o

    Agem on
Sign In or Register to comment.