Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 16 Mar 2011
Offline Last Active Yesterday, 09:31 PM

Topics I've Started

How to get correct coords of the sprite

Yesterday, 09:30 PM

I am having real trouble with making my program highlight correct tile on the grid when I hover over with the mouse (over the tile on the grid). I have my grid drawing correctly and my marker tile is working, but when I so much as move the camera, coordinates don't get updated for the tiles on the grid when I do sprite.getGlobalBounds() part.


I am not sure what am I doing wrong, here is the relevant code

// create an array of 100 sprites for 10x10 grid
	sf::Sprite spr[100];
	// x and y coordinates for grid tile placement
	float x = 0.0f;
	float y = 0.0f;

	// create 10x10 grid
	for(int i = 0; i < 100; i++)
		// scale sprite using formula -> target size / original size for both x and y
		//spr[i].setScale(sf::Vector2f(48.0f / spr[i].getLocalBounds().width, 48.0f / spr[i].getLocalBounds().height));
		if(i < 100)
			spr[i].setPosition(96.0f * (i % 10), y + (96.0f * (i / 10)));

	// create tile marker
	sf::Sprite marker;
	marker.setColor(sf::Color(255, 0, 0));
	marker.setPosition(-96.0f, -96.0f);

	// store mouse position
	sf::Vector2i Mouse;

	// create a view with the rectangular area of the 2D world to show
	sf::View view;
	view.setSize(800.0f, 600.0f);
	view.setCenter(sf::Vector2f(400.0f, 300.0f));

	// loop until a WM_QUIT message is received
	msg.message = static_cast<UINT>(~WM_QUIT);
	while(msg.message != WM_QUIT)
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
			// Win32 part
		else // SFML part
			SFMLView.clear(sf::Color(64, 64, 64));

			for(int i = 0; i < 100; i++)
				SFMLView.draw(spr[i]); // draw grid sprite

			// check mouse position
			Mouse = sf::Mouse::getPosition(SFMLView);

			if(Mouse.x > spr[0].getGlobalBounds().left && 
				Mouse.x < (spr[0].getGlobalBounds().left + spr[0].getGlobalBounds().width) && 
				Mouse.y > spr[0].getGlobalBounds().top && 
				Mouse.y < (spr[0].getGlobalBounds().top + spr[0].getGlobalBounds().height))
					marker.setPosition(0.0f, 0.0f);
				marker.setPosition(-96.0f, -96.0f);


storing level data

19 August 2014 - 02:30 PM

I am looking to create 2D game (maybe RPG, maybe rogue-like, maybe both biggrin.png), but i am facing a problem coming up with a solution for storing level data. I want to create initial level structure through tile system, but I don't want game it self to be dependent on tiles.


All levels can be created with tiles, that is tiles for ground, walls and other stuff, but than I would like player and other characters to be moving in freely without being constrained tiles and have fallout type zig-zaging and such. What confuses me is the fact that tiles that were used to construct the level would be written in a file in a way that would make sense to a human.


How should i go around storing data for a character's position if its not constrained to tiles? 

math problems

23 April 2014 - 12:48 PM

I am trying to implement accurate selection tool for my level editor that would be able to use both isometric tiles and isometric hexagons. with the same sprite dimensions. I am now focusing on getting regular isometric tile to work properly.


Below is the color coded isometric tile that i will be using (it will be transparent with only lines visible, this one is for explanation only). I want my program to register specific tile if the cursor is only in green area.




The sprite has dimensions of 192x96. How do I calculate if the mouse is only in green area? Since from what I find everything is based on rectangle logic, because I check X, and Y coordinates of the mouse.


From here I have something like


if(mouse.x > 0 && mouse.y < 48)

  if(mouse.x < 96 && mouse.y < 48)

    select tile


but these coordinates also catch stuff from red triangle area in top left.

array incrementation

21 February 2014 - 09:26 PM

I wanted to draw a grid with array of sprites. And I made it work with this code

// loop over each sprite, setting their textures
// grid
for(int i = 0; i < 20; i++)
	spr[i].scale(sf::Vector2f(0.55f, 0.55f));

	// 1st row
	if(i < 10)
		spr[i].setPosition(105.6f * i, y);
	// 2nd row
	if(i > 10 || i < 20)
		spr[i+10].setPosition(105.6f * i, y + 52.8f);
	// 3rd row
	//if(i > 20 || i < 30)
	//	spr[i+20].setPosition(105.6f * i, y + 105.6f);


Now I want to optimize this code so I can use it more efficiently later. The code brakes if I set spr[i] instead of spr[i+10] in second if loop. Why is this? How can I continue through the array without +10 or whatever next to it?

How is CSG done?

30 January 2014 - 09:56 AM

This question is one of the reasons I am much happier with programming that doesn't involve anything low-level. Since this is way higher tier math for my understanding when it comes to implementation, and there is not much info on this floating around the internet, I decided to ask here.


Here is the link http://en.wikipedia.org/wiki/Constructive_solid_geometry


I would like to know how is this calculated, and how do people store this kind of data when they make an engine (first thing that comes to mind, when looking at level editors). I find this interesting since I would need to store entire geometric data (vertices, faces, normals) and uv's along with this when I get the end result of the csg operation.