🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Move object in relation to parent

Started by
10 comments, last by Nagle 2 years, 10 months ago

Hello!

I'm writing a simple game in C# and i'm having some difficulty to calculate the local position of a sprite.

What i'm aiming to achieve:

  • Calculate the sprite "local position" relative to the parent.
  • When the parent sprite position is changed, the children sprite should be aways following the parent while keeping its relative position.

Example:

Lets assume i have a parent sprite at world position (4, 4) and a children sprite at local position (2, 2)

If the parent sprite world position moves to (5, 5), his children local position should be at (3, 3)

Below is a example code of what i'm trying to make.
Lets assume the parent of this sprite moved and SetPosition was called:

public class Sprite
{
	public Sprite parent;
	public Point world_position;
	public Point local_position;
	
	public void SetPosition(Point new_position)
	{
	    // What should these numbers contain?
 		this.world_position = ???
 		this.local_position = ???
	}
}
Advertisement

World position should be absolute not relative. Meaning that if you move from (4,4) to (5,5), your object is world position is at (5,5). With that said, it is always easier to have child frame/position/origin defined relative to its parent. This means that you do not use world space position for the child, instead they are defined relative to the parent local origin. For simplicity, lets assume that the parent local origin is (0,0) and lets assume the only transform allowed ins translation. To position the parent in the world, you would just concatenate( add) the desired world space position to the parent local space origin. This gives the desired effect of ‘positioning' the parent in world space. Now lets assume that we have a child that defined relative to its parent at (1,1). This means that wherever the parent is positioned, the child will always be (1,1) units away in the local space of the parent. To compute the child world space position, all that is required it to compute the parent world space position and concatenate( in this add ) the child relative position.

@cgrant Hello, thanks for the answer, i think i understand it better now but could you provide the formulas for this?

Anyone?

Might have misunderstood but I think you are missing one variable. If new position is the world position then the child position would be new position + local position.

@undefined But how do i calculate all of that?

@wolfrick Look into SceneGraphs and SceneNodes. That should help you out. You attach the nodes to stay relative to the parent. So where ever you move the parent node the child will follow.

@LedMar I don't know what these are but i think the math to solve my problem might be simpler than i think, so could someone provide the math?

The location and rotation of each object is called a “transform.”

A transform, for sprites, can be expressed as a matrix of 2 rows of 3 columns.

m00 m01 m02
m10 m11 m12

The left 2x2 (m00, m01, m10, m11) imbues rotation and scale, and the rightmost 2-item column (m02, m12) imbues position.

The actual position of a vertex on the sprite (either the center, or one of the corners) is expressed as the column vector (x, y, 1)

Then, you get the transformed position as

outx=(x, y, 1)dot(m00, m01, m02)
outy=(x, y, 1)dot(m10, m11, m12)

This turns out to be the same as matrix-vector multiplication, if you tack on a “1” at the end of the position vector in the end.

So:

OutPosition = TransformMatrix * InPosition

If you have a hierarchy, such as “gun attaches to turret, turret attaches to tank, tank moves around world,” then you string those along, with the “biggest” (topmost) transform first:

WorldPosition = TankTransform * TurretTransform * GunTransform * InPosition

So, yes, the math is very simple, assuming you understand matrix / vector multiplication.

Also, how to create a matrix given the values “rotation X degrees, scale times Y, position ZZ” – which you can likely find in your math library, or look up in a local reference. (It's a couple of sine/cosines.)

enum Bool { True, False, FileNotFound };

@hplus0603 Hello, thanks for the answer, i'll look into it.

This topic is closed to new replies.

Advertisement