Jump to content

  • Log In with Google      Sign In   
  • Create Account


DishSoap

Member Since 13 Sep 2012
Offline Last Active Yesterday, 08:09 AM

Topics I've Started

Pointer to Pointer Issue

26 February 2014 - 10:59 AM

*EDIT* As Ardvajk pointed out, while printing I am using the variable 'used' which == elements in the map. I should be using the number of bins or buckets in my array. 

 

Hey everyone, I'm having some strange issues I was hoping someone could help with. Basically I am inserting into a HashSet at the same index in an array [0] in my case. And when I loop through to print all the elements in the set I get some strange output for the bucket at index [1] even though I believe it to be completely empty. Also, this is a homework assignment, so just some guidance and not an outright answer would be appreciated smile.png

 

I am building a HashSet where the map is basically a pointer to a pointer of nodes. Hopefully I'm not posing a wall of code here next, as that would be annoying. But I have to post quite a bit for you to see what's going on. The declaration looks as such:

Node** set      = nullptr;
...
Node (T v,  Node* n = nullptr) : value(v), next(n){} // Relevant constructor

In my constructor for the HashSet I initialize the set to such:

template<class T>
HashSet<T>::HashSet(int (*func)(const T& element), double load) : hash(func), load_factor(load) {
	set = new LN*[bins];
	for(int i = 0; i < bins; i++) set[i] = nullptr;
}

My insert statement looks like this:

template<class T>
int HashSet<T>::insert(const T& element) {
  //write code here
	if(!contains(element))
	{
		int index = hash_compress(element);
	        Node* temp = set[index];
		if(temp == nullptr)
		{
			temp = new LN(element);
			set[index] = temp;
		}
		else
		{
			Node* new_node = new LN(element, set[index]);
			set[index] = new_node;
		}
		maintain_load(++used);
	}
}

Ok, that's all I will post. As I don't want to post too much, but I am happy to give more code if needed. Basically in my test, you will have to trust that int index = 0 for two inserts. After the second insert, the item gets properly inserted into bucket 0 but basically fills bucket 1 with a ton of garbage. To be exact, when I go to print it out all of my environment variables and the alphabet print out, it's kind of funny looking smile.png

 

My print method basically looks like this:

for(int i = 0; i < bins; i++) // used to be i < used
	{
		Node* temp = set[i];
		std::cout << "[" << i << "]" << std::endl;
		std::cout << "{" << std::endl;
		while(temp != nullptr)
		{
			std::cout << temp->value << std::endl;
			temp = temp->next;
		}
		std::cout << "}" << std::endl;
	}

Memory Leak Question C++

06 December 2013 - 12:28 PM

I was working on a project with a friend the other day and he had code that looked similar to this:

std::unordered_map<std::string, Person* person> map;

for(int i = 0; i < N; i++)
{
    Person person_to_add;
    /* create members for person */
    map[person_to_add.name] = &person_to_add;
}

map["Tom"].email = "something";

This is funny, because to me, this code is completely wrong. Person_to_add goes out of scope after each iteration of the loop so it's address should be pointing to some location on the stack, no? 

 

Anyway, what is curious, and why we had a hard time finding the bug was that the project will go on working as normal this way, it will just result in a memory leak. What am I missing here? Does the c++ container dynamically allocate 'Person' at some point and never free it? Or is there something else going on?

 

Thanks. 


Platform Architecture Help

28 November 2013 - 11:32 AM

Hi there. (The title meant to say Platformer* not Platform)

 

I am currently building a platformer. I use sweep and prune to detect collision and for the screen culling. It works great, I just wanted some ideas for a good way of detecting the 'response' when two individual objects collide. Currently(which I want to change) I have a struct that looks something like this:

struct WorldObject
{
    Vector position;
    Rect intersection_rect;
    Drawable sprite;
    CustomResponse* response;
};

Where my data structure for sweep and prune holds a group of these CollidableObjects and the response is basically implemented in the object that the player is colliding with. So a moving platforms response would accept the Player as a parameter, and respond by changing the player's position by adding or subtracting its movement. It would then end this response when the player no longer collides with it.

 

I don't really like this strategy. It adds a lot of functionality, which I don't feel really relates, to one struct. I was wondering how you guys have  done this in the past? How did you query for the 'types' of objects intersecting, how did you find the drawable based on your culling from sweep and prune, etc. Thanks!

 

Oh, by the way. Happy Thanksgiving!! smile.png


Beginning Isometric RTS

01 June 2013 - 10:00 AM

I've layed the groundworks for a 2D isometric rts. But I have a couple basic questions if anyone would be willing to point me to an article or offer me advice.

 

1. Movement: So the movement I have implmented now is a basic Euler integration where I increment acceleration, velocity then position. Now that I have a tile map, sprite sheet and a few characters on screen I've quickly realized I can't have 2D movement with 360 degrees of freedom. I must restrict movement to the tiles on my map(duh!). Is the best way to do this going from point a->b simply to increment a.x and a.y until it = b.x and b.y? And have the character move from grid point to grid point? 

 

2. Storing Units: What type of data structures would be best to use for this type of game. There are many actions that are going to require a quick look up on a few units. Selecting units, collision detection, etc... How have you guys implemented this? I was thinking of perhaps doing some sort of map where the key is from ymin -> ymax where each bucket is +50px from the last or something. Then sorting from xmin->xmax in each bucket with the same +50px offset.

 

Best,

    Dish


Confusion About Rotating Camera Around Point

25 April 2013 - 01:55 PM

So I have a requirement to rotate around a scene. The scene is just a revolving sun with an earth and moon revolving around that.

 

I have to use gluLookAt() to rotate around that sceen. I do it with this code:

 

GLdouble theta = -anglex * PI / latiGrid;
GLdouble phi = angley * 2 * PI / longiGrid;
    
gluLookAt(eyeZ * cos(theta) * sin(phi), eyeZ * sin(theta), eyeZ * cos(theta) * cos(phi), 0, 0, 0, 0, 1, 0);

Where anglex and angley is simply the difference in angles of the X and Y screen coordinates... First I extracted this code from a similar project, so I was hoping someone could explain why it works in the first place. How are the eye coordinates derived exactly? I'm a bit at a loss of how this works.

 

Also, It rotates fine in the horizontal direction, but in the vertical direction it gets to the top it translates the orbiting earth from one end to the other.


PARTNERS