Saturday, January 16, 2021

AI Understanding Pac-Man Ghost Behavior

 https://gameinternals.com/understanding-pac-man-ghost-behavior?fbclid=IwAR2AlMX2ofDrYJC8G596xBxAWd8eoK7emtiAOeyb5v0lI5Nx0T3_emImaMc

Thursday, January 14, 2021

Explain about Common terminology used throughout the official Spine runtimes.

 http://esotericsoftware.com/spine-runtime-terminology/


Atlas An atlas, also known as a texture atlas, stores named regions of a texture. Spine can perform texture packing to create an atlas, or external tools such as Texture Packer Pro can be used (most runtimes use the “libgdx” atlas format).

Animation An animation stores a list of timelines. Each timeline stores keys, each of which have a time and one or more values. When the animation is applied, the timelines use the keys to manipulate the skeleton, fire events, etc. The animation does not store any state.

AnimationState An animation state is a convenience class that holds the state for applying one or more animations to a skeleton. It has the notion of “tracks” which are indexed starting at zero. The animation for each track is applied in sequence each frame, allowing animations to be applied on top of each other. Each track can have animations queued for later playback. Animation state also handles mixing (crossfading) between animations when the current animation changes.

Attachment An attachment is something that is attached to a bone by being placed on a slot. For example, a texture region or bounding box.

AttachmentLoader An attachment loader is used by SkeletonJson to create attachments. This is a hook to provide your own attachment implementations, eg to do lazy loading. Most commonly an attachment loader is used to customize where the images come from for region attachments.

Bone A bone has a local transform (SRT) which child bones inherit. A bone also has a world transform, which is the combination of all parent bone transforms with the local transform. The world transform uses the same coordinate system the root bone is defined in.

Bounding box attachment An attachment that has a polygon for performing hit detection, physics simulation, etc.

Draw order Draw order is a list of slots on a skeleton. The order of the list is the order each slot’s attachment should be drawn, from back to front.

Mixing Mixing, also known as crossfading, is applying an animation by blending linearly between the current pose and the pose for the animation.

Region attachment An attachment that has a texture region and an offset SRT, which is used to position the region relative to the attachment’s bone.

Slot A slot is a placeholder on a bone. A slot can have either a single attachment or no attachment at all. It also has a color and the time elapsed since its attachment was changed.

Skeleton A skeleton holds the state of a skeleton. This includes the current pose, bones, slots, draw order, etc.

SkeletonBounds A skeleton bounds is a convenience class for performing hit detection for a skeleton using the currently attached bounding box attachments.

SkeletonData The skeleton data contains the skeleton information (bind pose bones, slots, draw order, attachments, skins, etc) and animations but does not hold any state. Multiple skeletons can share the same skeleton data.

SkeletonJson The skeleton JSON loads a SkeletonData from JSON.

SkeletonRenderer The skeleton renderer iterates over the slots in the draw order for a skeleton and knows how to render various attachments.

Skin A skin is a map where the key is a slot and name and the value is an attachment. A skin is a level of indirection which allows specific attachments to be found using a slot and a non-specific name. For example, a skin might have a key of [slot:head,name:head] with a value of [attachment:redHead]. Another skin might have [attachment:greenHead] for the same key. The non-specific name is used in animations, enabling animations that change attachments to be reused with skeletons that have different attachments.

SRT Scale, rotation, and translation. Also known as the “transform”.

Transform The scale, rotation, and translation.

Tuesday, January 12, 2021

Orbit around game object

  public Transform target;


    void Update()
    {
        Vector3 relativePos = target.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);

        Quaternion current = transform.localRotation;

        transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime);
        transform.Translate(0, 0, 3 * Time.deltaTime);
    } 

Pokemon Unity with NPC behavior, Sound Handler

 https://github.com/PokemonUnity/PokemonUnity/blob/Beta_Feature_NetworkPlay/Pokemon%20Unity/Assets/Scripts/Alpha/NPCHandler.cs

Saturday, January 2, 2021

Finding angle between 2D point

 http://mathforum.org/library/drmath/view/61081.html


Finding an Angle

Date: 08/09/2002 at 08:43:00
From: Timo Petmanson
Subject: Sine and cosine maths

Hello, 

I need to know how to calculate an angle between two 2D points. I know 
how to calculate a new point by angle, but I can't figure out how 
otherwise.

Calculating by angle:

a = 143 // this is the angle
x = 1   // 2d point x coord
y = 1   // 2d point y coord
u = 3   // how many units we change coords

So, if we want to change point's coordinates by angle, then we must 
do:

x = x + SIN(a * (pi / 180)) * u  // (pi / 180)radians to degrees
y = y + COS(a * (pi / 180)) * u  // I hope I didn't change sin and cos

but what must I do to calculate the angle?

x1 = 1
y1 = 1
x2 = (-1)
y2 = (-5)

0
|      (x2,y2)
|      /
|angle/
|    /
|   /
|  /
| /
(x1,y1)------------ 90
|
|
|
|
|
|
|
180

I hope you understood my problem. Thanks.

Timo

Date: 08/09/2002 at 09:48:14
From: Doctor Rick
Subject: Re: Sine and cosine maths

Hi, Timo.

Yes, I understand: you know how to find the coordinates of a point 
given the starting point, angle and distance, but you don't know how 
to do the reverse - calculate the angle given the starting point and 
ending point. (You probably do know how to calculate the distance.)

We can take your equations as a starting point. You did have sin and 
cos interchanged, if the angle is measured counterclockwise from the 
positive x axis as we usually do. I'll call the two points (x1,y1) 
and (x2,y2).

  x2 = x1 + cos(a * (pi / 180)) * u
  y2 = y1 + sin(a * (pi / 180)) * u

Subtract the starting point coordinates:

  x2 - x1 = cos(a * (pi / 180)) * u
  y2 - y1 = sin(a * (pi / 180)) * u

Divide the second equation by the first:

  (y2-y1)/(x2-x1) = (sin(a*pi/180)*u)/(cos(a*pi/180)*u)

What is sin/cos? It's the tangent.

  (y2-y1)/(x2-x1) = tan(a*pi/180)

We can find the angle by taking the inverse tangent (arctan) of both 
sides:

  a*pi/180 = arctan((y2-y1)/(x2-x1))

  a = 180/pi * arctan((y2-y1)/(x2-x1))

There's your formula. However, there are some tricky things to add. 
One is that x2 may equal x1, and you'll get a divide-by-zero error. 
Another is that the arctan won't distinguish between the angle from 
point 1 to point 2 and the angle from point 2 to point 1. These should 
be 180 degrees apart, but they will come out identical. In summary, to 
do it right, you need to consider separate cases for x2-x1 negative, 
zero or positive.

It looks as if you're writing a program in C++. If so, check out the 
function atan2(y,x). It computes the arctangent of y/x, and it takes 
care of those special cases too, because it is designed for exactly 
the sort of thing you are doing.

- Doctor Rick, The Math Forum
  http://mathforum.org/dr.math/ 

Date: 08/20/2002 at 14:10:09
From: Timo Petmanson
Subject: Thank you (sine and cosine maths)

Thanks, that helped me.  I was working on a computer game and I needed 
that formula for the movements.

How to Add Collider to Line Renderer in Unity

http://www.theappguruz.com/blog/add-collider-to-line-renderer-unity


 The objective of this blog post is to explain how to add Collider to Line Renderer or how to draw physics line like “Free Rider” game in unity.

 

 

Step 1 Introduction

The main purpose of posting this blog is to clear all the doubts in previously uploaded blog “Unity – Draw Line on mouse move and detect line collision in Unity 2D and Unity 3D”, regarding how to draw line with collider as in “Free Rider” game.

In previous blog Draw Line on mouse move and detect line collision in Unity 2D and Unity 3D, how line can be drawn on mouse movement using Line Renderer component and detect line collision, is explained. Now here, how to draw physics line like Free Rider game is explained and to accomplish this, we need to add collider to line.

 

Step 2 Basic Steps

Follow the following steps to add collider on Line Renderer:
• Draw a line using line renderer component.
• Make an empty child game object of the line object.
• Now add BoxCollider component to this empty child object.
• Set its size according to the line’s length and width.
• Set its position according to line’s position elements.
• Calculate the angle between first element of line’s position and second element of line’s position and set Z coordinate of eulerAngles of empty child object to this calculated angle.

 

Step 3 Example

The following C# script will draw straight line between mouse down and mouse up positions (or touch begin and touch end positions) and add collider on it.

Screenshots:

draw-straight-line
line-renderer
box-colider
example
example1
 

3.1 DrawPhysicsLine Script

  • Add following script on empty game object.
  • using UnityEngine;
  • using System.Collections;
  • public class DrawPhysicsLine : MonoBehaviour
  • {
  •     private LineRenderer line; // Reference to LineRenderer
  •     private Vector3 mousePos;    
  •     private Vector3 startPos;    // Start position of line
  •     private Vector3 endPos;    // End position of line
  •     void Update ()
  •     {
  •         
  •         // On mouse down new line will be created
  •         if(Input.GetMouseButtonDown(0))
  •         {
  •             if(line == null)
  •                 createLine();
  •             mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  •             mousePos.z = 0;
  •             line.SetPosition(0,mousePos);
  •             startPos = mousePos;
  •         }
  •         else if(Input.GetMouseButtonUp(0))
  •         {
  •             if(line)
  •             {
  •                 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  •                 mousePos.z = 0;
  •                 line.SetPosition(1,mousePos);
  •                 endPos = mousePos;
  •                 addColliderToLine();
  •                 line = null;
  •             }
  •         }
  •         else if(Input.GetMouseButton(0))
  •         {
  •             if(line)
  •             {
  •                 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  •                 mousePos.z = 0;
  •                 line.SetPosition(1,mousePos);
  •             }
  •         }
  •     }
  •     // Following method creates line runtime using Line Renderer component
  •     private void createLine()
  •     {
  •         line = new GameObject("Line").AddComponent<LineRenderer>();
  •         line.material = new Material(Shader.Find("Diffuse"));
  •         line.SetVertexCount(2);
  •         line.SetWidth(0.1f,0.1f);
  •         line.SetColors(Color.black, Color.black);
  •         line.useWorldSpace = true;
  •     }
  •     // Following method adds collider to created line
  •     private void addColliderToLine()
  •     {
  •         BoxCollider col = new GameObject("Collider").AddComponent<BoxCollider> ();
  •         col.transform.parent = line.transform; // Collider is added as child object of line
  •         float lineLength = Vector3.Distance (startPos, endPos); // length of line
  •         col.size = new Vector3 (lineLength, 0.1f, 1f); // size of collider is set where X is length of line, Y is width of line, Z will be set as per requirement
  •         Vector3 midPoint = (startPos + endPos)/2;
  •         col.transform.position = midPoint; // setting position of collider object
  •         // Following lines calculate the angle between startPos and endPos
  •         float angle = (Mathf.Abs (startPos.y - endPos.y) / Mathf.Abs (startPos.x - endPos.x));
  •         if((startPos.y<endPos.y && startPos.x>endPos.x) || (endPos.y<startPos.y && endPos.x>startPos.x))
  •         {
  •             angle*=-1;
  •         }
  •         angle = Mathf.Rad2Deg * Mathf.Atan (angle);
  •         col.transform.Rotate (0, 0, angle);
  •     }

I hope you find this blog very helpful while adding collider to Line renderer in Unity. Let me know in comment if you have any questions regarding Unity. I will reply you ASAP.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.