How do you make a 'homing missile'?

For topics that don't fit under the other topics or forums.

How do you make a 'homing missile'?

Postby icrls984 » Sat Jul 20, 2013 9:18 am

I've seen some scenes on algobox with homing 'missiles'. I also want to make one but I don't know any scripting. Could someone please help? Thanks
.
User avatar
icrls984
 
Posts: 80
Joined: Thu Jun 13, 2013 6:33 am

Re: How do you make a 'homing missile'?

Postby racergonemad » Sun Jan 05, 2014 6:53 am

There are a few ways of going about this. The simplest way would be to just make a script that makes the velocity vector of your "missile", proportional to the relative vector between the position of the object and your target (in these examples, the mouse). Add the following code in postStep in the script menu:
Code: Select all
(e)=>{
    e.this.vel := [(app.mousePos(0) - e.this.pos(0)), (app.mousePos(1) - e.this.pos(1))]
}

Another, more complicated but more physically realistic way to do this is to add the following code to your missile body where it says postStep:
Code: Select all
(e)=>{
    (app.mousePos(0) - e.this.pos(0)) <= 0 ? {
        e.this._mouseAngle := math.pi + (math.atan((app.mousePos(1) - e.this.pos(1)) / (app.mousePos(0) - e.this.pos(0))))
    } : {
        (app.mousePos(1) - e.this.pos(1)) >= 0 ? {
            e.this._mouseAngle := math.atan((app.mousePos(1) - e.this.pos(1)) / (app.mousePos(0) - e.this.pos(0)))
        } : {
            e.this._mouseAngle := 2 * math.pi + (math.atan((app.mousePos(1) - e.this.pos(1)) / (app.mousePos(0) - e.this.pos(0))))
        }
    };
    e.this.angle <= 0 ? {
        e.this._rotation := (2 * math.pi) + e.this.angle
    } : {
        e.this._rotation := e.this.angle
    };
    (e.this._rotation - e.this._mouseAngle) >= math.pi || (e.this._rotation - e.this._mouseAngle) <= -1 * math.pi ? {
        e.this.controllerAcc := e.this._mouseAngle - e.this._rotation;
        e.this.color := [0.8, 0, 0, 1]
    } : {
        e.this.controllerAcc := e.this._rotation - e.this._mouseAngle;
        e.this.color := [0.1, 0.3, 0.5, 1]
    }
}

Next, add two thrusters on each side of the body of the missile pointing in the same direction, and in their script menus, where it says Force, add the following codes (one for each):
Code: Select all
{
    0.05 * (scene.entityByGeomID((readable(entity)).geom)).controllerAcc + ((math.vec.dist(app.mousePos, pos) * 0.01) - 0.001)
}

Code: Select all
{
    -0.05 * (scene.entityByGeomID((readable(entity)).geom)).controllerAcc + ((math.vec.dist(app.mousePos, pos) * 0.01) - 0.001)
}

Finally, go to the script menu of the body, and on top of all the parameters, right below where it says "Use at own risk!", type the following in the black box and press enter:
Code: Select all
_target := [0,0]

You can experiment with the constants in the Force parameters of the thrusters to get different performance results. For me, the best results are with working with a round body with about 0.1m radius, and adding a low density tail to add aerodynamic stability.
Attachments
Mouse-Chase Bot 2.phz
(12.23 KiB) Downloaded 76 times
My signature is worth a lot of money, So I'm gonna put this stamp instead:
[$20 PER AUTOGRAPH]
racergonemad
 
Posts: 26
Joined: Tue Aug 17, 2010 7:37 am

Re: How do you make a 'homing missile'?

Postby icrls984 » Mon Jan 20, 2014 1:53 am

Thanks!
.
User avatar
icrls984
 
Posts: 80
Joined: Thu Jun 13, 2013 6:33 am


Return to Algodoo in general

Who is online

Users browsing this forum: No registered users and 1 guest

cron