Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Accessor Method Vector question

Started by andersonj21 Feb 23, 2008 at 4:33 PM 11 replies 2k views
Original Post
andersonj21
andersonj21
I'm using Vectors to store the positions of my sprites. In the Sprite class I have an accessor method like this: private Vector2 position; public Vector2 Position { get { return pos; } set { pos = value; } } Now, in my other classes where I want to use a Sprite I'm running into problems. When I want to update a Sprite's position I can't figure out what to do. How do update the X or Y position of the vector from another class without having to create a new vector each time. If "enemy" is the sprite I'm creating, I want to do something like this: enemy.Position.X += 5f; // obviously this won't work but I'm sure there must be something I can do without creating a new Vector. Until now I've been handling Position with two accessor methods: PosX and PosY which each return floats, and then when I draw the sprites I just do this: spriteBatch.Draw(texture, new Vector2(enemy.PosX, enemy.PosY), Source, Color) I think this is a bad way to do it....any suggestions
SiCrane
SiCrane
You know, nothing stops you from having an accessor for position as a vector and an accessor for PosX and an accessor for PosY.
andersonj21
andersonj21
yes, but wouldn't i still have to make a new vector when i assign the posx and posy to the position vector:

enemy.Position = new Vector2(enemy.PosX, enemy.PosY);

it seems like a waste since I already have the Vector and only really need to Update its value.
SiCrane
SiCrane
Any reason you wouldn't use

enemy.Position = enemy.Position;

? Which is admittedly a pretty silly example, setting a position to its current position.
Sneftel
Sneftel
In C#, one creates lots of small objects. It's the way of things. Don't worry about it. Allocation and GC are plenty fast.
andersonj21
andersonj21
SiCrane:

I'm not understanding how that would help me change the X or Y positions of the position vector...how do I gain access to the X and Y (float) properties of the Vector to change them?


Sneftel:

So is it ok to make the vector in the Draw method like I am doing:
spriteBatch.Draw(texture, new Vector2(enemy.PosX, enemy.PosY), Source, Color)



By the way, thanks to both of you for your help.
SiCrane
SiCrane
You'd just set the .X or .Y members of the position vector inside your PosX or PosY property.
chipmeisterc
chipmeisterc
So instead of having one get/set method which takes a vector

How about you also have a GetX() GetY() GetZ() which return a single float component and a SetX() SetY() SetZ() which takes floats and set the respective components.

you can also override the + - operators etc so that you can add vectors together, and
* to multiply the length by a scalar.

you may also want a Length() function a Normalise() function and Dot() and Cross() products :)

Hope that helps.
Structural
Structural
Quote:
Original post by andersonj21
I'm using Vectors to store the positions of my sprites.
In the Sprite class I have an accessor method like this:

private Vector2 position;
public Vector2 Position
{
get { return pos; }
set { pos = value; }
}


Now, in my other classes where I want to use a Sprite I'm running into problems.
When I want to update a Sprite's position I can't figure out what to do.
How do update the X or Y position of the vector from another class without having to create a new vector each time.

If "enemy" is the sprite I'm creating, I want to do something like this:

enemy.Position.X += 5f; // obviously this won't work but I'm sure there must be something I can do without creating a new Vector.

Until now I've been handling Position with two accessor methods: PosX and PosY which each return floats, and then when I draw the sprites I just do this:

spriteBatch.Draw(texture, new Vector2(enemy.PosX, enemy.PosY), Source, Color)

I think this is a bad way to do it....any suggestions


Why not throw out the getter/setter? Like:

public Vector2 Position;

It may not be that pretty, but at least you can use your member variable in an intuitive manner.
STOP THE PLANET!! I WANT TO GET OFF!!
zdlr
zdlr
Structural: I think it's because C# properties are intended to contain side affects of getting or setting the underlying field, which should be private. Like firing PropertyChanged events, implementing transactions, etc.
Zahlman
Zahlman
Quote:
Original post by andersonj21
Now, in my other classes where I want to use a Sprite I'm running into problems.
When I want to update a Sprite's position I can't figure out what to do.


Reframe the problem. Why would you "update the Sprite's position"?

You can just have both a Position accessor and x/y accessors:

private Vector2 position;public Vector2 Position { get { return position; } set { position = value; } }public int X { get { return position.x; } set { position.x = value; } }public int Y { get { return position.y; } set { position.y = value; } }


But you really don't want to do this, for the same reason you wouldn't do something analogous in real life:

// not socially acceptableOpen customer's wallet.Remove amount owed from wallet.Return wallet to customer.// Instead:Give bill to customer.Customer pays you amount owed.


In other words, we don't do something like 'enemy.x += dx; enemy.y += dy;'. Instead, we let the enemy do that calculation: 'enemy.move(dx, dy);', where that function makes the necessary changes. This also means that we avoid repeating the process for X and Y directions: the repeated code (process x, then process y the same way) appears only in one place (inside .move()).
Structural
Structural
Quote:
Original post by zdlr
Structural: I think it's because C# properties are intended to contain side affects of getting or setting the underlying field, which should be private. Like firing PropertyChanged events, implementing transactions, etc.


Well, I know that. But in this case it seems that the getter/setter are just there for aesthetics and are only getting in the way. In such a case I'd opt for the more practical solution and ignore the guidelines for a second.

On the other hand, I more agree with Zahlman in this case. I hadn't thought about it yet, but this does look like an "Inappropiate Intimacy" code smell because you're directly juggling with the private parts of the Sprite (the getter/setter is just syntactic suggar in this case). Creating a "move(dx, dy)" is a very good suggestion by Zahlman.
STOP THE PLANET!! I WANT TO GET OFF!!
Zahlman
Zahlman
Quote:
Original post by Structural
Creating a "move(dx, dy)" is a very good suggestion by Zahlman.


In fact, I would argue this is the classic example of the situation. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.