Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


proanim

Member Since 16 Mar 2011
Offline Last Active Feb 06 2015 09:03 PM

Topics I've Started

floating sprite problem

05 February 2015 - 11:00 AM

I have a sprite that is supposed to stay in place when I move the camera around the level, but when I try to move it my sprite floats out of position while camera is moving. How can I fix this? I tried to do 'fix your time step' thing but something tells me I am doing it the wrong way.

 

Here are the screenshots that I cropped out to illustrate the problem.

 

This one is the correct position when camera is not moving: https://dl.dropboxusercontent.com/u/118608180/correct_position.png

 

And these are the screenshots what happens depending on which side i choose to move the camera: 

https://dl.dropboxusercontent.com/u/118608180/incorrect_position_1.png (if i move camera up-left)

https://dl.dropboxusercontent.com/u/118608180/incorrect_position_2.png (if i move camera down)

https://dl.dropboxusercontent.com/u/118608180/incorrect_position_3.png (if i move camera right)

 

And here is a releveant portion of the code I used:

float dt = 0.0f;

// loop until a WM_QUIT message is received
msg.message = static_cast<UINT>(~WM_QUIT);
while(msg.message != WM_QUIT)
{
	sf::Event Event;

	if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
	{
		// Win32 part
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	else // SFML part
	{
		// start frame clock
		timer.start();

		RenderView.clear(sf::Color(64, 64, 64));

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

		// set camera move speed
		cam->SetCamSpeed(1);
		dt = timer.msec;

		while(RenderView.pollEvent(Event))
		{
			if(Event.type == sf::Event::KeyPressed)
			{
				if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) // move camera up
				{
					cam->CamOffsetY -= cam->CamSpeed * dt;
					StandardView.move(0.0f, -(cam->CamSpeed * dt));
				}
				if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) // move camera down
				{
					cam->CamOffsetY += cam->CamSpeed * dt;
					StandardView.move(0.0f, cam->CamSpeed * dt);
				}
				if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) // move camera left
				{
					cam->CamOffsetX -= cam->CamSpeed * dt;
					StandardView.move(-(cam->CamSpeed * dt), 0.0f);
				}
				if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) // move camera right
				{
					cam->CamOffsetX += cam->CamSpeed * dt;
					StandardView.move(cam->CamSpeed * dt, 0.0f);
				}
			}
		}

		// find map tile that has mouse cursor on it
		for(unsigned int i = 0; i < spr.size(); i++)
		{
			if(Mouse.x + cam->CamOffsetX > spr[i].getGlobalBounds().left &&
				Mouse.x + cam->CamOffsetX < spr[i].getGlobalBounds().left + spr[i].getGlobalBounds().width &&
				Mouse.y + cam->CamOffsetY > spr[i].getGlobalBounds().top &&
				Mouse.y + cam->CamOffsetY < spr[i].getGlobalBounds().top + spr[i].getGlobalBounds().height)
			{
				ValidMarker = 1;
				marker.setPosition(spr[i].getPosition().x, spr[i].getPosition().y);
				break;
			}
			else
				ValidMarker = 0;
		}

		for(unsigned int i = 0; i < spr.size(); i++)
			RenderView.draw(spr[i]); // draw grid sprites
		
		RenderView.setView(StandardView);
		if(ValidMarker == 1) // draw marker if mouse is hovering over tiles
			RenderView.draw(marker);
		RenderView.draw(CharacterPawn);
		RenderView.display();
		
		// stop frame clock
		timer.stop();
	}
}

Crash with <unable to read memory> error

30 November 2014 - 08:52 PM

I don't know how to fix this error that popped up when I tried to assign value of variable to another variable. Program crashes when I try to assign value of variable B to variable A, this thing should not happen, and I think I already tried everything. This is what I have:

class GameMap
{
public:
	GameMap();
	~GameMap();

	int SetMapSizeX(int SizeX);
	int SetMapSizeY(int SizeY);

	int GetMapSizeX();
	int GetMapSizeY();
	int GetMapSize();

	int MapSizeX;
	int MapSizeY;
	int MapTotalSize;
};

GameMap::GameMap()
{
	MapSizeX = 0;
	MapSizeY = 0;
	MapTotalSize = 0;
}

GameMap::~GameMap()
{
}

int GameMap::SetMapSizeX(int SizeX)
{
	return MapSizeX = SizeX;
}

int GameMap::SetMapSizeY(int SizeY)
{
	return MapSizeY = SizeY;
}

int GameMap::GetMapSizeX()
{
	return  MapSizeX;
}

int GameMap::GetMapSizeY()
{
	return MapSizeY;
}

int GameMap::GetMapSize()
{
	GetMapSizeX();
	GetMapSizeY();

	return MapTotalSize = MapSizeX + MapSizeY;
}

Code maybe looks bad but it, had to separate everything so I can findout what exactly caused this error.

So, the crash is on this line:

return MapSizeX = SizeX;

I am not sure what am I doing wrong here.


How to get correct coords of the sprite

30 October 2014 - 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++)
	{
		spr[i].setTexture(tex);
		// 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.setTexture(tex);
	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));
	view.zoom(2.0f);
	SFMLView.setView(view);

	// 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
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		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);
			}
			else
				marker.setPosition(-96.0f, -96.0f);

			SFMLView.draw(marker);
			SFMLView.display();
		}
	}

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.

 

tile_spr.jpg

 

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.


PARTNERS