Rockets

Topics: Developer Forum
May 15, 2012 at 6:19 AM
Edited May 15, 2012 at 6:27 AM

Working on a game at the moment in which a rocket attached to a car needs to be able to push the car in the direction the car is facing

I got it figured out (i think) i tested this in my test environment which doesnt have a background yet, so i was only able to test by watching the ground, which goes offscreen a lot, it seems to work (i think it might have some issues.

Im posting here because I think that there should be a way to apply force to an object in the direction its facing, in the engine, anyway, heres my code

 

float halfForce = 2500

float rotation = MathHelper.ToDegrees(players[0].bodies[2].Rotation);
int temp = (int)Math.Floor((decimal)(MathHelper.ToDegrees(players[0].bodies[2].Rotation) / 360));
rotation -= temp * 360;
rotation /= 90;

float ratioA = 0;
float ratioB = 0;
float forceX = 0;
float forceY = 0;

if (rotation < 1)
{
	//0-0.9
	float tempr = rotation;
	ratioA = tempr;
	ratioB = tempr;

	forceX = ratioA * halfForce;
	forceY = ratioB * halfForce;
}
else if (rotation < 2)
{
	//1-1.9
	float tempr = rotation - 1;
	ratioA = tempr;
	ratioB = 1 - tempr;

	forceX = -(ratioA * halfForce);
	forceY = ratioB * halfForce;
}
else if (rotation < 3)
{
	//2-2.9
	float tempr = rotation - 2;
	ratioA = tempr;
	ratioB = 1 - tempr;

	forceX = -(ratioB * halfForce);
	forceY = -(ratioA * halfForce);
}
else
{
	//3-3.9
	float tempr = rotation - 3;
	ratioA = tempr;
	ratioB = 1 - tempr;

	forceX = ratioA * halfForce;
	forceY = -(ratioB * halfForce);
}

 

this gets the rotation, turns it into degrees (because idk how to work in radians) removes all redundant rotations (changes it to 0-360, normally its not limited) then splits the 360 degrees into 4 quadrants (i think this is kind of what radians do... idfk)

it then gets the ratio of x to y from the quadrant that the car is facing, and applies it to half the force were using

that gives you the x force and y force, there are times that it seems weird, i havent figured it all out yet, if you decide to put something like this in the engine i would be thankful

if there already IS something like this in the engine, then where the hell is it and how do i use it :)

 

*edit* fixed issue in case 3, ratios were backwards

May 16, 2012 at 9:14 AM
Edited May 16, 2012 at 9:16 AM

There is actually a much easier way and it is also an essential technique for many other things as well. It has to do with converting coordinate systems. You can convert from one coordinate system to another quite easily given some conditions. This is called "frame of reference" or "reference frame".

Therefore your body has its own coordinate system with (0,0) at it's center of mass and from there using GetWorldVector/GetWorldPoint/GetLocalVector/GetLocalPoint you can do the conversion from local to world.

For example if your body is oriented and you know where the tip is let's call that vector localDirection in local coordinates (e.g. (1,0). Then the body will be facing Body.GetWorldVector(localDirection) globally. To apply force do worldDirection.Normalize(); worldDirection*=magnitude; Body.ApplyForce(worldDirection).

Vector math - pretty powerful and you don't have to deal with the body transform yourself :)

May 22, 2012 at 8:10 AM

the main point of this post was to ask why the hell there isnt a function that applies force in a direction based on the rotation of the object in the first place, it seems like something that would be commonly used, so why not just add it into the engine?

 

*********************************

thought of this while i was writing this post, do you think this would be a good alternative? it should save cpu cycles! (good for xbox games)

I suppose i could work up a table of predetermined "directional offsets" for each degree, which would mean i only have to calculate 360 times rather than once each frame, it would be a bit inaccurate (i could even go down to 1/10 of a degree to get it more accurate), but it would (in theory) be a LOT faster than calculating it every frame

*********************************

 

Im not really sure how I would get where the "tip" of the caris without using the same math that I already used

At some point you are going to have to take the angle into consideration to get it, lets say the tip is 1 (for simplicity sake) unit to the right,

if the ship is rotated 1/8 turn counter clockwise (pointing up right), how would i get that without extrapolating degrees (as i already have)

how would i then translate the original tips location without using at LEAST trigonometry (which would require a square root, much more complex than the simple addition subraction division and multiplication i've used so far, at least to the best of my knowledge)

 

lets see, 

 

center: 0,0

Tip: 1,0

rotation 315* (up right at 45*)

 

my mind works a little differently than most people, i tend to figure out complex problems quite easily and as a result i tend to over-complicate things, because it feels natural to me, if there IS a simpler (or faster) way, then i would be interested in figuring it out but I dont like to re-figure something out that works, so its not something i want to spend a lot of time on :)