Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


mcjohnalds45

Member Since 14 Apr 2012
Offline Last Active Aug 16 2012 07:32 PM

Topics I've Started

Using atan2 to find an angle between two points

16 April 2012 - 06:58 AM

I'm trying to find the angle aimDirection with this code.

aimDirection = atan2(mouseY - vertical centre of box, mouseX - horizontal centre of box) * 180 / pi; // what I'm doing in english

aimDirection = atan2((double)(mouseY - hitBox.y + (hitBox.h / 2)), (double)(mouseX - hitBox.x + (hitBox.w / 2))) * 180 / M_PI; // actual code

I thought you could use atan2 like so to find an angle, maybe I'm wrong.

angle = atan2(y2 - y1, x2 - x1) * 180 / pi.

I don't know how to describe the situation clearly so I made a pretty picture instead.

Posted Image

But the angle is never what I expect (depending on where I move my mouse vs the middle of the box) when I run the program.

(Sorry if I don't reply till tomorrow, it's getting late and I don't want to be drowsy for school Posted Image)

SDL_Events working in my main function but not in a class

14 April 2012 - 05:42 AM

First post, hooray! I'm trying to call onEvent (a function of my player class) every time I call SDL_PollEvent in my game loop to move my player up when I press W.

Part of player definition.
void Player::onEvent(SDL_Event Events)
{
	if (events.type == SDL_KEYDOWN)
	{
		switch (Events.key.keysym.sym)
		{
		case SDLK_w: ySpeed = -maxSpeed; break;
		}
	}
	if (events.type == SDL_KEYUP)
	{
		switch (Events.key.keysym.sym)
		{
		case SDLK_w: ySpeed = 0; break;
		}
	}
}

Part of my main function where onEvent is called.
while (SDL_PollEvent(&events))
{
	if (events.type == SDL_QUIT)
	run = false;
	player.onEvent(events);
}

I expect my player to move up when I press W but it does not, I am almost certain there is something wrong with onEvent's definition or how I am calling it because changing the above piece of code to the following makes the program run as expected.


while (SDL_PollEvent(&events))
{
	if (events.type == SDL_QUIT)
		run = false;
	if (events.type == SDL_KEYDOWN)
	{
		switch (events.key.keysym.sym)
		{
		case SDLK_w: player.ySpeed = -player.maxSpeed; break;
		}
	}
	if (events.type == SDL_KEYUP)
	{
		switch (events.key.keysym.sym)
		{
		case SDLK_w: player.ySpeed = 0; break;
		}
	}
}

Sorry for goofing up any terminology, I'm pretty new to c++.

PARTNERS