Homing missile

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
neoxeo
Posts: 5
Joined: Tue Mar 07, 2017 7:57 pm

Homing missile

Post by neoxeo »

Hi,

Thank you for BEPUphysics that I have discovered few days ago.

I try to convert this Unity code to BepuPhysics :

Code: Select all

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
  
 public class Missile : MonoBehaviour
 {
     public float missileVelocity = 100f;
     public float turn = 20f;
     public Rigidbody homingMissile;
     public Transform Enemy;
 
     private Transform target;
	 
     // Use this for initialization
     void Start()
     {
         var closestGameObject = GameObject.FindGameObjectsWithTag("Enemy").OrderBy(go => Vector3.Distance(go.transform.position, transform.position)).FirstOrDefault();
         float dist = Vector3.Distance(closestGameObject.transform.position, transform.position);
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         if (target == null || homingMissile == null)
             return;
 
         homingMissile.velocity = transform.forward * missileVelocity;
 
         Quaternion targetRotation = Quaternion.LookRotation(target.position - transform.position);
 
         homingMissile.MoveRotation(Quaternion.RotateTowards(transform.rotation, targetRotation, turn));
     }
 }
I have an existing flight simulator and I want to use BEPUphysics JUST to compute the position of missile and transmit it to simulator. (I don't need to have a renderer)

Thank you very much for your help.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Homing missile

Post by Norbo »

Do you have any specific questions? At a high level, a lot of the concepts are similar or identical; the differences are mainly just in the names and API locations of objects and functions.

I would recommend looking at the BEPUphysicsDemos demos and the documentation for an introduction to the library.
neoxeo
Posts: 5
Joined: Tue Mar 07, 2017 7:57 pm

Re: Homing missile

Post by neoxeo »

Hi Norbo,

I need help to find the equivalent of :
Rigidbody (Entity ?)
Transform
Rigidbody.velocity
Quaternion.LookRotation (BEPUutilities.Quaternion ?)
Quaternion.RotateTowards (BEPUutilities.Quaternion ?)

in BEPUPhysics.

Thanks
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Homing missile

Post by Norbo »

When it comes to this kind of thing, I would recommend just fiddling with stuff and exploring. Jump into the BEPUphysicsDemos, try modifying them a little to see how stuff reacts. It's a good way to get a feel for the API with rapid feedback. That said:

RigidBody: Yup, Entity.

Transform: You could use a Matrix3x3 to store rotation, and a separate Vector3- which happens to be the layout of an AffineTransform. The Matrix3x3 has a Forward property, too. Another option would be a full Matrix, though it has some wasted rows. RigidTransform would work, but the orientation is stored as a quaternion- so extracting the Forward direction is a bit less direct.

Rigidbody.velocity: Presumably Entity.LinearVelocity. There's also AngularVelocity.

Quaternion.LookRotation: Assuming that this is creating an orientation with an implicit up vector, there's not a direct analog on the Quaternion. There's a Matrix.CreateWorldRH, though that includes a translation component. If you look at the source code, it's pretty simple to create a 3x3 matrix version without translation, or you can just use the full 4x4 version and create a Quaternion from it.

Quaternion.RotateTowards: Assuming this uses the 'turn' as some sort of maximum change in angle, you could use Quaternion.GetRelativeRotation and Quaternion.GetAxisAngleFromQuaternion on the result, then Quaternion.CreateFromAxisAngle with a limited version of the angle, and finally a concatenation to get the new rotation. That's a little indirect, and there are other ways to go about it. If you just want to move immediately to the target rotation, just directly set the entity's orientation. You could also easily do some kind of exponential smoothing with Quaternion.Slerp with an interpolation of 0.2 or something.

Note that it's generally a bad idea to change position or orientation directly, since any form of 'teleportation' is not affected or stopped by collision. If you want collisions to be able to stop the motion, the control should be on a velocity level. So, for example, rather than directly setting the orientation, change the AngularVelocity of the entity so that the orientation changes over time in a dynamic way.
neoxeo
Posts: 5
Joined: Tue Mar 07, 2017 7:57 pm

Re: Homing missile

Post by neoxeo »

Hi Norbo,

Thank you very much for your help.
neoxeo
Posts: 5
Joined: Tue Mar 07, 2017 7:57 pm

Re: Homing missile

Post by neoxeo »

Hi Norbo,

I try to compile demos to try to progress with Bepu but I have errors in LineDrawer.cs :
public void Deactivate(Line line)
{
line.ColorA = new Color(line.ColorA.R, line.ColorA.G, line.ColorA.B, 0);
line.ColorB = new Color(line.ColorB.R, line.ColorB.G, line.ColorB.B, 0);
}

/// <summary>
/// Makes a line visible.
/// </summary>
/// <param name="line">Line to make visible.</param>
public void Activate(Line line)
{
line.ColorA = new Color(line.ColorA.R, line.ColorA.G, line.ColorA.B, 255);
line.ColorB = new Color(line.ColorB.R, line.ColorB.G, line.ColorB.B, 255);
}
Error : The call is ambiguous between the following methods or properties: 'Color.Color(int, int, int, int)' and 'Color.Color(byte, byte, byte, byte)'


I have try to "cast" in byte, I have no error with these lines but have a new error in
instancingEffect = game.Content.Load<Effect>("InstancedEffect");
in InstanciedModelDrawer.cs.

An unhandled exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll
Additional information: HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: Paramètre incorrect.

Thank you for your help.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Homing missile

Post by Norbo »

This appears to be a compatibility issue with MonoGame 3.6.

While the new byte overload is easy enough to address, I am having issues getting through some other assembly load problems. At this rate I might just end up repackaging monogame and its build tools locally, if the license allows it- that would stop it from breaking when someone has a different version (or maybe even no version) installed.

As a workaround, installing 3.5 should work, though it's not an ideal solution by any means.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Homing missile

Post by Norbo »

Okay, managed to get everything hopefully working now. I just grabbed all the dependencies so that other versions of monogame being installed won't pull the rug out from under it.
neoxeo
Posts: 5
Joined: Tue Mar 07, 2017 7:57 pm

Re: Homing missile

Post by neoxeo »

Downloaded and tested : all is OK.

Thanks again.

I will try to understand how BEPUphysics works with all demos :)
Post Reply