Rotate object to "follow" another object?

Started by
5 comments, last by ollyb342 12 years, 11 months ago
Hey guys,

I'm still working on my tower defense game at the minute, and I've hit another brick wall.

For testing I'd like to find a way of making the towers rotate around their origin on the Y axis to follow an enemy if it's within range of them.

So for example if an enemy is walking down the path towards them I'd like them to rotate slowly to ensure that they're always facing in the right direction.
if(e.getWorldPos().getX() < (-t.getPosition().getX()) + t.getRadius() && e.getWorldPos().getX() > (-t.getPosition().getX()) - t.getRadius())
{
double xRot = Math.sin(t.getPosition().getX() - e.getWorldPos().getX());
System.out.println(xRot);
t.setCurrentRot((float)xRot);
}


Obviously I'll do the same for the Y axis checking as well, but am I anywhere near the right track for this? I have no idea if Sin's the correct mathematical function to use here, sorry.

Any assistance will be helpful here.

BTW: e is the very first enemy in the wave and t is a tower.

Cheers,

Ollie.
Advertisement
So basically what you want to achieve is to set the tower to be facing an enemy right? Well, the approach I would go with is to compute a facing vector for the tower ( It would be a unit vector transformed by the tower rotation) and a vector between tower an the enemy ( e.getPosition() - t.getPosition()). Then you will need to compute the angle between these two, you achieve this by normalizing these two vectors and computing the acos from the dot product. Hope this helps a little ;)
could also get rid of that horrible multi-predicate in the if statement if do as kzyg suggest... if (vecBetweenEnemyAndTower.magnitude < t.radius) {...}
kzyg,

That's extremely helpful, thankyou. I'll put some code together in a while and let you know if it works out!

As an aside, you mentioned a facing vector; would this be in a similar format to the "up" vector used in a lot of camera classes (i.e it's going to start by facing the positive Z axis, so my facing vector would start with some thing like vec3(0,0,1) then obviously multiplied by the current rotation..)?

Other than that it all seems pretty clear, thankyou! (although I am now a bit ashamed of my poor maths skills!)

And Dehebo, good suggestion thanks man; I'll definitely put that in.

Cheers for the suggestions!
Yes, I think that's correct, in other words the face vector is a translated unit vector in the objects local space (indicating where the object is facing xD lol).
Don't worry about the maths, I had the same problem few weeks ago :P. Glad I could help ;)


As an aside, you mentioned a facing vector; would this be in a similar format to the "up" vector used in a lot of camera classes (i.e it's going to start by facing the positive Z axis, so my facing vector would start with some thing like vec3(0,0,1) then obviously multiplied by the current rotation..)?


Yes, infact you can get rid of angles entirely if you want (its easier to work without angles alot of the time). You have 3 vectors, on is the "up" vector you mention, one is the "forward" vector (the direction your tower is facing) and the last is the "right" vector, which points right of where your tower is facing. All 3 vectors are at right angles to each other and if theres no rotation they will probable be right(1, 0, 0), up(0, 1, 0), forward(0, 0, 1). From those you can create a matrix that represents the orientation of your tower. If you know 2 of them (the up vector is usually always the same, it just points up - the forward vector is where yuor looking normalise((target - position))) you can use the cross product to get the 3rd.

Its definately an easier way to do "look at" than calculating and using an angle.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Cheers for the suggestion Nanoha, that's definitely something I'll look into aswell.

This is turning out to be an awesome little project now! Only things I regret so far are using immediate mode OpenGL and using some crazy transformations at random points in my code, so now it's all a little bit messy/difficult to maintain =/

Cheers for the help guys!

Ollie.

This topic is closed to new replies.

Advertisement