How to Change Direction of Moving Shape

Post Reply
Fudz4
Posts: 6
Joined: Tue Apr 05, 2016 2:27 pm

How to Change Direction of Moving Shape

Post by Fudz4 »

Hello, Im working on my program which is to show collision detection with Bullet, and everything works but my objects (spheres) simply move in one direction and I would like to get them to change direction every now and then, I present my code, specifically the method which adds a sphere into my world:

Code: Select all

btRigidBody* addSphere(float rad, float x, float y, float z, float mass)
{
	btTransform t;	// Variable to generate the spheres position and rotation
	t.setIdentity();
	t.setOrigin(btVector3(x, y, z)); // Initialize Bullet function which allows for velocity and movement.
	btSphereShape* sphere = new btSphereShape(rad);	// Correct Bullet shape to use along with OpenGL quad.
	btVector3 inertia(0, 0, 0); // Initialize inertia function to be calculated. 
	if (mass != 0.0)
		sphere->calculateLocalInertia(mass, inertia); // Calculate inertia of the sphere.
	
	btMotionState* motion = new btDefaultMotionState(t);	//set the position (and motion)
	btRigidBody::btRigidBodyConstructionInfo info(mass, motion, sphere, inertia);	//create the constructioninfo, you can create multiple bodies with the same info
	btRigidBody* body = new btRigidBody(info);	//let's create the body itself
	world->addRigidBody(body);	//and let the world know about
	speed = rand() % 201 + -100;
	if (direction == 1) {
		body->setLinearVelocity(btVector3(speed, 0, 0));
	}
	else if (direction == 2) {
		body->setLinearVelocity(btVector3(0, speed, 0));
	}
	else if (direction == 3) {
		body->setLinearVelocity(btVector3(0, 0, speed));
	}
	body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
	body->getMotionState()->getWorldTransform(t);
	bodies.push_back(new bulletObject(body,1,1.0,0.0,0.0));	//to be easier to clean, I store them a vector
	body->setUserPointer(bodies[bodies.size() - 1]);
	return body;
}
notice my (probably unorthodox) method of applying 'speed' to either the x, y or z co-ordinate of the btVector3 function depending on a direction variable that is assigned to an object, at another part of the program, I show it below:

Code: Select all

	int sphereCount = 85;
	for (int i = 1; i <= sphereCount; i++) {
		direction = rand() % 2 + 1;
		int randomX = rand() % 200 + 1;
		xLocation = randomX;
		int randomY = rand() % 100 + 1;
		yLocation = randomY;
		int randomZ = rand() % 200 + 1;
		zLocation = randomZ;
		int randomRadius = rand() % 2 + 1;
		addSphere(randomRadius, xLocation, yLocation, 0.0, 1.0);
So my spheres just drift into blackness, can anyone help me get my spheres to sproradically change direction after lets say 5 seconds?

Thank you
S1L3nCe
Posts: 50
Joined: Thu Mar 24, 2016 10:22 am
Location: France

Re: How to Change Direction of Moving Shape

Post by S1L3nCe »

Hi,

By storing bodies you can use several methods after stepping the World.

applyImpulse
applyCentralImpulse
applyForce
applyCentralForce
setGravity
setLinearVelocity

Else, you can create 4 wall with restitution for keeping the spheres into the area.
Fudz4
Posts: 6
Joined: Tue Apr 05, 2016 2:27 pm

Re: How to Change Direction of Moving Shape

Post by Fudz4 »

S1L3nCe wrote:Hi,

By storing bodies you can use several methods after stepping the World.

applyImpulse
applyCentralImpulse
applyForce
applyCentralForce
setGravity
setLinearVelocity

Else, you can create 4 wall with restitution for keeping the spheres into the area.
Hello,

Thanks very much for your suggestions.

I dont think I want to contruct walls, its not so much about bouncing spheres on platforms but to make the spheres reverse/change their directions on their own, so as to ensure theres always chances for collision. I know walls could technically do that but those functions sound like they could get the job done.

Ok so you say that this should be initiated after I step the world? So do you mean that I have to create a new method that my main method will call upon? I present code:

This is halfway down my main method. Do I call a new function at where I comment?

Code: Select all

world->stepSimulation(1 / 60.0);
		display();
                // new function here?
		SDL_GL_SwapBuffers();
		if (1000.0 / 60>SDL_GetTicks() - start)
			SDL_Delay(1000.0 / 60 - (SDL_GetTicks() - start));
	}
If so, can you give any tips on how to write this method for something like apply impulse or central impulse? Im guessing within the method there would be something along the lines of:

Code: Select all

for (int i = 0; i<bodies.size(); i++)
	{
		applyImpulse(bodies[i]);
	}
}
Or is there more to it?
S1L3nCe
Posts: 50
Joined: Thu Mar 24, 2016 10:22 am
Location: France

Re: How to Change Direction of Moving Shape

Post by S1L3nCe »

Code: Select all

for (int i = 0; i<bodies.size();++i)
{
   bodies[i]->applyCentralImpulse(impulseToApply);
}
It's look like the good way, juste after the stepSimulation.

You can find method's prototype here :
http://bulletphysics.org/Bullet/BulletF ... dBody.html
Fudz4
Posts: 6
Joined: Tue Apr 05, 2016 2:27 pm

Re: How to Change Direction of Moving Shape

Post by Fudz4 »

S1L3nCe wrote:

Code: Select all

for (int i = 0; i<bodies.size();++i)
{
   bodies[i]->applyCentralImpulse(impulseToApply);
}
It's look like the good way, just after the stepSimulation.

You can find method's prototype here :
http://bulletphysics.org/Bullet/BulletF ... dBody.html
Hey dude!

Thanks, I tried sphere->applyCentralImpulse(btVector3(0, 0, 0)); < Insert numbers

and its way better than before, unfortunately after a certain time they will eventually dirft into blackness again but its all ok because the objective of my report to study how Bullet handles collision detection and its sufficient as it is, Im going to try and find a way to have the user be able to restart the program should he wish.

Ill try and find better ways of doing this in the future, I suppose walls would be a start lol

Thanks again! :)
S1L3nCe
Posts: 50
Joined: Thu Mar 24, 2016 10:22 am
Location: France

Re: How to Change Direction of Moving Shape

Post by S1L3nCe »

Try to get the sphere's linear velocity and use the opposite for make them turn back ;)
Post Reply