bounce fixes, some force stuff, entitylist class

master
BuildTools 5 years ago
parent 269a9b4f5d
commit 39126672c0
  1. 23
      Program.cs
  2. 14
      ball.cs
  3. 34
      collisionHandler.cs
  4. 19
      entityList.cs
  5. 3
      globals.cs

@ -17,29 +17,16 @@ namespace game
callbacks.registerCallbacks(window);
CircleShape cursor = new CircleShape(25f);
cursor.FillColor = Color.Green;
g.entityList.Add(new ball_t(50f, new Vector2f(1280 / 2, 720 / 2), new Vector2f(.2f, .2f)));
g.entityList.Add(new ball_t(25f, new Vector2f(100, 100), new Vector2f(.2f, .2f)));
g.entityList.Add(new ball_t(25f, new Vector2f(300, 100), new Vector2f(.2f, .2f)));
g.entityList.Add(new ball_t(25f, new Vector2f(200, 100), new Vector2f(.2f, .2f)));
ball_t ball = new ball_t(50f);
ball_t ball2 = new ball_t(25f, new Vector2f(100, 100), new Vector2f(.5f, .5f));
// main loop
while (window.IsOpen)
{
window.DispatchEvents();
cursor.Position = new Vector2f(g.mouse.position.X - (cursor.GetLocalBounds().Width / 2), g.mouse.position.Y - (cursor.GetLocalBounds().Height / 2));
if (g.mouse.button == Mouse.Button.Left && g.mouse.buttonState)
cursor.FillColor = Color.Red;
else
cursor.FillColor = Color.Green;
window.Clear();
// add stuff to drawing surface
window.Draw(cursor);
ball.Run(window);
ball2.Run(window);
g.entityList.Run(window);
window.Display();
}
}

@ -37,13 +37,15 @@ namespace game
{
//temp place for collision stuff
Vector2f center = new Vector2f(this.shape.Position.X + (this.shape.GetLocalBounds().Width / 2), this.shape.Position.Y + (this.shape.GetLocalBounds().Height / 2));
bool isValidPosition = ((center.X > (1280 - (this.shape.GetLocalBounds().Width / 2)) || center.Y > (720 - (this.shape.GetLocalBounds().Height / 2))) || (center.X < (0 + (this.shape.GetLocalBounds().Width / 2)) || center.Y < (0 + (this.shape.GetLocalBounds().Height / 2))));
if (isValidPosition || collisionHandler.isColliding(this))
bool isValidPositionX = ((center.X > (1280 - (this.shape.GetLocalBounds().Width / 2)) || (center.X < (0 + (this.shape.GetLocalBounds().Width / 2)))));
bool isValidPositionY = ((center.Y > (720 - (this.shape.GetLocalBounds().Height / 2))) || center.Y < (0 + (this.shape.GetLocalBounds().Height / 2)));
if ((isValidPositionX || isValidPositionY) || collisionHandler.isColliding(this).colliding)
{
//this.shape.Position = new Vector2f(1280 / 2, 720 / 2);
this.velocity *= -1;
this.velocity.X *= isValidPositionX ? -1f : 1f;
this.velocity.Y *= isValidPositionY ? -1f : 1f;
this.velocity /= 2f;
this.shape.Position += new Vector2f(0, -1F);
this.shape.Position += new Vector2f(0, -.1F);
this.shape.Position = new Vector2f(Math.Clamp(this.shape.Position.X, 0, 1280), Math.Clamp(this.shape.Position.Y, 0, 720));
Console.WriteLine("COLLISION: "+this.velocity.Y.ToString());
}
@ -51,11 +53,13 @@ namespace game
this.shape.Position += this.velocity;
ball_t dummy = this;
dummy.shape.Position = this.shape.Position;
if (collisionHandler.isColliding(dummy))
collision_t simCollision = collisionHandler.isColliding(dummy);
if (simCollision.colliding)
{
this.shape.Position = oldPosition;
this.velocity *= -1;
this.velocity /= 2f;
//this.velocity *= simCollision.force;
}
if ((Math.Abs(this.velocity.X) < 1 ) && (Math.Abs(this.velocity.Y) < 1))
this.velocity += forces;

@ -2,21 +2,49 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using SFML.System;
namespace game
{
class collision_t
{
public bool colliding;
public float force;
public collision_t(bool colliding_, float force_)
{
this.colliding = colliding_;
this.force = force_;
}
}
static class collisionHandler
{
public static float TEMP_vecLen(Vector2f v)
{
return (float)Math.Sqrt(v.X * v.X + v.Y * v.Y);
}
public static List<ball_t> registry = new List<ball_t>();
public static bool isColliding(ball_t ball)
public static collision_t isColliding(ball_t ball)
{
foreach (ball_t registeredBall in registry)
{
if (registeredBall != ball && registeredBall.shape.GetGlobalBounds().Intersects(ball.shape.GetGlobalBounds()))
return true;
{
/*
// conservation of momentum: m*v = M, net momentum is conserved through collision
// (r.m*r.vx)/b.m == b.vx
// (r.m*r.vy)/b.m == b.vy
// where r is object we're colliding with and b is us, m is maass and v is velocity
Vector2f calcVelocity = new Vector2f();
calcVelocity.X = (registeredBall.shape.Radius * registeredBall.velocity.X) / ball.shape.Radius;
calcVelocity.Y = (registeredBall.shape.Radius * registeredBall.velocity.Y) / ball.shape.Radius;
return new collision_t(true, calcVelocity);
*/
float forceCalc = 10 / ((ball.shape.Radius * TEMP_vecLen(ball.velocity) > registeredBall.shape.Radius * TEMP_vecLen(registeredBall.velocity)) ? (ball.shape.Radius * TEMP_vecLen(ball.velocity)) : registeredBall.shape.Radius * TEMP_vecLen(registeredBall.velocity));
return new collision_t(true, forceCalc);
}
}
return false;
return new collision_t(false, 1f);
}
}
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using SFML.Graphics;
using game;
namespace game
{
class entityList : List<ball_t>
{
public void Run(RenderWindow window)
{
foreach (ball_t ball in this)
{
ball.Run(window);
}
}
}
}

@ -5,7 +5,7 @@ using SFML.Window;
using SFML.System;
using System.Net;
using System.Threading;
using System.Collections.Generic;
namespace game
{
@ -13,5 +13,6 @@ namespace game
{
public static mouse_t mouse = new mouse_t();
public static Random rand = new Random();
public static entityList entityList = new entityList();
}
}

Loading…
Cancel
Save