Aug 14, 2014 at 11:14pm UTC
Its just like any other component based entity system. An object is made up of smaller components instead of a very large class hierarchy.
//velocity += accel * deltaTime.restart().asSeconds();
As I mentioned
//get user input
//change the x or y velocity accordingly
so..It would really look something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//WASD Movement
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
velocity.x -= accel * deltaTime.restart().asSeconds();
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
velocity.x += accel * deltaTime.restart().asSeconds();
}
else
{
velocity.x *= decel * deltaTime.restart().asSeconds();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
velocity.y -= accel * deltaTime.restart().asSeconds();
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
velocity.y += accel * deltaTime.restart().asSeconds();
}
else
{
velocity.y *= decel * deltaTime.restart().asSeconds();
}
Also, why are you multiplying by decel? Should you be adding the decel and it would be a negative number?
Sorry actually you wouldn't restart the clock each time so it would actually be something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
float deltaTime = clock.restart().asSeconds();
//WASD Movement
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
velocity.x -= accel * deltaTime;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
velocity.x += accel * deltaTime;
}
else
{
velocity.x *= decel * deltaTime;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
velocity.y -= accel * deltaTime;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
velocity.y += accel * deltaTime;
}
else
{
velocity.y *= decel * deltaTime;
}
Last edited on Aug 14, 2014 at 11:17pm UTC
Aug 15, 2014 at 5:00am UTC
Hmm, well my sprite moves like a vehicle with that code, which is not what I want, so i'll stick with what I have. However I still need to figure out the collision problem with the sprite not stopping when hitting the edge of the screen.
Aug 15, 2014 at 7:46pm UTC
When you say position.x = foo;
you are forcing the object to be at that position.
So if you want to the object to be on the right side of the screen.......
Aug 15, 2014 at 8:02pm UTC
ahhhh, i see, so the origin is set to 0, 0, so I should make an origin point for the bottom right and then it should stop right? I set up a bounding box could I just use that? I'm not sure if i did the bounding box right though
//Collision
sf::FloatRect boundingBox = sprite.getGlobalBounds();
sf::Vector2f point = sprite.getPosition();
if(boundingBox.contains(point))
{
}
Last edited on Aug 15, 2014 at 8:04pm UTC
Aug 15, 2014 at 8:18pm UTC
So like this?:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if (position.x <= 0)
{
position.x = 0;
}
if (position.y <= 0)
{
position.y = 0;
}
if (position.x >= currWSize.x)
{
position.x = currWSize.x - sprite.getGlobalBounds().width;
}
if (position.y >= currWSize.y)
{
position.y = currWSize.y - sprite.getGlobalBounds().height;
}
this sort of works but the sprite goes almost all the way off screen.
Last edited on Aug 15, 2014 at 8:21pm UTC
Aug 15, 2014 at 8:28pm UTC
Nevermind I did it :D I just stopped and did it step by step of what the square was doing and it works :D