Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - anoynlk

Pages: [1]
1
General / Re: Inverting velocity to bounce an object (along x-axis)
« on: April 17, 2017, 02:44:18 pm »
Ok so I have managed to solve this!

in the IF statement I changed it to getPosition().x instead of position.x

So it looks like this:
Code: [Select]
void Enemy3::update(float dt)
{
// moving from left to right of the screen and having negative gravity effecting the y movement
if ([b]getPosition.x[/b] >= 800)
{
velocity.x= -velocity.x;
}
if ([b]getPosition.x[/b] <= 0)
{
velocity.x = -velocity.x;
}
move(velocity * dt);
updateAABB(); // updating AABB collision
}

Incase anyone else has a similar problem!

2
General / Re: Inverting velocity to bounce an object (along x-axis)
« on: April 17, 2017, 10:49:09 am »
Sorry I really should have just linked the entire .update(); functions code, it is actually still there I just linked the code I was using to move the object.

Is it worth sitting transparent objects at the side of the screen and trying to invert the velocity when they hit them? I feel this would not be efficient as it is pretty much unnecessary collision checks being made every frame.

3
General / Re: Inverting velocity to bounce an object (along x-axis)
« on: April 16, 2017, 04:38:33 pm »
This post is talking about collision however I am simply looking to invert the x velocity when it reaches a certain point in the window.

Is there somthing else to it other than velocity.x = -velocity.x ?

I should note if I move the enemy by updating the position using step variable such as
Code: [Select]
if (position.x >= 800)
{
    position.x = 799;
    step = -step;
}
if (position.x <= 0)
{
    position.x = 1;
    step = -step;
}
position.x += position * step;
setPosition(position);

This works.

However if I do it this way the object will not increase its Y velocity as time goes on.

This is a problem because ALL enemies take the player velocity every frame and use it as there own. The player does not actually move, however it calculates what its velocity should be each frame so that all other objects in the game can use it (and apply it negativley to there own y direction) to give the illusion of the player falling. (this was the best way I could code this to make sure all enemies move at the same speed).

4
General / Re: Inverting velocity to bounce an object (along x-axis)
« on: April 15, 2017, 09:28:12 pm »
No it inherits from: sf::RectangleShape

5
General / Re: Inverting velocity to bounce an object (along x-axis)
« on: April 15, 2017, 09:07:26 pm »
Hey,

I tried this, changed my code to:

Code: [Select]
        if (position.x >= 800)
{
position.x = 799;
velocity.x = -velocity.x;
}
if (position.x =< 0)
{
position.x = 1;
velocity.x = -velocity.x;
}
move(velocity * dt);
updateAABB(); // updating AABB collision

but still just going past the edge and offscreen.

Seems very simple and this logic should work i'm at a loss as to what i'm doing wrong.

6
General / Re: Inverting velocity to bounce an object (along x-axis)
« on: April 15, 2017, 12:13:14 am »
The problem is that it does not invert the velocity. It just travels off screen.

7
General / Inverting velocity to bounce an object (along x-axis)
« on: April 14, 2017, 02:57:25 pm »
Hey guys,

I'm programming a small game and apart from a few issues it's coming along nicley.

One problem I have is one of the enemies will move from left to right across the screen, he will also be affected by negative gravity to increase his velocity to travel upwards aswell.

For this i'm using:

Code: [Select]
move(velocity * dt);

The full code for inside it's update(dt); is as follows:

Code: [Select]
void Enemy3::update(float dt)
{
// moving from left to right of the screen and having negative gravity effecting the y movement
if (position.x >= 800)
{
velocity.x= -velocity.x;
}
if (position.x <= 0)
{
velocity.x = -velocity.x;
}
move(velocity * dt);
updateAABB(); // updating AABB collision
}

At the moment it will just keep traveling past the screen x edge untill it reaches the max y (800) and will respawn at the bottom of the screen - still traveling at a positive x velocity.

As far as i'm aware this is just inverting the velocity when it reaches the side of the screen? or have I missed somthing.

**I should note its Y velocity is being effected by a manager class which is setting its velocity by taking the players current velocity and matching it through the game.update();

Thanks

8
Yeah that's my fault for including vague code.

However simply changing

std::vector<Enemy1> Enemy1Manager::getEnemies()

to

std::vector<Enemy1>& Enemy1Manager::getEnemies()

has fixed it!

Thanks for the input guys! :)

9
Quote
std::vector<Enemy1> Enemy1Manager::getEnemies()
This function returns a copy of the enemy array, not the original one. Therefore, any modification to what is returned won't impact the actual array of enemies.

Oh I see.

I thought enemy1M.getEnemies().collisionResponse(); would find the correct enemy in the array.

Am I right in saying I must use pointers to solve this?

And if so could you help me set them up? I'm not very confident using them yet.

Thanks again for the time guys

10
Since you haven't shown what the internal value position actually does, I presume that this would be the internal representation of its position. This isn't being updated when you just set the position; it is being updated when you use collision response.

As for setPosition();

As far as I was aware this was from #include <SFML/Graphics.hpp>?
Only if the class inherits from sf::Transformable.

setPosition(); does indeed inherit from sf::Transmorable. The value of 'position' is set by giving the x and y values such as

Code: [Select]
position.x = xxx;
position.y = xxx;

setPosition(position);

Does that clear it up anymore?

Sorry if i'm being a noob I am pretty new to this!

11
Yes I mean it has not moved it anywhere. If I do the same for the player however it does indeed move it.
The problem I think lies within the enemies being managed by a manager class.

As for setPosition();

As far as I was aware this was from #include <SFML/Graphics.hpp>?

I should note also that when im changing its y position to offscreen, the manager for the enemy will then update on the next frame and because it is offscreen, it will kill it and respawn it at a new x & y pos.

Hope that makes sense

Thanks again for your time :)

12
I suppose, then, this transforms my question into:
what is an Enemy1 and what is in its setPosition method?

Enemy 1 is a class being managed by enemy manager. This is its constructor & update function:

Code: [Select]
Enemy1::Enemy1()
{
setSize(sf::Vector2f(50, 50));
position.x = 800;
position.y = 800;
setPosition(position);
scale = 10.f;
gravity = -5.0f * scale;
falling = true;
velocity.y = -100;
} // essentially making it travel upwards (all enemies are being being moved this way to give the illusion the player is falling


void Enemy1::update(float dt)
{
// this is always applying negative gravity to the enemy
if (falling)
{
velocity.y += (gravity)* dt;
move(velocity * dt);
}
updateAABB(); // updating AABB collision
}

Quote
'It not working' seems to be that it is not being moved with setPosition() but is being moved with collisionResponse(). Is that correct?

Unfortunately not. The collision response for the enemy is simply

Code: [Select]
void Enemy1::collisionResponse()
{
position.y = 850;
setPosition(position);
}

13
Also when you say;
How do you know? What does it do? Why is that different from what you expect it to do?

It doesn't seem to actually do anything, the object just continues on its normal path (-y axis). When it should(?) set it off screen.(the window i'm using is 800x800)

14
Hey Hapax thanks for responding!

the line
Code: [Select]
enemy1M.getEnemies()[i].setPosition(850, 850);

Does pretty much the exact same as the line

Code: [Select]
enemy1M.getEnemies()[i].collisionResponse();

As the collision response is simply to move the enemy off screen.

The enemy1M is an object of the enemy 1 manager class which is creating enemies using a vector like so

Code: [Select]
// inside the enemy 1 manager constructor
for (int i = 0; i < 6; i++)
{
enemies.push_back(Enemy1());
enemies[i].setAlive(false);
enemies[i].setTexture(&texture);
enemies[i].setSize(sf::Vector2f(50, 50));
}

and the line you quoted is the following;

Code: [Select]
// from the enemy 1 manager .cpp
std::vector<Enemy1> Enemy1Manager::getEnemies()
{
return enemies;
}

Hope this clears it up a bit, trying to be as informative as possible (i'm a noobie ;D)

thanks again!

15
General / Collision with enemies being managed by a 'manager' class
« on: April 03, 2017, 09:06:53 pm »
Hey guys,

Programming my first game using SFML here and encounter a problem when testing collision between the player and an enemy.

Too add some context, the player is falling past enemies and is using A + D to avoid them, when he collides with an enemy it will kill the enemy or move it off screen (thus killing it) and for testing purposes is moving the player to the top left of the screen aswell. The enemies are being managed by a manager class and the player is not.

code as follows for the collision check in game.update();
Code: [Select]
for (int i = 0; i < enemy1M.getEnemies().size(); i++)
{
if (enemy1M.getEnemies()[i].isAlive()) // check if alive first
{
// call the slow enemies function if they collide
player.collisionResponse();
enemy1M.getEnemies()[i].collisionResponse();
//enemy1M.getEnemies()[i].setPosition(850, 850); <-- this does not work either
}
}

if you need to see any other code let me know and i'll post asap (i'll be monitoring this thread) - didn't want to spam the page with unnecessary code!

Thanks for any time you contribute!   :)

Pages: [1]