Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7890191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:17:27+00:00 2026-06-03T06:17:27+00:00

I have this enemy class, and I want it to 1) Spawn at a

  • 0

I have this enemy class, and I want it to

1) Spawn at a certain place Vector3 pos

2) Rotate to face my player position

3) Move forward

As this code is now, it will appear at it’s specified place: pos, and attempt to rotate to face it’s target: targetShip. It fails to correctly face it’s target, because of this offset.

If I remove the code that assigns a value to translation, or if I normalize pos then the model will appear at the origin and rotate to face it’s target correctly.

If I try to move it at all using any of the comment out code regarding translation, It appears to start someplace else and I never can find it.

However, if I remove the code relating to rotation, and uncomment the code relating to translation, then I can get it to move forward.

The trick is doing it all together.

class Enemy : BasicModel
{


    Matrix rotation = Matrix.Identity;
    Matrix translation = Matrix.Identity;
    public Vector3 pos, up, right, targetShip,dir;

    public Enemy(Model m, Vector3 pos)
        : base(m)
    {
        up = Vector3.Up;
        //sets the position to the Vector3 as it's spawn point.
        translation = Matrix.CreateTranslation(pos);

    }

    public override void Update()
    {
        //Glo is a global class, where I store the player world.
        targetShip = Glo.world.Translation;
        targetShip.Normalize();

      pos = transform.Translation;

      rotation = RotateToFace(targetShip, pos, Vector3.Up);
      //Attempt at moving the model forward. Causes it to go out of view
      //translation *= Matrix.CreateTranslation(this.GetWorld().Backward);
      //translation *= Matrix.CreateTranslation(pos);
    }

    public override Matrix GetWorld()
    {
        return rotation * world * translation ;

    }

    /*Params: O = Our target
     * P = Our position
     * U = up.
     * 
     * Code from some site I googled up.
      */
    Matrix RotateToFace(Vector3 O, Vector3 P, Vector3 U)
    {

        //The direction we're facing.
        Vector3 D = (O - P);
        //Our relative Right.
        Vector3 Right = Vector3.Cross(U, D);
        Vector3.Normalize(ref Right, out Right);
        //Our back
        Vector3 Backwards = Vector3.Cross(Right, U);
        Vector3.Normalize(ref Backwards, out Backwards);
        //Our relative up
        Vector3 Up = Vector3.Cross(Backwards, Right);
        //Make a matrix out of all of these.
        Matrix rot = new Matrix(Right.X, Right.Y, Right.Z, 0, Up.X, Up.Y, Up.Z, 0, Backwards.X, Backwards.Y, Backwards.Z, 0, 0, 0, 0, 1);
        return rot;
    }



}

}

—-


I tried doing

anathonline’s method, and here’s what I have now
It doesn’t work. I can’t see my model anywhere.

public override void Update()
        {
            targetShip = Glo.world.Translation;
            targetShip.Normalize();

            //Translate it to the origin
            translation = Matrix.Identity;
            //Orient it
            rot = RotateToFace(targetShip, translation.Translation, Vector3.Up);
            //Move it back to where it was originally;
            translation = Matrix.CreateTranslation(offset);
            //Calculate the front of the object
            Vector3 front = Vector3.Forward * translation.Translation;
            //normalize it
            Vector3.Normalize(ref front, out front);
            //multiply it
            front *= 2;
            //translate the object
            translation *= Matrix.CreateTranslation(front);



        }

        public override Matrix GetWorld()
        {
            return world * rot *  translation;

        }

—-


2nd revision: Still doesn’t work

public override void Update()
    {
        targetShip = Glo.world.Translation;
        targetShip.Normalize();


         translation = Matrix.CreateLookAt(offset, targetShip, Vector3.Up);
        var amountToMove = 0.2f;
        var finalPos = Vector3.Lerp(targetShip, offset, amountToMove);
        rot = translation * Matrix.CreateTranslation(finalPos); 


    }

    public override Matrix GetWorld()
    {
        return world * rot * translation ;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:17:29+00:00Added an answer on June 3, 2026 at 6:17 am

    I fixed it, don’t worry. All of this code is obsolete anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem: there is this enemy which rotates to my player. While
I have an Entity baseclass which the classes Player and Enemy Inherit. class Entity
Ok, I have this main class called Enemy, and inside it I have subclasses
In my game, all game objects extend an Entity class. All enemy objects have
Essentially its a pacman clone game I'm working on. I have an Enemy class,
I have an Entity baseclass which the classes Player and Enemy Inherit. Both player
Lets say we have a movieclip Enemy in the Flash library and a class
This system is really confusing me. I'm rotating a turret to face an enemy
I have the following class: class Enemy(pygame.sprite.Sprite): def __init__(self): pygame.init() pygame.sprite.Sprite.__init__(self) # Basic variables
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.