SFML TransformToGlobal - bad idea?
Just working on a bit for a game, getting a simple square bouncing off the edges of the window. Presently, I have the window set at 800 x 600. I draw this box at 50, 50. It is 30 x 30 in size. I'm currently animating it, so it moves over to the right hand side of the screen. The problem is, even though I've done a check for this, it goes past the edge of the screen. However, if I start it at 0, 50 position, it does not. If I output the x value of the box (I use a class to store position and size info), it stays the same regardless.
I'm fairly certain this is because of where ever it's drawn, that is its origin or 0,0. I am wondering if using TransformToGlobal might help, by specifying the 50, 50 in that, and starting the box at 0, 0. My concern is that I don't want to get into a bad habit of using this function, where game objects or sprites should refer to origin as the local starting position.
here ya go sorry.
// Pong.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <SFML/Graphics.hpp>#include "Piece.hpp"#include <iostream>void updateBallPosition(Piece& ball, float ballSpeed, float timePassed);void updateBallPosition(Piece& ball, float ballSpeed, float timePassed){ float x = ball.GetX(); float y = ball.GetY(); float max_x = 800 - ball.GetSizeX(); float max_y = 600 - ball.GetSizeY(); if((x + (ballSpeed * timePassed)) <= max_x) { std::cout << "x: " << x << "\n"; ball.IncrementX(ballSpeed * timePassed); }}int _tmain(int argc, _TCHAR* argv[]){ sf::RenderWindow app(sf::VideoMode(800, 600, 32), "Pong Clone"); Piece theBall(50, 50, 30, 30); sf::Shape squarePiece; squarePiece.AddPoint(theBall.GetX(), theBall.GetY(), sf::Color(255, 255, 255), sf::Color(255, 255, 255)); squarePiece.AddPoint(theBall.GetRightX(), theBall.GetY(), sf::Color(255, 255, 255), sf::Color(255, 255, 255)); squarePiece.AddPoint(theBall.GetRightX(), theBall.GetBottomY(), sf::Color(255, 255, 255), sf::Color(255, 255, 255)); squarePiece.AddPoint(theBall.GetX(), theBall.GetBottomY(), sf::Color(255, 255, 255), sf::Color(255, 255, 255)); squarePiece.AddPoint(theBall.GetX(), theBall.GetY(), sf::Color(255, 255, 255), sf::Color(255, 255, 255)); squarePiece.EnableFill(true); app.SetFramerateLimit(100); app.UseVerticalSync(false); while(app.IsOpened()) { sf::Event e; while(app.GetEvent(e)) { if(e.Type == sf::Event::Closed) { app.Close(); } // if escape was pressed if((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Escape)) { app.Close(); } } float timePassed = app.GetFrameTime(); app.Clear(); app.Draw(squarePiece); app.Display(); squarePiece.SetX(theBall.GetX()); squarePiece.SetY(theBall.GetY()); updateBallPosition(theBall, 150.0f, timePassed); } return EXIT_SUCCESS;}
The squarePeice in your code states:
Create a rectangle sized 30 by 30, whose origin is at (-50, -50). Therefore when I translate the shape to (0,0), the top-left corner of the rectangle is at (50,50)
Try this:
This states:
Create a rectangle sized 30 by 30, whose origin is at (0, 0). Therefore when I translate the shape to (0,0), the top-left corner of the rectangle is at (0,0)
Create a rectangle sized 30 by 30, whose origin is at (-50, -50). Therefore when I translate the shape to (0,0), the top-left corner of the rectangle is at (50,50)
Try this:
const int width = 30;const int height = 30;Piece theBall(50, 50, width, height);sf::Shape squarePiece;const sf::Color& ballColor = sf::Color::White;squarePiece.AddPoint(0, 0, ballColor, ballColor);squarePiece.AddPoint(width, 0, ballColor, ballColor);squarePiece.AddPoint(width, height, ballColor, ballColor);squarePiece.AddPoint(0, height, ballColor, ballColor);squarePiece.AddPoint(0, 0, ballColor, ballColor);
This states:
Create a rectangle sized 30 by 30, whose origin is at (0, 0). Therefore when I translate the shape to (0,0), the top-left corner of the rectangle is at (0,0)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement