Skip to content
Snippets Groups Projects
Commit 84ab3b98 authored by Aron Nikku's avatar Aron Nikku
Browse files

Added all existing files

parents
Branches
No related tags found
No related merge requests found
a.out 0 → 100755
File added
c_change 0 → 100755
File added
#include <math.h>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
int SIZE = 500;
double PI = 3.14159265359;
class box {
public:
box(float x, float y, float s, float pX, float pY) {
movement = sf::Vector2f(x, y);
form = sf::RectangleShape(sf::Vector2f(s, s));
form.setFillColor(sf::Color::Blue);
form.setPosition(pX, pY);
size = s;
}
void move() {
form.move(movement * 0.1f);
checkCollision();
}
void draw(sf::RenderWindow& window) {
window.draw(form);
}
void boxCollision(box other) {
float x1 = form.getPosition().x;
float y1 = form.getPosition().y;
float s1 = size;
float x2 = other.form.getPosition().x;
float y2 = other.form.getPosition().y;
float s2 = other.size;
bool colX = false;
if (x1 < x2 && (x1 + s1) > x2) {
colX = true;
} else if (x1 > x2 && x1 < (x2 + s2)) {
colX = true;
}
bool colY = false;
if (y1 < y2 && (y1 + s1) > y2) {
colY = true;
} else if (y1 > y2 && y1 < (y2 + s2)) {
colY = true;
}
if (colX && colY) {
movement.x *= -1;
movement.y *= -1;
/*
double x = x1 - x2 + (s1 + s2)/2;
double y = y1 - y2 + (s1 + s2)/2;
//double angle = atan2(x, y);
//double r = pow(sin(angle), 2);
double angle = atan(y/x);
angle = PI - angle;
double speed = sqrt(pow(movement.x, 2) + pow(movement.y, 2));
movement.x = sin(angle) * speed;
movement.y = cos(angle) * speed;
*/
}
}
//private:
sf::Vector2f movement;
sf::RectangleShape form;
float size;
void checkCollision() {
// Wall
if (form.getPosition().x + size > SIZE || form.getPosition().x < 0) {
movement.x *= -1;
}
if (form.getPosition().y + size > SIZE || form.getPosition().y < 0) {
movement.y *= -1;
}
}
};
int main() {
sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Physics");
window.setFramerateLimit(60);
box box1(5, 5, 100.f, 50, 50);
box box2(3, 2, 100.f, 200, 400);
/*
std::vector<sf::RectangleShape> shapeList;
for (int i = 0; i < SIZE / 100; i++) {
for (int j = 0; j < SIZE / 100; j++) {
i++;
}
}
*/
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
box1.move();
box2.move();
window.clear();
box1.draw(window);
box2.draw(window);
box1.boxCollision(box2);
box2.boxCollision(box1);
window.display();
}
return 0;
}
\ No newline at end of file
#include <math.h>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
int SIZE = 500;
double PI = 3.14159265359;
class sphere {
public:
// VARIABLES
double radius, posX, posY;
double mass;
double angle;
double speed;
sphere(double x, double y, double r) {
posX = x;
posY = y;
radius = r;
mass = 10;
//angle = 0.3;
//speed = 1;
}
void draw(sf::RenderWindow& window) {
//float r = float(radius);
sf::CircleShape s(radius);
s.setPosition(sf::Vector2f(posX - radius, posY - radius));
s.setFillColor(sf::Color::Red);
window.draw(s);
}
void move() {
posX += speed * cos(angle);
posY += speed * sin(angle);
checkWalls();
}
void checkWalls() {
if (posX + radius > SIZE || posX - radius < 0) {
angle = PI - angle;
}
if (posY + radius > SIZE || posY - radius < 0) {
angle *= -1;
}
}
void sphereCollision(sphere& other) {
double dist = sqrt(pow(posX - other.posX, 2) + pow(posY - other.posY, 2));
//std::cout << dist << " " <<radius + other.radius << std::endl;
if (dist < radius + other.radius) {
angle += PI;
}
}
};
int main() {
sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Physics");
window.setFramerateLimit(60);
sphere s1(50, 50, 30);
sphere s2(350, 200, 30);
s1.angle = 0.2;
s2.angle = 0;
s1.speed = 2;
s2.speed = 0;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
// MOVE
s1.move();
s2.move();
// Collide
s1.sphereCollision(s2);
s2.sphereCollision(s1);
window.clear();
// DRAW
s1.draw(window);
s2.draw(window);
window.display();
}
return 0;
}
\ No newline at end of file
#include <math.h>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
int SIZE = 500;
double PI = 3.14159265359;
double pyt(double x, double y) {
return sqrt(pow(x, 2) + pow(y, 2));
}
class Vector2 {
public:
double x;
double y;
Vector2() {
x = 0;
y = 0;
}
Vector2 add(Vector2 other) {
Vector2 newVector;
newVector.x = x + other.x;
newVector.y = y + other.y;
return newVector;
}
Vector2 sub(Vector2 other) {
Vector2 newVector;
newVector.x = x - other.x;
newVector.y = y - other.y;
return newVector;
}
Vector2 mult(double m) {
Vector2 newVector;
newVector.x = x * m;
newVector.y = y * m;
return newVector;
}
Vector2 div(double d) {
Vector2 newVector;
newVector.x = x / d;
newVector.y = y / d;
return newVector;
}
double dot(Vector2 other) {
double result = 0;
result += x * other.x;
result += y * other.y;
return result;
}
double abs() {
return pyt(x, y);
}
};
class sphere {
public:
// VARIABLES
double radius;
Vector2 speed, pos;
double mass;
sphere(double x, double y, double r) {
pos.x = x;
pos.y = y;
radius = r;
mass = 10;
speed.x = 0;
speed.y = 0;
}
void draw(sf::RenderWindow& window) {
sf::CircleShape s(radius);
s.setPosition(sf::Vector2f(pos.x - radius, pos.y - radius));
s.setFillColor(sf::Color::Red);
window.draw(s);
}
void move() {
pos.x += speed.x;
pos.y += speed.y;
checkWalls();
}
void checkWalls() {
if (pos.x + radius > SIZE || pos.x - radius < 0) {
speed.x *= -1;
}
if (pos.y + radius > SIZE || pos.y - radius < 0) {
speed.y *= -1;
}
}
void setSpeed(Vector2 newSpeed) {
speed.x = newSpeed.x;
speed.y = newSpeed.y;
}
Vector2 sphereCollision(sphere& other) {
double dist = pyt(pos.x - other.pos.x, pos.y - other.pos.y);
Vector2 newSpeed;
if (dist < radius + other.radius) {
// ALT 1
/*
double v1 = pyt(speed.x, speed.y);
double v2 = pyt(other.speed.x, other.speed.y);
double t1;
double t2;
double phi;
double partOne = (( v1*cos(t1-phi)*(mass-other.mass) ) + ( 2*other.mass*v2*cos(t2-phi) )) / (mass + other.mass);
double partTwoX = cos(phi) + v1*sin(t1-phi)*cos(phi+PI/2);
double partTwoY = sin(phi) + v1*sin(t1-phi)*sin(phi+PI/2);
newSpeed.x = partOne * partTwoX;
newSpeed.y = partOne * partTwoY;
*/
// ALT 2
double part1 = 2 * other.mass / (mass + other.mass);
double part2 = ( (speed.sub(other.speed)).dot(pos.sub(other.pos)) ) / pow((pos.sub(other.pos)).abs(), 2);
Vector2 r1 = pos.sub(other.pos).mult(part1 * part2);
newSpeed = speed.sub(r1);
} else {
newSpeed.x = speed.x;
newSpeed.y = speed.y;
}
return newSpeed;
}
};
int main() {
sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Physics");
window.setFramerateLimit(60);
sphere s1(50, 50, 30);
sphere s2(350, 200, 30);
s1.speed.x = 1.5;
s1.speed.y = 0.5;
s2.speed.x = 0;
s2.speed.y = 0;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
// MOVE
s1.move();
s2.move();
// Collide
Vector2 speed1 = s1.sphereCollision(s2);
Vector2 speed2 = s2.sphereCollision(s1);
s1.setSpeed(speed1);
s2.setSpeed(speed2);
window.clear();
// DRAW
s1.draw(window);
s2.draw(window);
window.display();
}
return 0;
}
\ No newline at end of file
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "WINDOW");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
#include <SFML/Graphics.hpp>
int main()
{
sf::REnderWindow window(sf::VideoMode(200, 200), "WINDOW");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
File added
green 0 → 100755
File added
class Grid
{
private:
double something;
public:
int size_x;
int size_y;
};
\ No newline at end of file
in_test 0 → 100755
File added
phys 0 → 100755
File added
#include <SFML/Graphics.hpp>
int main()
{
int SIZE = 500;
sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Walker");
window.setFramerateLimit(60);
sf::RectangleShape shape(sf::Vector2f(50.f, 50.f));
shape.setFillColor(sf::Color::Blue);
sf::Vector2f movement(10.f, 5.f);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
// Move
shape.move(movement * 0.2f);
// Collision
if (shape.getPosition().x + 50 > SIZE || shape.getPosition().x < 0) {
movement.x *= -1;
}
if (shape.getPosition().y + 50 > SIZE || shape.getPosition().y < 0) {
movement.y *= -1;
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
\ No newline at end of file
physics.o 0 → 100644
File added
rand 0 → 100755
File added
#include <iostream>
int main()
{
std::cout << "Working" << std::endl;
return 0;
}
\ No newline at end of file
g++ -c main.cpp
g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
./sfml-app
g++ [NAME] -lsfml-graphics -lsfml-window -lsfml-system
g++ -Wall -Wextra -Weffc++ -Wold-style-cast -Woverloaded-virtual -std=c++17 -pedantic -Werror -g hello.cc
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "Window");
window.setFramerateLimit(10);
const int SIZE = 25;
sf::CircleShape spheres[SIZE];
sf::Color colors[SIZE];
int c_offset = 0;
for (int i = 0; i < SIZE; i++)
{
float s_size = (i + 1) * 10.f;
spheres[i] = sf::CircleShape(s_size);
float pos = 250 - s_size;
spheres[i].setPosition(sf::Vector2f(pos, pos));
//spheres[i].setFillColor(sf::Color(i*20, i*20, i*20));
colors[i] = sf::Color(i * 10, i * 10, i * 10);
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
// Change colors
for (int i = 0; i < SIZE; i++)
{
int index = i + c_offset;
if (index >= SIZE)
index -= SIZE;
/**
int index = i - c_offset;
if (index < 0)
index += SIZE;
**/
spheres[i].setFillColor(colors[index]);
}
c_offset++;
if (c_offset > SIZE)
{
c_offset -= SIZE;
}
window.clear(spheres[SIZE - 1].getFillColor());
for (int i = SIZE - 1; i > 0; i--)
{
window.draw(spheres[i]);
}
window.display();
}
return 0;
}
\ No newline at end of file
File added
spheres 0 → 100755
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment