سؤال

Well friends, I have returned here to try and get a little help once again. The issue is this, the ship on build doesn't wrap. That is to say it doesn't reappear on the other side of the window when it goes past the windows limit. The function called setmaxLocations() is being very strange. It erases the ship, or doesn't set the max location. So here is my code. Bear with me, this is a large project. If I messed up the format just let me know, I check often so it'll get fixed close to when you tell me.

main implementation cpp:

   #include <SFML/Graphics.hpp>
   #include "ship.h"

   const int WINDOW_WIDTH = 700;
   const int WINDOW_HEIGHT = 700;
   //==============================================================================
int main()
{
   Ship ship;


   sf::RenderWindow window( sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), 
    "Delta Quadrant", sf::Style::Titlebar | sf::Style::Close);

    window.setFramerateLimit(120); 
    // this causes loop to execute 120 times a second at most.
    // (a delay is automatically added after screen is drawn)

    //set's the limit on the ship's location to the window dimensions;
    ship.setmaxLocations(WINDOW_WIDTH, WINDOW_HEIGHT);

    //sets position of the ship in the middle of the screen
    ship.setLocation(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);

    while (window.isOpen())
{
    //----------------------------------------------------------
    //handle user input (events and keyboard keys being pressed)
    sf::Event event;
    while (window.pollEvent(event)) {
        if (event.type == sf::Event::Closed)
            window.close();
    }

    //turn left with press of left button
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        ship.rotateLeft();

    //turn right with press of right button
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        ship.rotateRight();

    //apply thrust with press of up button
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        ship.applyThrust();

    //----------------------------------------------------------
    //draw new frame
    window.clear();

    //draw ship
    ship.updateLocation();

    ship.draw(window);


    //redisplay window
    window.display();

}

return 0;
}

SpaceObject header:

#ifndef SPACE_OBJECT_H
#define SPACE_OBJECT_H

#include "vector.h"

class SpaceObject  {
private: 
    Vector maxLocations;    //maximum allowable values for location
    Vector location;        //current location (x,y)
    Vector velocity;        //current velocity (in pixels/frame)
    double angleDeg;        //angle ship is facing, in degrees
    double radius;      //gross radius (for collision detection)

public:
    SpaceObject();

    //--------------------------------------------
    //mutators
    void setLocation(double x, double y);
    void setVelocity(double velocityX, double velocityY);
    void setAngle(double angDeg);
    void setRadius(double radius);

    //--------------------------------------------
    //accessors
    Vector getLocation();
    Vector getVelocity();
    double getAngle();
    double getRadius();
    //--------------------------------------------
    //others
    void updateLocation();
    void setmaxLocations(double x, double y);
 };

#endif 

SpaceObject source file:

#include<cmath>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include"vector.h"
#include "SpaceObject.h"

//constructor
SpaceObject::SpaceObject() {
maxLocations.x = 500;
maxLocations.y = 500;
radius = 5;

location.x = 0;
location.y = 0;

velocity.x = 0;
velocity.y = 0;

angleDeg = 0;

}
//================================================================
//mutators

//================================================================
// Function:   setLocation
//
// Description: Sets the x and y values of the SpaceObject's location
// Argument list:
// x(I) - x coordinate for SpaceObject's location
// y(I) - y coordinate for SpaceObject's location 
//=================================================================*/
void SpaceObject::setLocation(double x, double y){

//Check and correct for the SpaceObject going out of bounds.
if (x < 0)
    location.x += maxLocations.x;
else if (x > maxLocations.x)
    location.x -= maxLocations.x;
else 
    location.x = x;

if (y < 0)
    location.y += maxLocations.y;
else if (y > maxLocations.y)
    location.y -= maxLocations.y;
else
    location.y = y;
}

//================================================================
// Function:   setVelocity
//
// Description: Sets the velocity for the x and y direction of the SpaceObject
// Argument list:
// velocityX(I) - sets the velocity in the x direction
// velocityY(I) - sets the velocity in the y direction
//=================================================================
void SpaceObject::setVelocity(double velocityX, double velocityY){
velocity.x = velocityX;
velocity.y = velocityY;
}
//================================================================
// Function:   setLocation
//
// Description: Sets the directional angle of the SpaceObject
// Argument List: 
// angDeg(I) - sets the SpaceObjects angle
//=================================================================*/
void SpaceObject::setAngle(double angDeg){
while (angDeg >= 360)
    angDeg -= 360;
while (angDeg < 0)
    angDeg += 360;
angleDeg = angDeg;   
}


//================================================================
//accessors
//================================================================
// Function: getRadius
// Description: Returns the Radius
//
// Return value: 
//  Radius - the SpaceObjects turning axis
//=================================================================*/
double SpaceObject::getRadius(){
return radius;
}
//================================================================
// Function: getLocation
// Description: Returns the x and y values of the SpaceObjects location
//
// Return value: 
//  location - the location of the SpaceObject
//=================================================================*/
Vector SpaceObject::getLocation(){
return location;
}
//================================================================
// Function: getVelocity
// Description: Returns the x and y values of the SpaceObjects directional velocity
//
// Return value: 
//  Velocity
//=================================================================*/
Vector SpaceObject::getVelocity(){
return velocity;
}
//================================================================
// Function:   getAngle
// Description: returns the angle 
// 
// Return value: 
//  Angle - the SpaceObjects direction
//=================================================================*/
double SpaceObject::getAngle(){
return angleDeg;
}

//============================================
//other functions
//================================================================
// Function:   setmaxLocations
//
// Description: Sets the x and y values of the SpaceObject's max possible area of movement 
// Argument list:
// x(I) - max width of SpaceObject's possible positions
// y(I) - max heigth of SpaceObject's possible positions
//=================================================================*/
void SpaceObject::setmaxLocations(double x, double y){
 maxLocations.x = x;
 maxLocations.y = y;
}
//================================================================
// Function:   updateLocations
//
// Description: Sets the x and y values of the SpaceObject's location while including the change with velocity
//=================================================================*/
void SpaceObject::updateLocation(){
location.x = location.x + velocity.x;
location.y = location.y + velocity.y;
 }  

ship header file:

#ifndef SHIP_H
#define SHIP_H

#include "SpaceObject.h"

class Ship: public SpaceObject {
 public:
Ship();
void rotateLeft(); 
void rotateRight();
void applyThrust();
void draw(sf::RenderWindow& win);
};

#endif

ship source file:

#include<cmath>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include"vector.h"
#include "ship.h"


//Constants
const double PI = 3.14159;
const double THRUST = 0.005;
const double TURN_SPEED = 1;

Ship::Ship() {

}
//============================================
//other functions
//================================================================
// Function:  rotateLeft
//
// Description: turns the ship left by subtracting from the ship's angle
//=================================================================*/
void Ship::rotateLeft(){
int newAngle;
newAngle = getAngle() - TURN_SPEED;
setAngle(newAngle);
}
//================================================================
// Function:  rotateRight
//
// Description: turns the ship Right by adding to the ship's angle
//=================================================================*/
void Ship::rotateRight(){
int newAngle;
newAngle = getAngle() + TURN_SPEED;
setAngle(newAngle);
}
//================================================================
// Function:   applyThrust
//
// Description: Sets the x and y value of the ship's movement
//=================================================================*/
void Ship::applyThrust(){

double forcex = cos((getAngle()-90)*PI/180) * .005;
double forcey = sin((getAngle()-90)*PI/180) * .005;
setVelocity(getVelocity().x + forcex, getVelocity().y + forcey);

}

//--------------------------------------------------------------------------
// Function: draw
// Description: draws the ship on the given window
// Parameters:
//  win - the window for drawing the ship
//--------------------------------------------------------------------------
void Ship::draw(sf::RenderWindow& win) {

// draw ship
sf::ConvexShape ship;
ship.setPointCount(3);
ship.setPoint(0, sf::Vector2f(10, 0));
ship.setPoint(1, sf::Vector2f(0, 25));
ship.setPoint(2, sf::Vector2f(20, 25));

sf::Vector2f midpoint(10,15);
ship.setOrigin(midpoint);

ship.setFillColor(sf::Color(0, 0, 0));
ship.setOutlineThickness(1);
ship.setOutlineColor(sf::Color(255, 255, 255));

ship.setPosition(getLocation().x, getLocation().y);
ship.setRotation(getAngle());
win.draw(ship);
}

vector header file (just for clarity):

#ifndef VECTOR_H
#define VECTOR_H
     struct Vector{
      float x;
      float y;
     };
#endif
هل كانت مفيدة؟

المحلول

It seems that SpaceObject::setLocation() does the actual checking to see if the ship is in bounds, yet this function is only called once at the beginning of your main(). It needs to be called within the loop so it can continually check that the ship has a valid location. I would recommend calling it within SpaceObject::updateLocation()

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top