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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <cmath>
using namespace std;
SDL_Renderer* renderer;
SDL_Window* window;
SDL_Texture* texture;
SDL_Surface* surface;
SDL_Event event;
class Vector2{
public:
double x;
double y;
Vector2(double x,double y): x(x),y(y){}
Vector2(){x = 0;y = 0;}
void add(Vector2& other){
x += other.x;
y += other.y;
}
void scale(double scalar){
x*=scalar;
y*=scalar;
}
double magnitude(){
return sqrt( (x * x) + (y * y) );
}
Vector2 normalize(){
double mag = magnitude();
return Vector2(x/mag,y/mag);
}
int dotProduct(Vector2& other){
cout << " x * other.x == " << x << " * " << other.x << " == " << x * other.x << endl;
cout << " y * other.y == " << y << " * " << other.y << " == " << y * other.y << endl;
cout << "(x * other.x) + (y * other.y) == " << (x * other.x) + (y * other.y) << endl;
return (x * other.x) + (y * other.y);
}
};
Vector2 subtractVectors(const Vector2& one,const Vector2& two){
Vector2 difference;
double x = -(two.x);
double y = -(two.y);
difference.x = one.x + x;
difference.y = one.y + y;
return difference;
}
class BlackRect{
public:
bool objectLeft = false;
bool objectRight = true;
Vector2 vec;
SDL_Rect spriteOneRect;
SDL_Surface* spriteOneSurface;
SDL_Texture* spriteOneTexture;
BlackRect(int x,int y,string fileName){
vec.x = x;
vec.y = y;
spriteOneSurface = IMG_Load(fileName.c_str());
spriteOneTexture = SDL_CreateTextureFromSurface(renderer,spriteOneSurface);
spriteOneRect.x = x;
spriteOneRect.y = y;
SDL_QueryTexture(spriteOneTexture,NULL,NULL,&spriteOneRect.w,&spriteOneRect.h);
}
BlackRect(){
vec.x = 250;
vec.y = 250;
}
void update(){
spriteOneRect.x = vec.x;
spriteOneRect.y = vec.y;
}
};
void render(BlackRect& br,BlackRect& rr){
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderCopy(renderer,br.spriteOneTexture,NULL,&br.spriteOneRect);
SDL_RenderCopy(renderer,rr.spriteOneTexture,NULL,&rr.spriteOneRect);
SDL_RenderPresent(renderer);
}
bool collisionBounds(SDL_Rect &rect){
if(rect.y <= 0)
return true;
if(rect.y + rect.h >= 600)
return true;
if(rect.x <= 0)
return true;
if(rect.x + rect.w >= 800)
return true;
return false;
}
void moveObject(BlackRect& rect,Vector2& velocity){
if(collisionBounds(rect.spriteOneRect)){
if(rect.objectRight){
rect.objectRight = false;
rect.objectLeft = true;
}else{
rect.objectLeft = false;
rect.objectRight = true;
}
}
if(rect.objectRight)
rect.vec.x += velocity.x;
if(rect.objectLeft)
rect.vec.x -= velocity.x;
rect.update();
}
int SDL_main(int argc,char* argv[]){
init();
bool quit = false;
BlackRect br(20,400,"blockone.png");
BlackRect rr(400,200,"blockoneRed.png");
Vector2 brVelocity(4,0);
while(true)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
cleanUp();
return 0;
}
if(event.type == SDL_KEYDOWN)
{
int key = event.key.keysym.sym;
if(key == SDLK_b)
moveObject(br,brVelocity);
}
}
Vector2 direc = br.vec.normalize();
Vector2 direc2 = rr.vec.normalize();
direc.scale(10);
direc2.scale(10);
int directions = direc.dotProduct(direc2);
cout << "directions == " << directions << endl;
if(directions > 0)
cout << "same direction" << endl;
else if(directions < 0)
cout << "opposite directions" << endl;
else
cout << "perpendicular " << endl;
render(br,rr);
SDL_Delay(20);
}
|