PDA

View Full Version : homing rocket physics



syspeat
March 27th, 2005, 08:33 AM
alright, say im making a missile weapon for a shooter game that rotates its position to follow a specific target. The missile finds an exact rotation position to the target, the rocket eases its rotation towards this value and thrusts in the direction of its physical rotation.

This is my code so far:

//wayP is the specified target
//rot is the targeted rotation

this.onEnterFrame=function(){
dx=this._x-_root.wayP._x
dy=this._y-_root.wayP._y
radians=Math.atan2(dy,dx)


rot=360*radians/(2*Math.PI)
this._rotation-=(this._rotation-rot)/8
this._x-=Math.cos(this._rotation/360*(2*Math.PI))*speed
this._y-=Math.sin(this._rotation/360*(2*Math.PI))*speed

}


my problem is that it often cant decide wether to go left or right to rotate to sync the rotation quickest and thus flutters endlessly in confusion. I need to figure a way to choose a direction for it while keeping the easing effect ive got. If anyones got a solution to this or a link to a good demonstration and sourse code it would be muchly appreciated because id love to make this a feature in my next game.

thanks in advance :D


btw; the swf demonstrates some extra code that sets speed according to distance, its target is the little rectangle.

syspeat
March 27th, 2005, 09:31 PM
btw that swf may not appear to do anything, i just set the intial speed of the rocket way to slow. Just wait for the speed to build up and itll dance around after the target ^__^

Sirisian
March 27th, 2005, 09:58 PM
hardest code ever but i have it!!!! your first problem is degree. Look at this code i made a year ago. Its made for an object rotating to the mouse but it can be changed easily


for (var x = 0; x<4; x++) {
//Rotation
adjacent = _root._xmouse-250;
opposite = 250-_root._ymouse;
hypotenuse = ((adjacent ^ 2)+(opposite ^ 2)) ^ 0.5;
if (adjacent>0 && opposite>0) {
this.rotate = 90-Math.atan2(opposite, adjacent)*(180/Math.PI);
}
if (adjacent>0 && opposite<0) {
this.rotate = 90-Math.atan2(opposite, adjacent)*(180/Math.PI);
}
if (adjacent<0 && opposite<0) {
this.rotate = 90-Math.atan2(opposite, adjacent)*(180/Math.PI);
}
if (adjacent<0 && opposite>0) {
this.rotate = 90+(360-Math.atan2(opposite, adjacent)*(180/Math.PI));
}
this.rotate = Math.round(this.rotate);
//----------------
if (theta(0)-180<0 && (this.rotate<theta(0) || this.rotate>theta(180))) {
this._rotation -= 1;
} else {
if (theta(0)+180>360 && (this.rotate<theta(-180) || this.rotate>theta(0))) {
this._rotation += 1;
} else {
if (this.rotate>theta(0)) {
this._rotation += 1;
} else {
if (this.rotate<theta(0)) {
this._rotation -= 1;
}
}
}
}
}

it uses a theta function to turn lets say -90 to 270

function theta(ang) {
var angle = 0;
if (this._rotation<0) {
angle = 360+this._rotation+ang;
} else {
angle = this._rotation+ang;
}
return angle;
}
If u have any questions ask

Sirisian
March 27th, 2005, 10:08 PM
oh and to understand the theory behind it think of the degree from your anchor point(the object your firing at) to your missile is 45 degrees(think of a line going from your target to your missile and what degree that is) and the degree of your missile using the function theta is 270. 270>45 so your program probably says move +1 rotation to the missile when moving -1 would get there faster. What my code does is figure out whether the angle difference between what your firing at and the angle of your missile is close enough to the breaking point to say moving -1 rotation is faster than moving +1 rotation or vice a versa if you were using 45 degrees for your missile and 270 for the angle between your anchor and your missile.

Nice and long explanation but the code isn't simple. Oh and any math wizes out there can someone simplify that code I wrote it out the long way.

syspeat
March 27th, 2005, 10:14 PM
thanks for your reply, that codes looks helpful. Um i noticed that you cut off the bottom of the code halfway through an if statement .__.

if you could re paste that would be great, i wanna try paste that straight in see what it does then learn from it. Thanks for your help ^__^

syspeat
March 27th, 2005, 11:55 PM
heres the fla of the one i was working on, i added some rough looking code to check if its within a certain rotation, if it fails to rotate itll just turn one direction till it lines up again. Kinda retarded but itd work good for certain enemies, like a slightly dumb homing missile or suicide laser gremilin-bot

Sirisian
March 28th, 2005, 11:02 AM
ok this is sirisian could u change the format to MX. Im at school.

Blackspirit
March 28th, 2005, 11:03 PM
suicide laser gremilin-bot

???

A game with one of those...whatever they are...gets my game of the year award :P

syspeat
March 29th, 2005, 08:17 AM
..gets my game of the year award :P

ill add it in my next game just to impress you. I just thought of it im my head, they could work...with the power of complete and utter ranodom attack.

Sirisian
March 29th, 2005, 04:42 PM
there fixed the code. I also copied the for loop and by changing the number 4 you can speed up or slow down how fast it rotates. Have fun. Any other questions about it I'll be happy to answer. Also i have some free time if u want I can make your game for you?(lol)

Mediamonkey
March 30th, 2005, 11:24 AM
how about this?

rotation = 0
easing = 5;

onEnterFrame = function() {
var dx = mc._x - _xmouse;
var dy = mc._y - _ymouse;
var angle = Math.atan2(dy, dx);

var diff = (angle*(180/Math.PI) - (rotation %= 360));
if (diff > 180) diff -= 360;
else if (diff < -180) diff += 360;
rotation += diff/easing;

mc._rotation = rotation;
}

*edit

oops, nevermind. I just read your code and you indeed use some variation of what i just wrote :)

Sirisian
March 30th, 2005, 04:54 PM
Does your code the exact same thing as the code I wrote if so I asted a lot of time

Mediamonkey
March 31st, 2005, 09:01 AM
You never waste time by learning more about actionscript ;)
It's basicly the same as your code, except for the second part, starting at 'var diff = ..'
That piece of code rotates your mc with some easing and at the same time solves your problem with the rocket always turning right.