Welcome, Guest. Please Login or Register.  • Help
SMF Underground
+ SHMUP-DEV » SHMUP DEV FORUMS » Assistance
|-+ Bullet keep following mouse please Help c++/sfml

Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read December 19, 2011, 09:04:29 PM #0
Fantasy

Bullet keep following mouse please Help c++/sfml

Hello
can you guys please please help me. I'm going insane here.
I wrote a piece of code that when you click on the mouse the bullet check for the mouse angle and fire in that direction.
but the problem is the bullet keep following the mouse. its not suppose to do that. it should check for the mouse position and angle and fire in that direction. can you please help.

Code:
void Bullet::Draw(sf::RenderWindow &Game)
{

float ElapsedTime = Game.GetFrameTime()/60;

int MouseX = Game.GetInput().GetMouseX();
int MouseY = Game.GetInput().GetMouseY();

double PI =3.14159265;
if(Counter>=0)
{
for(int i=0; i<=Counter; i++)
{
float Speed = 10;

float BulletX = Sprite[i].GetPosition().x;
float BulletY = Sprite[i].GetPosition().y;

float AngleX = MouseX - BulletX;
float AngleY = MouseY - BulletY;

float vectorLength = sqrt(AngleX*AngleX + AngleY*AngleY);

float DirectionX = AngleX / vectorLength;
float DirectionY = AngleY / vectorLength;

float VelocityX = DirectionX * Speed;
float VelocityY = DirectionY * Speed;


Sprite[i].Move (VelocityX, VelocityY);


Game.Draw(Sprite[i]);
}
}
}
Offline  
Read December 19, 2011, 10:55:41 PM #1
Hornet600S

Re: Bullet keep following mouse please Help c++/sfml

This happens because each time you run that update loop you get the most current mouse position for the following calculations.
In contrast to what you say this piece of code is not suited for being executed on mouse-click, it is suited for running triggered by a timer to let a bunch of bullets follow the mouse-cursor.

If you want to change that you have to:

1. extend your sprite struct by a MouseX / MouseY attribute (or by a direction vector).

2. make a function that spawns a bullet on mouse-click. Inside that function you set Sprite[Counter].MouseX and .MouseY (or do the direction calculus). And don't forget to increase Counter and check for overflow.

3. remove the MouseXY stuff from the Draw function. It is a draw-function, no input function. Instead use the Sprite's MouseX/Y for the calculations.


"When the Amiga came out, everyone at Apple was scared as hell."
(Jean-Louis Gassée - Head of Macintosh development at Apple)
Offline  
Read December 20, 2011, 06:34:08 AM #2
Fantasy

Re: Bullet keep following mouse please Help c++/sfml

Thank you the replay i really appreciate it. However i think i did what you said (not sure) and its still not working. I'm sorry but I'm a newbie i spent the last 4 hours reading, researching and coding trying to fix the stupid problem but i just can't. Now the problem is that the when i click the mouse the bullets move only 1 pixel and spawn new bullet.

I created a new function called createBullet() which handles the calculations and Init() get mouse X and Y positions. I call CreateBullet() when the mouse is clicked.

Bullet.h
Code:
#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
#include <sstream>

class Bullet
{
public:
Bullet();
~Bullet();
void LoadFile(sf::RenderWindow &Game);
void Init(sf::RenderWindow &Game);
void SetPosition(sf::RenderWindow &Game);
void MouseEvent(sf::Event &Event);
void CreateBullet(sf::RenderWindow &Game);
void Draw(sf::RenderWindow &Game);

bool Clicked;
sf::RenderWindow Game;
private:
sf::Image Image;
sf::Sprite Sprite[101];
int Direction, Amenition, Amenitioncoco, MouseX, MouseY, Counter;

float BulletX, BulletY, AngleX, AngleY, vectorLength,
DirectionX, DirectionY, VelocityX, VelocityY, Speed;
double PI;
};


Bullet.cpp
Code:
#include "Bullet.h"
#include "Player.h"
#include <math.h>

Player P;
Bullet::Bullet()
{
Clicked=false;
Speed = 10;
Counter = -1;
Amenition = 100;
PI =3.14159265;
}

Bullet::~Bullet()
{
}

void Bullet::LoadFile(sf::RenderWindow &Game)
{
Image.LoadFromFile("data/Images/Bullet.png");
}

void Bullet::Init(sf::RenderWindow &Game)
{
MouseX = Game.GetInput().GetMouseX();
MouseY = Game.GetInput().GetMouseY();
}

void Bullet::MouseEvent(sf::Event &Event)
{
Amenitioncoco = Amenition - Counter;
if(Event.Type == sf::Event::MouseButtonPressed  && Event.MouseButton.Button == sf::Mouse::Left && Amenitioncoco !=0)
{
Clicked=true;
Counter++;
CreateBullet(Game);
}
}

void Bullet::SetPosition(sf::RenderWindow &Game)
{
P.Movement(Game);

if(Clicked==true)
{
for(int i=Counter; i<=Counter; i++)
{
Sprite[i].SetImage(Image);
Sprite[i].SetPosition(P.GetPlayerX(), P.GetPlayerY());
}

Clicked=false;
}
}

void Bullet::CreateBullet(sf::RenderWindow &Game)
{
float ElapsedTime = Game.GetFrameTime()/60;

if(Counter>=0)
{
for(int i=0; i<=Counter; i++)
{
BulletX = Sprite[i].GetPosition().x;
BulletY = Sprite[i].GetPosition().y;

AngleX = MouseX - BulletX;
AngleY = MouseY - BulletY;

vectorLength = sqrt(AngleX*AngleX + AngleY*AngleY);

DirectionX = AngleX / vectorLength;
DirectionY = AngleY / vectorLength;

VelocityX = DirectionX * Speed;
VelocityY = DirectionY * Speed;

Sprite[i].Move (VelocityX , VelocityY );
}
}
}

void Bullet::Draw(sf::RenderWindow &Game)
{
//Display Amo
std::stringstream out;
out << "Amo: " << Amenitioncoco;
std::string myString;
myString = out.str();
sf::Unicode::Text myText(myString);
sf::String Amo (myText, sf::Font::GetDefaultFont(), 15);
Amo.SetPosition(700,550);
Game.Draw(Amo);

if(Counter>=0)
{
for(int i=0; i<=Counter; i++)
{
Game.Draw(Sprite[i]);
}
}
}
« Last Edit: December 20, 2011, 06:35:50 AM by Fantasy »
Offline  
Read December 20, 2011, 02:47:29 PM #3
motorherp

Re: Bullet keep following mouse please Help c++/sfml

No offense Fantasy, but the types of issues you're describing here are really just simple logical errors in your programming.  These are really the kinds of things you should be able to fix yourself quickly and easily by becoming more familiar with your debugger.  Learn how to use the various features of your IDE such as how to place breaks points in your code, step through it line by line when its running, jump in and out of functions, and inspect the values of variables.  Once you're familiar with these techniques you'll quickly be able to solve these sorts of simple issues for yourself quickly and easily by following the flow of your code and assuring its doing what you expected.


Offline  
Pages: [1]   Go Up
Jump to:  

Page created in 0.081 seconds with 18 queries.