example

This is a discussion on example within the C++ Programming forums, part of the General Programming Boards category; I tried to make a program using your formula but I couldn't get it to work propery. Could you show ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    79

    example

    I tried to make a program using your formula but I couldn't get it to work propery.
    Could you show a small part of a simple program that uses it to, for instance, make a circle bounce around on the screen? Don't waste any time on the graphics, it's the maths that I'm interested in (remeber I haven't read trigonometry yet). If you could do that it would be really great.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    79
    Oopps! Sorry, my mistake. This was supposed to be a reply to a different topic. I must have clicked on new topic instead of post reply.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Hope this helps. It compiles with bloodshed dev c++. Note, modified the randPosition function so it varies with the constants, rather than just returns a position in the rectange between (0,0) and (1,1)

    Code:
    // code for pongesque 2D motion
    
    #include <cmath> 
    #include <cstdlib>
    //#include "your2dgraphicslib.h"
    
    struct vec {
    	vec(float _x, float _y) : x(_x), y(_y) { };
    	vec() 			: x(0),  y(0)  { };
    	void operator = (const vec& rhs) { x = rhs.x; y = rhs.y; };
    	float x;
    	float y;
    };
    
    vec operator + (const vec& left, const vec& right) {
    	return vec(left.x + right.x, left.y + right.y);
    }
    
    vec operator - (const vec& left, const vec& right) {
    	return vec(left.x - right.x, left.y - right.y);
    }
    
    vec operator * (const vec& left, float scale) {
    	return vec(left.x*scale, left.y*scale);
    }
    
    inline vec operator* (float scale, const vec& right) {
    	return right * scale;
    }
    
    enum wall { LEFT_WALL, RIGHT_WALL, TOP_WALL, BOTTOM_WALL};
    
    // moves between rectangle with bottom left coords (0,0) and top right coords (1,1)
    const float RIGHT_BORDER = 1.0f;
    const float TOP_BORDER = 1.0f;
    const float LEFT_BORDER = 0.0f;
    const float BOTTOM_BORDER = 0.0f;
    
    class point {
    public:
    	point(const vec& pos, const vec& vel);
        point() { };
    	void move(float timeChange);	
    	vec getPosition() { return position; };
    	void draw();
    private:
    	vec position;
    	vec velocity;
    	void collide(wall Wall);
    };
    
    
    point::point(const vec& pos, const vec& vel) {
    	velocity = vel;
    	position = pos;
    	move(0.0f); // make sure point starts off in the valid area
    }
    
    void point::collide(wall Wall) {
    	switch (Wall) {
    		case LEFT_WALL:	 
    			position.x = LEFT_BORDER; // make sure it doesn't move through the wall
    			velocity.x = -velocity.x;  // reverse movement in x direction
    			break;
    		case RIGHT_WALL: 
    			position.x = RIGHT_BORDER;
    			velocity.x = -velocity.x;
    			break;
    		case TOP_WALL:   
    			position.y = TOP_BORDER;
    			velocity.y = -velocity.y;
    			break;
    		case BOTTOM_WALL:
    			position.y = BOTTOM_BORDER;
    			velocity.y = -velocity.y;
    			break;
    	}
    }
    
    void point::move(float timeChange) {
    	position = position + timeChange * velocity;
    	
    	// check for collosions
    	if (position.x > RIGHT_BORDER) {
    		collide(RIGHT_WALL);
    	} else if (position.x < LEFT_BORDER) {
    		collide(LEFT_WALL);
    	} 
    	if (position.y > TOP_BORDER) {
    		collide(TOP_WALL);
    	} else if (position.y < BOTTOM_BORDER) {
    		collide(BOTTOM_WALL);
    	}
    }
    
    // cheesy method because I am not gonna use it to actually draw to screen
    #include <iostream>
    
    void point::draw() {
    	/* your drawing function comes in here, keep in mind x and y
    	 coordinations of point are in position.x and position.y */
        std::cout << position.x << "\t" << position.y << endl;
    
    }
    
    // returns vector with magnitude of one and random direction
    vec randDirection() {
    	double ang = (double) (rand() % 360) * 3.14159265 / 180.0;
    	return vec(cos(ang) , sin(ang));
    }
    
    vec randPosition() {
    	return vec(
    (float) rand()/RAND_MAX *(RIGHT_BORDER - LEFT_BORDER) + LEFT_BORDER, 
    (float) rand()/RAND_MAX * (TOP_BORDER - BOTTOM_BORDER) + BOTTOM_BORDER);
    }
    
    const int NUM_POINTS = 50;
    const float SPEED_SCALE = .0002f;
    
    int main() {
    	srand(0);
    	point pointList[NUM_POINTS];
    	for (int i = 0; i < NUM_POINTS; i++) {
    		// fill point list with random points
    		pointList[i] = point(randPosition() , randDirection() * SPEED_SCALE);
    	}
    
    	while (true) {
    		for (int i = 0; i < NUM_POINTS; i++) {
    			pointList[i].move(1);
    			pointList[i].draw();
    		}
    	}
    
    	return 0;
    }
    Last edited by SilentStrike; 10-20-2001 at 10:31 AM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Now THAT'S an example!
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    12
    if only the whole world was a generous as silent strike...
    THE Dark_Knight_506

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    79

    Talking

    Yeah, that's an example
    I'll take some time to look at it.
    Thanks.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21