how would i attach two images together using nodes?

Currently I am trying to make a 2D tank scroll-er and I have two classes at the moment one for the player and one for the turret, each has its own image and other bits but I am currently at a loss at how to attach the turret using it as a child of node..my images also inherited from Csprite which is a sprite library I have made, anyway I will post my node H and .cpp and my Csprite .cpp so you can see what I am doing. I am not after a spoon feeding I just want some Pseudo code to point me in the right direction or some where with some good tutorials



Cnode Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

#ifndef _CNODE_H_
#define _CNODE_H_

#include <list>
#include "Matrix3.h"
#include "Vector2.h"

class CNode
{
public:

	CNode();
	CNode(CNode *parent);
	CNode( CNode &copy);
	
	virtual ~CNode();
	virtual void Update(float deltaTime);
	virtual void Draw();

	Matrix3 GetWorldTransform(); 

	Matrix3 &GetTransform()		
	{
		//m_transform = transform;
		return m_transform; 
	}

	Vector2 &GetPosition()
	{
		return Position;
	}

	float &GetRotation()
	{
		return Rotation;
	}
	
	// make the node a child of this node.
	// if the node is a child of another node remove it from that node
	void AddChild(CNode *node);

	// find the node in its children and remove it if it exists
	void RemoveChild(CNode *node);
	
	// remove the node from its parent if it has one
	void UnParent();
	
	
protected:

	CNode *m_parent;
	std::list<CNode *> m_children;
	Matrix3 m_transform;
	
	Vector2 Position;
	float Rotation;

};

inline Matrix3  CNode::GetWorldTransform() 
{
	if( m_parent != NULL )
		return  m_transform * m_parent->GetWorldTransform();
	
	return m_transform;
}

#endif



Cnode .cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
CNode::CNode()
{
	m_parent = NULL;
	m_transform = Matrix3::IDENTITY;
}

void CNode::AddChild(CNode *node)
{
	if( node->m_parent != NULL )
	{	node->m_parent->RemoveChild(node); }

	node->m_parent = this;
	m_children.push_back(node);

}

void CNode::RemoveChild(CNode *node)
{

	m_children.remove(node);
	node->m_parent = NULL;
}

void CNode::UnParent()
{
	if( m_parent != NULL )
		m_parent->RemoveChild(this);
}

void CNode::Update(float deltaTime)
{
	if( m_parent != NULL )
	{
		//m_parent->Update( deltaTime );
		std::list<CNode *>::iterator itr;
		for (itr = m_children.begin(); itr != m_children.end(); ++itr)
		{
			(*itr)->Update(deltaTime);
		}
	}
}

void CNode::Draw()
{

}

CNode::~CNode()
{

}


and the Csprite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

Csprite::Csprite() : CNode()
{
	m_pTexture = NULL;
}
Csprite::Csprite(const char *pFilename): CNode()
{
	m_pTexture = new CTexture(pFilename);
	if (m_pTexture)
	{
		m_pTexture->GetSize(m_fWidth,m_fHeight);
	}
}

Csprite::~Csprite()
{

}

void Csprite::Draw()
{
	unsigned int colour = 0;
	
	Vector2 tl(-m_fWidth*0.5f, -m_fHeight*0.5f); //Top left,
	Vector2 tr(m_fWidth*0.5f, -m_fHeight*0.5f); //Top Right
	Vector2 br(m_fWidth*0.5f, m_fHeight*0.5f); //Bottom Right
	Vector2 bl(-m_fWidth*0.5f, m_fHeight*0.5f); //Bottom Right

	// transform those point... the matrix should have the scale rotation and translation
	tl = m_transform.TransformPoint(tl);
	tr = m_transform.TransformPoint(tr);
	br = m_transform.TransformPoint(br);
	bl = m_transform.TransformPoint(bl);

	// bind the texture
	glBindTexture(GL_TEXTURE_2D, m_pTexture->GetTextureHandle());
	//start working on our quads....
	glBegin(GL_QUADS);

	//top left
	glTexCoord2f(0, 0);
	glVertex2i(tl.x,tl.y);

	//bottom left
	glTexCoord2f(1, 0);
	glVertex2i(tr.x, tr.y);

	//bottom right
	glTexCoord2f(1, 1);
	glVertex2i(br.x, br.y);

	//top right
	glTexCoord2f(0, 1);
	glVertex2i(bl.x, bl.y);
	glEnd();

 }



It seems sort of odd that you would have 2 separate classes. Why don't you just have the player class deal with both the body and the turret of the tank?
well I was going to have different enemies tanks that also use the turrets stuff so I thought it would be better to have it in a separate class
In that case, just make a "Tank" class that contains a body and a turret (classes for those maybe?) and you can make a Tank based off any combination of turrets/bodies.

The body/turret might just be sets of graphics; maybe a class isn't needed.
Topic archived. No new replies allowed.