Rotating around 2 points at the same time - c++

Basically, I'm trying to rotate a square around 2 points at the same time.
Here, when mouse is going left or right I'm trying to rotate the square around it's center, and when mouse is going up or down, I'm rotating around any other point (50:100 here). However, my square is jumping around the screen, and when I'm trying to rotate around the center only, it continues to rotate around 50:100.
Any suggestions how to fix that?
#include <SFML/Graphics.hpp>
using namespace sf;
Event evt;
int main(int argc, char* argv)
{
RenderWindow window(VideoMode(600, 600), "test");
RectangleShape test(Vector2<float>(20.0f, 20.0f));
test.setFillColor(Color::Red);
test.setPosition(300, 300);
test.setOrigin(10, 10);
Clock deltaTime;
Clock timer;
timer.restart();
int mouseX = 0, mouseY = 0;
int curMouseX = 0, curMouseY = 0;
float offset = 100;
bool moving = false;
while (window.isOpen())
{
while (window.pollEvent(evt)) {
switch (evt.type)
{
case Event::MouseMoved:
curMouseX = mouseX;
curMouseY = mouseY;
mouseX = evt.mouseMove.x;
mouseY = evt.mouseMove.y;
moving = true;
break;
}
}
float elaspedTime = deltaTime.restart().asSeconds();
if (curMouseX != mouseX && moving) {
test.setOrigin(10, 10);
test.rotate(360 * elaspedTime);
// test.setOrigin(50, 100); Tried this to avoid jumping. When uncommented, doesn't rotate around the center.
}
if (curMouseY != mouseY && moving) {
test.setOrigin(50, 100);
test.rotate(60 * elaspedTime);
}
window.clear();
window.draw(test);
window.display();
moving = false;
}
return 0;
}

Instead of using the sf::Transfomable class inherited from your shape, create a sf::Transform for it and use rotate (float angle, const Vector2f &center)
You just have to store your mouse position each time it moves in a sf::Vector2f

Related

sfml gravity clipping shapes through floor

I tried to make a cube that moves side to side and bounces off the floor.
It bounced a couple times and then fell through the floor.
I tried making the floor higher.
I tried adding extra vertical velocity.
I have tried everything i can think of.
I would like to get the cube to not fall through the floor.
how do I do that?
#include <SFML/Graphics.hpp> <iostream>
int main(){
sf::RenderWindow window(sf::VideoMode(1000, 700), "project");
window.setFramerateLimit(60);
sf::RectangleShape rect;
int w = 100;
int h = 100;
rect.setSize(sf::Vector2f(w, h));
sf::Vector2f rectangle_position(500 - (w/2), 300 - (h/2));
rect.setPosition(rectangle_position);
float x_velocity = 3;
float y_velocity = 3;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
}
if (rectangle_position.x > 1000 - w) {
x_velocity = x_velocity * -1;
}
if (rectangle_position.x < 1 ) {
x_velocity = x_velocity * -1;
}
if (rectangle_position.y > 700 - h) {
y_velocity = y_velocity * -1;
}
if (rectangle_position.y < 50) {
y_velocity = y_velocity * -1;
}
y_velocity = y_velocity + 3;
rectangle_position.x = rectangle_position.x + x_velocity;
rectangle_position.y = rectangle_position.y + y_velocity;
rect.setPosition(rectangle_position);
window.clear();
window.draw(rect);
window.display();
}
}
In your implementation, once the bottom of the rectangle goes below the ground, you just reverse the velocity but never update the position of the rectangle. This causes the rectangle to sink below the ground.
You should make sure that the bottom of the rectangle never goes below the ground. This can be done by adding the following condition:
if (rectangle_position.y > 700 - h) {
// make sure that rectangle never goes below the ground
rectangle_position.y -= 3;
y_velocity = y_velocity * -1;
}
And the result is:
Hope it helps.

Possible to add multiple bounding boxes to one sprite and resize them in SFML?

I'm making a Pacman game using SFML. I have a background image for the maze that I'm using and I was wondering how I would be able to use multiple bounding boxes for the background so that when the yellow Pacman character collides with the walls, it stops him. Also, I was wondering if I could use multiple bounding boxes for the background maze image, if so, how?
My code:
#include <SFML/Graphics.hpp>
#include <iostream>
int main(int argc, char** argv)
{
sf::RenderWindow renderWindow(sf::VideoMode(280, 480), "Pacman" /*, sf::Style::Titlebar | sf::Style::Close*/);
sf::Event event;
sf::Texture texture, texture2;
texture.setSmooth (true);
texture2.setSmooth (true);
texture.loadFromFile("images/pacmanbkgd.png");
sf::Sprite background_sprite(texture);
background_sprite.setScale(sf::Vector2f(1.25, 1.25));
texture2.loadFromFile("images/transparentPacman3Sprites.png");
sf::IntRect rectSourceSprite(0, 0, 18, 16);
sf::Sprite pacmanMC_sprite(texture2, rectSourceSprite); // sf::intrect(int rectleft, int recttop, int rectwidth, int rectheight)
// pacmanMC_sprite.setScale(sf::Vector2f(1, 1));
pacmanMC_sprite.move(sf::Vector2f(200, 345)); // set sprite at inital location point (140, 145)
sf::Clock clock;
//sets the origin of the sprite
sf::Rect<float> size = pacmanMC_sprite.getGlobalBounds();
pacmanMC_sprite.setOrigin(sf::Vector2f(size.width / 2, size.height / 2));
while (renderWindow.isOpen())
{
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed)
renderWindow.close();
}
if (clock.getElapsedTime().asSeconds() > 0.75f) // 0.07, used for making the mouth faster
{
if (rectSourceSprite.left == 34)
rectSourceSprite.left = 0;
else
rectSourceSprite.left += 17; // pushes the sprite towards the left using x axis
pacmanMC_sprite.setTextureRect(rectSourceSprite);
clock.restart();
}
// pacmanMC_sprite movement
// sprite keeps on moving to a different position when a the correct key is pressed for its rotation
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
{
pacmanMC_sprite.move(0.0f, -0.015f);
pacmanMC_sprite.setRotation(-90.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
{
pacmanMC_sprite.move(0.0f, 0.015f);
pacmanMC_sprite.setRotation(90.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
{
pacmanMC_sprite.move(-0.015f, 0.0f);
pacmanMC_sprite.setRotation(-180.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
{
pacmanMC_sprite.move(0.015f, 0.0f);
pacmanMC_sprite.setRotation(0);
}
renderWindow.clear();
renderWindow.draw(background_sprite);
renderWindow.draw(pacmanMC_sprite);
renderWindow.display();
}
return 0;
}

I don't see the animation of the sprite

This is code after includes:
Sprite player;
Texture playerTexture;
IntRect playerContainer(0, 0, 32, 32);
Vector2f playerPosition;
int playerDirection = 0; // 0 - fwd, 1 - back, 2 - stay
This is animation update method:
void updateAnims(Clock clock, float time) {
if(time > 0.3f) {
if(playerDirection == 0) playerContainer.top = 0;
else if(playerDirection == 1) playerContainer.top = 32;
else if(playerDirection == 2) playerContainer.top = 64;
if(playerContainer.left == 96) playerContainer.left = 0;
else playerContainer.left += 32;
player.setTextureRect(playerContainer);
clock.restart();
}
}
and this method is updated in "int main()" method.
int main() {
// Init window
RenderWindow window(VideoMode(800, 600), "RPG");
Clock gameClock;
Clock animClock;
float gameTime;
float animTime;
// Setting up the player
playerTexture.loadFromFile("player.png");
player.setTexture(playerTexture);
player.setTextureRect(playerContainer);
player.setScale(Vector2f(3.f, 3.f));
playerPosition.x = 30;
playerPosition.y = 120;
player.setPosition(playerPosition);
while(window.isOpen()) {
Event event;
while(window.pollEvent(event)) {
if(event.type == Event::Closed) {
window.close();
}
}
checkInputs(gameTime);
animTime = animClock.getElapsedTime().asSeconds();
updateAnims(animClock, animTime);
window.clear();
gameTime = gameClock.getElapsedTime().asMilliseconds();
gameClock.restart();
window.display();
}
return 0;
}
Turns out that sprite is created in main method, gets texture and texture shape, gets position, but not drawn. Why?
I think think the problem is in animation method but I tried different variations of solutions.
You should call window.draw(player) method between window.clear() and window.display() to actually draw player sprite on screen.

SFML: Projectile stops

I am very new to SFML and C++. My problem is that I successfully fire projectiles towards the mouse. However as I move the mouse the projectiles also move.
EDIT:
Okay I solved my previous problem. However the projectile stops while it reaches the mouse position. How can the bullets continue to go further than its destination?
Code:
if (isFiring == true) {
sf::Vector2f startPos = sf::Vector2f(myPipe.getX(), myPipe.getY());
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2f endPos = sf::Vector2f(mousePos.x, mousePos.y);
double projSpeed{0.3};
//float projSpeed = projSpeed * (myPipe.getY() - mousePos.y) / 100;
Projectile newProjectile(20, startPos, endPos, projSpeed);
projVec.push_back(newProjectile);
isFiring = false;
}
for (int i = 0; i < projVec.size(); i++) {
projVec[i].fire(delta.asMilliseconds());
projVec[i].draw(window);
clock.restart();
}
fire function:
void fire( float delta) {
sf::Vector2f pos = projectile.getPosition();
float veloX = (_end.x - pos.x);
float veloY = (_end.y - pos.y);
float distance = sqrt((veloX*veloX + veloY * veloY));
float dx = veloX / distance;
float dy = veloY / distance;
projectile.move(dx * delta, dy * delta);
}
One more question, am I doing it correct with multiplying with delta? The bullets are really laggy and weird.
(Original reply:)
I don't have enough reputation to comment so I'll have to put it here...
So if I understand correctly you're saying you want to shoot the projectiles towards the mouse's position but don't want them to follow its position once fired? In this case:
I see you have the code to draw the projectiles inside that for-loop, which leads me to believe you are calling the loop continuously. But the same loop also contains the code used to shoot the projectiles, which means that you are also continuously calling this "fire" code. So you are steering the projectiles towards the current mouse position in every loop iteration which means they will always follow the current mouse position.
What you should be doing is move the code to shoot the projectiles and only call it once (you can only shoot a projectile once, right?) so a projectile's directional vector is not constantly altered, resulting in the problem you stated.
And make sure to always separate logic and rendering. Ideally you should be updating the physics (for example the projectile updating) at a fixed interval so that the game will run the same on all machines, and draw all your stuff separately.
If you do want the projectiles to always follow the mouse, you can use this (semi pseudo) code (I'm using this code in one of my games):
sf::Vector2f normalizedVector = normalize(mousePos - projectilePos);
normalizedVector.x *= speed * deltaTime;
normalizedVector.y *= speed * deltaTime;
projectile.move(normalizedVector);
(Edit:)
I made a quick example project that works. This doesn't use all of your code but hopefully gives you an idea how it can be done.
Here's a short video that shows what the code below does: https://webmshare.com/play/jQqvd
main.cpp
#include <SFML/Graphics.hpp>
#include "projectile.h"
int main() {
const sf::FloatRect viewRect(0.0f, 0.0f, 800.0f, 600.0f);
sf::RenderWindow window(sf::VideoMode(viewRect.width, viewRect.height), "Test");
window.setFramerateLimit(120);
window.setVerticalSyncEnabled(false);
window.setKeyRepeatEnabled(false);
sf::Clock deltaClock;
const sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Time timeSinceLastUpdate = sf::Time::Zero;
std::vector<Projectile> projectiles;
while (window.isOpen()) {
timeSinceLastUpdate += deltaClock.restart();
// process events
{
sf::Event evt;
while (window.pollEvent(evt)) {
switch (evt.type) {
case sf::Event::Closed: { window.close(); } break;
// shoot with left mouse button
case sf::Event::MouseButtonPressed: { switch (evt.mouseButton.button) { case sf::Mouse::Button::Left: {
const sf::Vector2f center(viewRect.left + viewRect.width / 2, viewRect.top + viewRect.height / 2);
const sf::Vector2f mousePos(window.mapPixelToCoords(sf::Mouse::getPosition(window)));
const float angle = atan2(mousePos.y - center.y, mousePos.x - center.x);
projectiles.push_back(Projectile(center, angle));
} break; default: {} break; } } break;
default: {} break;
}
}
}
// update
{
while (timeSinceLastUpdate > timePerFrame) {
timeSinceLastUpdate -= timePerFrame;
// update projectiles
{
for (std::size_t i = 0; i < projectiles.size(); ++i) {
Projectile &proj = projectiles[i];
proj.update(timePerFrame);
if (!viewRect.intersects(proj.getBoundingBox())) { proj.destroy(); }
}
projectiles.erase(std::remove_if(projectiles.begin(), projectiles.end(), [](Projectile const &p) { return p.getCanBeRemoved(); }), projectiles.end());
}
}
}
// render
{
window.clear();
for (std::size_t i = 0; i < projectiles.size(); ++i) {
window.draw(projectiles[i]);
}
window.display();
}
}
return EXIT_SUCCESS;
}
projectile.h:
#ifndef PROJECTILE_H_INCLUDED
#define PROJECTILE_H_INCLUDED
#include <SFML/Graphics.hpp>
class Projectile : public sf::Drawable {
public:
Projectile();
Projectile(const sf::Vector2f pos, const float angle);
virtual ~Projectile();
const bool &getCanBeRemoved() const;
const sf::FloatRect &getBoundingBox() const;
void destroy();
void update(const sf::Time dt);
private:
virtual void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const;
bool canBeRemoved_;
sf::FloatRect boundingBox_;
float angle_;
float speed_;
sf::RectangleShape shape_;
};
#endif
projectile.cpp:
#include "projectile.h"
Projectile::Projectile() :
canBeRemoved_(true),
boundingBox_(sf::FloatRect()),
angle_(0.0f),
speed_(0.0f)
{
}
Projectile::Projectile(const sf::Vector2f pos, const float angle) {
canBeRemoved_ = false;
boundingBox_ = sf::FloatRect(pos, sf::Vector2f(10.0f, 10.0f));
angle_ = angle;
speed_ = 0.5f;
shape_.setPosition(sf::Vector2f(boundingBox_.left, boundingBox_.top));
shape_.setSize(sf::Vector2f(boundingBox_.width, boundingBox_.height));
shape_.setFillColor(sf::Color(255, 255, 255));
}
Projectile::~Projectile() {
}
const bool &Projectile::getCanBeRemoved() const {
return canBeRemoved_;
}
const sf::FloatRect &Projectile::getBoundingBox() const {
return boundingBox_;
}
void Projectile::destroy() {
canBeRemoved_ = true;
}
void Projectile::update(const sf::Time dt) {
boundingBox_.left += static_cast<float>(std::cos(angle_) * speed_ * dt.asMilliseconds());
boundingBox_.top += static_cast<float>(std::sin(angle_) * speed_ * dt.asMilliseconds());
shape_.setPosition(boundingBox_.left, boundingBox_.top);
}
void Projectile::draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const {
renderTarget.draw(shape_);
}

How can I set gravity using this code?

I am trying to make a game and am stuck on gravity..... In the following code a rectangle stands for a player and when I press up key it moves in y-axis but when I activate gravity on it (i.e resetting its previous position) it does not animate (i.e. It does not jumps) instead it just stays in its position. I am using SFML library of C++ and that's a game development tool. Please Help!
#include <SFML/Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Gravity");
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(100, 100));
rectangle.setFillColor(sf::Color::Black);
rectangle.setPosition(sf::Vector2f(10, 350));
while(window.isOpen())
{
sf::Event Event;
while(window.pollEvent(Event))
{
if(Event.type == sf::Event::Closed)
{
window.close();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
rectangle.move(0, -1);
}
if(rectangle.getPosition().y >= 350-1)
{
rectangle.setPosition(0, 350);
}
window.display();
window.clear(sf::Color::Cyan);
window.draw(rectangle);
}
}
Theoretically your code would work, but there's one significant problem:
Your initial position is 350.
Now your "jumping code" (which will allow the player to fly indefinitely!) triggers and your position is changed to 349.
However, your code keeping the player from dropping off the screen (y >= 350-1) essentially resolves to the check y >= 349, which will be true, so your position is permanently reset to 350.
To solve this, just remove the -1 or replace the >= operator with >.
While your approach should be working (once the fix above is applied), you should rethink your strategy and store a velocity in addition to a position. I've recently written the following example code. It's far from being perfect, but it should teach you a few basics for a jump and run game (not necessarily the only way to do such things):
Allow the player to jump.
Apply gravity.
Allow the player to determine jump height based on how long he holds down a key.
#include <SFML/Graphics.hpp>
int main(int argc, char **argv) {
sf::RenderWindow window;
sf::Event event;
sf::RectangleShape box(sf::Vector2f(32, 32));
box.setFillColor(sf::Color::White);
box.setOrigin(16, 32);
box.setPosition(320, 240);
window.create(sf::VideoMode(640, 480), "Jumping Box [cursor keys + space]");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(false);
// player position
sf::Vector2f pos(320, 240);
// player velocity (per frame)
sf::Vector2f vel(0, 0);
// gravity (per frame)
sf::Vector2f gravity(0, .5f);
// max fall velocity
const float maxfall = 5;
// run acceleration
const float runacc = .25f;
// max run velocity
const float maxrun = 2.5f;
// jump acceleration
const float jumpacc = -1;
// number of frames to accelerate in
const unsigned char jumpframes = 10;
// counts the number of frames where you can still accelerate
unsigned char jumpcounter = 0;
// inputs
bool left = false;
bool right = false;
bool jump = false;
while (window.isOpen()) {
while (window.pollEvent(event)) {
switch(event.type) {
case sf::Event::KeyPressed:
case sf::Event::KeyReleased:
switch (event.key.code) {
case sf::Keyboard::Escape:
window.close();
break;
case sf::Keyboard::Left:
left = event.type == sf::Event::KeyPressed;
break;
case sf::Keyboard::Right:
right = event.type == sf::Event::KeyPressed;
break;
case sf::Keyboard::Space:
jump = event.type == sf::Event::KeyPressed;
break;
}
break;
case sf::Event::Closed:
window.close();
break;
}
}
// logic update start
// first, apply velocities
pos += vel;
// determine whether the player is on the ground
const bool onground = pos.y >= 480;
// now update the velocity by...
// ...updating gravity
vel += gravity;
// ...capping gravity
if (vel.y > maxfall)
vel.y = maxfall;
if (left) { // running to the left
vel.x -= runacc;
}
else if (right) { // running to the right
vel.x += runacc;
}
else { // not running anymore; slowing down each frame
vel.x *= 0.9;
}
// jumping
if (jump) {
if (onground) { // on the ground
vel.y += jumpacc * 2;
jumpcounter = jumpframes;
}
else if (jumpcounter > 0) { // first few frames in the air
vel.y += jumpacc;
jumpcounter--;
}
}
else { // jump key released, stop acceleration
jumpcounter = 0;
}
// check for collision with the ground
if (pos.y > 480) {
vel.y = 0;
pos.y = 480;
}
// check for collision with the left border
if (pos.x < 16) {
vel.x = 0;
pos.x = 16;
}
else if (pos.x > 624) {
vel.x = 0;
pos.x = 624;
}
// logic update end
// update the position
box.setPosition(pos);
window.clear();
window.draw(box);
window.display();
}
return 0;
}