Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 65 66 [67] 68 69 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100596 times)

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #990 on: January 07, 2013, 02:32:06 pm »

Back again with another programming question.

Code: [Select]
   transform.position.x += Mathf.Sin(transform.rotation.y) * speed * Time.deltaTime;
   transform.position.z += Mathf.Cos(transform.rotation.y) * speed * Time.deltaTime;
   transform.position.x += Mathf.Cos(transform.rotation.y) * sideSpeed * Time.deltaTime;
   transform.position.z += Mathf.Sin(transform.rotation.y) * sideSpeed * Time.deltaTime;

This is supposed to make the camera move along the x axis and z axis without moving up and down along the y axis. I thought I had my sines and cosines right, but it behaves... strangely, to say the least. Speed and sideSpeed are increasing and decreasing fine, so it's not a problem with them.
I'm pretty sure Unity can do matrix maths just fine, which would condense all of that mess into something more transparent.
Logged

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #991 on: January 07, 2013, 02:37:02 pm »

Chances are you're looking for -= Sin on the very last line, instead of += Sin. If you look at the 2d rotation matrix http://en.wikipedia.org/wiki/Rotation_matrix, notice the change in sign on the sine on the first row of the matrix.

Thanks! I fixed that, so all the buttons move the camera in the correct direction initially, but the angle of movement still doesn't change correctly after the camera is rotated.

Back again with another programming question.

Code: [Select]
   transform.position.x += Mathf.Sin(transform.rotation.y) * speed * Time.deltaTime;
   transform.position.z += Mathf.Cos(transform.rotation.y) * speed * Time.deltaTime;
   transform.position.x += Mathf.Cos(transform.rotation.y) * sideSpeed * Time.deltaTime;
   transform.position.z += Mathf.Sin(transform.rotation.y) * sideSpeed * Time.deltaTime;

This is supposed to make the camera move along the x axis and z axis without moving up and down along the y axis. I thought I had my sines and cosines right, but it behaves... strangely, to say the least. Speed and sideSpeed are increasing and decreasing fine, so it's not a problem with them.
I'm pretty sure Unity can do matrix maths just fine, which would condense all of that mess into something more transparent.

You mean with something like transform.forward? When I started on camera controls, that was what I did; the only problem is that I need the camera to stay at the same level on the y axis, which doesn't work if the camera is angled down. Before this, I had it just reset transform.position.y to a number after it moved, but I'd like to see how it would work if I wasn't using Unity for this program.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #992 on: January 07, 2013, 02:57:53 pm »

Transform.Translate can take a direction vector or the x, y and z steps, either relative to local space or relative to world space: http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html


Moving along the x and z axis would then become Transform.Translate(Direction*Time.deltaTime, Space.World); if you have a direction vector to move along, or Transform.Translate(xStep*Time.deltaTime, 0, zStep*Time.deltaTime, Space.World); if you don't.
« Last Edit: January 07, 2013, 03:05:15 pm by Virex »
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #993 on: January 07, 2013, 05:40:57 pm »

Back again with another programming question.

Code: [Select]
transform.position.x += Mathf.Sin(transform.rotation.y) * speed * Time.deltaTime;
transform.position.z += Mathf.Cos(transform.rotation.y) * speed * Time.deltaTime;
transform.position.x += Mathf.Cos(transform.rotation.y) * sideSpeed * Time.deltaTime;
transform.position.z += Mathf.Sin(transform.rotation.y) * sideSpeed * Time.deltaTime;

This is supposed to make the camera move along the x axis and z axis without moving up and down along the y axis. I thought I had my sines and cosines right, but it behaves... strangely, to say the least. Speed and sideSpeed are increasing and decreasing fine, so it's not a problem with them.


Is the camera moving at all?

If so, you might need to use transform.eulerAngles (iirc), which is a Vector3, whereas transform.rotation is a quaternion. You might also need to multiply it by Mathf.Deg2Rad

If not, you might have issues assigning the position. Do transform.position = new Vector3(newX, newY, newZ); or transform.Translate(xChange, yChange, zChange);
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #994 on: January 07, 2013, 06:13:12 pm »

Thanks for the help, everyone! It works with this snippet of code in the place of the original:

Code: [Select]
xMove = ((Mathf.Sin(transform.rotation.y) * speed) +  (Mathf.Cos(transform.rotation.y) * sideSpeed)) * Time.deltaTime;
zMove = ((Mathf.Cos(transform.rotation.y) * speed) + (Mathf.Sin(transform.rotation.y) * sideSpeed)) * Time.deltaTime;

transform.position = new Vector3(transform.position.x + xMove,6,transform.position.z + zMove);

@Virex: I didn't realize Space.World would have that effect. Learning something new every day!

EDIT: Wait, no. That doesn't work, and I made an incredibly silly math mistake. I think.

EDITEDIT: Okay, this version actually works!

Code: [Select]
charRotation = transform.rotation.eulerAngles.y * Mathf.Deg2Rad;

xMove = ((Mathf.Sin(charRotation) * speed) +  (Mathf.Cos(charRotation) * sideSpeed)) * Time.deltaTime;
zMove = ((Mathf.Cos(charRotation) * speed) + (-Mathf.Sin(charRotation) * sideSpeed)) * Time.deltaTime;

transform.position = new Vector3(transform.position.x + xMove,6,transform.position.z + zMove);

Kofthefens, you were right about using eulerAngles and Deg2Rad. Thanks!
« Last Edit: January 07, 2013, 08:32:48 pm by Araph »
Logged

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #995 on: January 10, 2013, 04:09:36 pm »

Okay, I'm getting stumped with inheritance and pointers in C++ (in dynamic structures).

I have (so far) 2 classes:
 - NodeBase, which has a "skeleton" function: it knows its parent, its children, and it knows some things to do with them.
 - Node, which inherits from NodeBase and adds a "value", which is used in backward induction etc. etc.

Code: (Node) [Select]
class NodeBase
{
public:
NodeBase* parent;
vector<NodeBase*> children;

public:
        void AddChild(NodeBase* NewChild) { /*...*/ }
        //other stuff

Code: (Node) [Select]
class Node : public NodeBase
{
public:
vector<double> val;
unsigned int player;

        Node* BestChildPointer(unsigned int Player)
{
return (children[BestChildIndex(Player)]);
}

        //other stuff

It seems that there are two problems:
 - Node::BestChildPointer(), and other functions which access the NodeBase members, can't convert NodeBase* into Node*
 - (lesser problem)  Node::AddChild() will need to be given a Node* (or ClassInheritedFromNode* ) so that "trimming" doesn't occur

The only solution I see here is casting NodeBase* as Node* and being really sure that I am legally doing that, but that's nasty. :P
Any ideas for how to go around this?
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #996 on: January 10, 2013, 04:26:10 pm »

- Node::BestChildPointer(), and other functions which access the NodeBase members, can't convert NodeBase* into Node*

You can use dynamic_cast to cast your way up or down the inheritance chain
Code: [Select]
Node* BestChildPointer(unsigned int Player)
{
return dynamic_cast<Node*>(children[BestChildIndex(Player)]) ;
}

dynamic_cast will return NULL if the cast fails.

Quote
- (lesser problem)  Node::AddChild() will need to be given a Node* (or ClassInheritedFromNode* ) so that "trimming" doesn't occur
Not sure I get the problem, what do you mean by "trimming"?
Logged

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #997 on: January 10, 2013, 09:04:52 pm »

Working on saving with XML in Unity; have this so far:

Code: [Select]
import System.Collections.Generic;
import System.Xml;
import System.Xml.Serialization;
import System.IO;

public var skin : GUISkin;
static public var title : String = "New Campaign";

private var titleWarning : boolean = false;
private var campaignExists : boolean = false;

public class metaTitle {
public var title : String;
}

@XmlRoot("meta")
public class metaDataContainer {
@XmlArray("metaData")
@XmlArrayItem("metaTitle")
public var metaData : System.Collections.Generic.List.<metaTitle>;

public function Save (path : String) {
var serializer : XmlSerializer = new XmlSerializer(metaDataContainer);
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}

public static function Load (path : String) : metaDataContainer {
var serializer : XmlSerializer = new XmlSerializer(metaDataContainer);
var stream : Stream = new FileStream(path, FileMode.Open);
var result : metaDataContainer = serializer.Deserialize(stream) as metaDataContainer;
stream.Close();
return result;
}
}

function OnGUI () {
GUI.skin = skin;

title = GUI.TextField(Rect(Screen.width * .75 - 60, Screen.height * .5 - 10, 120, 20), title);
GUI.Box(Rect(Screen.width * .75 - 50, Screen.height * .5 + 10, 100, 20), "Campaign Title");

if (GUI.Button(Rect(Screen.width * .75 - 100, Screen.height * .65 - 30, 200, 60), "Create Campaign")) {
if (title == "") {
titleWarning = true;
}
else if (System.IO.Directory.Exists("Campaigns/" + title)) {
titleWarning = false;
campaignExists = true;
}
else {
System.IO.Directory.CreateDirectory("Campaigns/" + title);
var metaDataContainer : metaDataContainer;
metaDataContainer.Save(Path.Combine(Application.dataPath, "Campaigns/" + title + "/meta.xml"));
}
}

if (titleWarning) {
GUI.Box(Rect(Screen.width * .75 - 80, Screen.height * .75 - 20, 160, 40), "Enter campaign title!");
}
if (campaignExists) {
GUI.Box(Rect(Screen.width * .75 - 80, Screen.height * .75 - 20, 160, 40), "Campaign already exists!");
}
}

All of it works except for metaDataContainer.Save(Path.Combine(Application.dataPath, "Campaigns/" + title + "/meta.xml"));, which makes Unity cough up the error "Object reference not set to an instance of the object". I'm sure the error is simple, but I can't find anything on Google about using classes like this. Does anybody here know about classes in JavaScript?
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #998 on: January 10, 2013, 09:40:21 pm »

Your problem is that the variable holding your metaDataContainer object is also called metaDataContainer. This makes the interpreter think that you're trying to call the function on the class, not on the specific object. It should work if you give it another name.
Logged

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #999 on: January 10, 2013, 11:22:42 pm »

- Node::BestChildPointer(), and other functions which access the NodeBase members, can't convert NodeBase* into Node*

You can use dynamic_cast to cast your way up or down the inheritance chain
Code: [Select]
Node* BestChildPointer(unsigned int Player)
{
return dynamic_cast<Node*>(children[BestChildIndex(Player)]) ;
}

dynamic_cast will return NULL if the cast fails.

Quote
- (lesser problem)  Node::AddChild() will need to be given a Node* (or ClassInheritedFromNode* ) so that "trimming" doesn't occur
Not sure I get the problem, what do you mean by "trimming"?
I mean, trying to put an object into a smaller hole (of the type that was inherited).
Also, you mean dynamic_cast will return 0? The problem is that that's a valid state in some parts of the program - it means that it's the topmost node.
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1000 on: January 11, 2013, 12:05:02 am »

Your problem is that the variable holding your metaDataContainer object is also called metaDataContainer. This makes the interpreter think that you're trying to call the function on the class, not on the specific object. It should work if you give it another name.

I think what you're saying is something like this:
Change...
Code: [Select]
var metaDataContainer : metaDataContainer;
metaDataContainer.Save(Path.Combine(Application.dataPath, "Campaigns/" + title + "/meta.xml"));
...to something like...
Code: [Select]
var metaDataSave : metaDataContainer;
metaDataSave.Save(Path.Combine(Application.dataPath, "Campaigns/" + title + "/meta.xml"));

After testing, it still gives the same error. Am I misinterpreting what you meant?
Logged

GalenEvil

  • Bay Watcher
    • View Profile
    • Mac-Man Games
Re: Programming Help Thread (For Dummies)
« Reply #1001 on: January 11, 2013, 01:08:33 am »

just took a look at it and you are declaring the variable but not initializing it.
you might want to do
Code: [Select]
var metaDataSave : metaDataContainer = new metaDataContainer();
metaDataSave.Save(Path.Combine(Application.dataPath, "Campaigns/" + title + "/meta.xml"));

Not initializing the metaDataSave variable is causing it to hold a null-value, and so you get a null reference exception when you try to do anything with it :P
Logged
Fun is Fun......Done is Done... or is that Done is !!FUN!!?
Quote from: Mr Frog
Digging's a lot like surgery, see -- you grab the sharp thing and then drive the sharp end of the sharp thing in as hard as you can and then stuff goes flying and then stuff falls out and then there's a big hole and you're done. I kinda wish there was more screaming, but rocks don't hurt so I guess it can't be helped.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1002 on: January 11, 2013, 03:31:05 am »

I mean, trying to put an object into a smaller hole (of the type that was inherited).
Also, you mean dynamic_cast will return 0? The problem is that that's a valid state in some parts of the program - it means that it's the topmost node.
Trimming only occurs when making the hole, or trying to access parts that are trimmed. AddChild neither allocates memory, nor does it (should it!) access any Node specific functions, so you should be okay there. That's polymorphism working for you.

As for BestChildPointer, either return a BaseNode*, or if you're reallyreally sure that it's always a Node*, return that using a dynamic_cast, and/or throw when the cast fails (or there are no children).

Returning 0 for BestChildPointer should be okay if you check for it when you call it; you should not be getting the topmost node when calling a "getChild"-like function, ever.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1003 on: January 11, 2013, 07:42:07 am »

Alright, thanks for clarifying.
I'm using an implementation of dynamic_cast which throws when it fails, so that should be fine there.
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1004 on: January 11, 2013, 01:27:16 pm »

@TolyK:
In your implementation, is there any reason to have any of the following:
- A separate BaseNode and Node class? AddNode isn't virtual in your implementation so I'm kinda curious why you need inheritance in this situation
- A reason to have BestChildPointer in your Node implementation but not in the BaseNode implementation?
- The necessity to have Nodes connect to nodes that are not Nodes themselves (i.e. to have vector<NodeBase*> children and NodeBase* parent)?

Usually you want to stay away from casts, since you throw out compile-time type safety and can incur significant performance costs. If you answered no to any of those questions, then you should consider redesigning your class structure. Either merge BaseNode and Node into one class, add BestChildPointer to the BaseNode class as an abstract function, or make BaseNode into a template class BaseNode<T> that has T* parent; and vector<T*> children; and have Node inherit from BaseNode<Node> (see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern).

Be careful about not falling into the trap of using inheritance to 'save lines of code'. Objects should not inherit from one another just because they share behavior - in that case, what you are looking for is a component pattern, not an inheritance pattern.

Also, as for trimming, I think slicing (http://en.wikipedia.org/wiki/Object_slicing) is what's being referred to? Trimming only happens when you copy objects. Pointers are only references to objects, and copying a reference from a subtype to a supertype does not copy the object, so slicing is not a problem when you're working with pointers. You just need to be sure define your destructor and any member functions that can be overriden as virtual so references to the base class will called the functions of the derived class properly (see http://www.parashift.com/c++-faq-lite/virtual-dtors.html).
Logged
Pages: 1 ... 65 66 [67] 68 69 ... 91