Skip to main content
gamedev.net
🔒 Locked

Bouncing Ball

Started by ridefast42 Aug 23, 2006 at 4:16 PM 0 replies 800+ views
Original Post
ridefast42
ridefast42
Well, I want to make my object bounce. The object has a specified elasticity to determine how much it should bounce. My problem is, I don't know how to translate it into code. My ball will change directions but continue to accelerate upwards after changing directions. Also, feel free to tell me if I am taking the completely wrong approach to this as I am almost brand new to physics. Here is the code, the physics calculations are in Physics.cpp. Physics.cpp:

#include "Physics.h"
#include <math.h>

object::object(int objectx, int objecty, int objectweight, int objectelasticity)
{
	gravity = 1;
	weight = objectweight;
	force_x = 0;
	force_y = 0;
	force2_x = 0;
	force2_y = 0;
	velocity_x = 0;
	velocity_y = 0;
	x = objectx;
	y = objecty;
	force_magnitude = 0;
	force_direction = 0;
	elasticity = objectelasticity;
}

void object::calcphysics(int& x,int& y,bool& collision)
{

	if(collision == true)
	{
		force2_y = elasticity * -1;
	}
	force_y = (gravity * weight) + force2_y;
	velocity_x += force_x;
	velocity_y += force_y;
	x += velocity_x;
	y += velocity_y;
}

Physics.h:

class object
{
private:
	int weight;
	int velocity_x;
	int velocity_y;
	int elasticity;
public:
	int gravity;
	int x;
	int y;
	int force_x, force_y;
	int force2_x, force2_y;
	float force_direction;
	int force_magnitude;
	bool collision;
	object (int, int, int, int);
	void calcphysics(int& x, int& y,bool& collision);
};

JohnBolton
JohnBolton
There are some problems with your computation. Here are some of the relevant equations (assuming constant acceleration):
    force = mass * acceleration    velocity = previous_velocity + acceleration * time    position = previous_position + previous_velocity * time + 0.5 * acceleration * time<sup>2</sup> 
Bouncing can be done like this:
    if ( collision )    {        velocity_y = -velocity_y * elasticity;    } 
Also, a couple things to note:
  1. Gravity is easier to deal with if you consider it as an acceleration instead of a force.
  2. "weight" is a force. It is mass * gravity. The proper term is "mass".
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.