00:00
00:00
mike
hello!

Mike Welsh @mike

Age 38, Male

stupids

Penn State

San Diego

Joined on 2/24/00

Level:
23
Exp Points:
5,596 / 5,880
Exp Rank:
8,110
Vote Power:
6.47 votes
Rank:
Police Captain
Global Rank:
3,933
Blams:
1,268
Saves:
1,003
B/P Bonus:
16%
Whistle:
Normal
Trophies:
10
Medals:
916
Supporter:
9y 11m 18d
Gear:
1

Wow it's good to see a thread about this.

My mind is "rusty" math wise. it's been a while.

I am trying to make my object go towards another but it doesn't seem to work. Can someone see what I am doing wrong ?

In the blow example I simply want to return the new point of m_objEntity. I want to move m_objEntity towards m_objTarget. The .Left propety is = "x" and the Top property is for "y"

Dim intSpeed As Integer = 4
Dim length As Double
Dim directionX As Double
Dim directionY As Double
Dim ReturnX As Double
Dim ReturnY As Double

directionX = m_objEntity.Left - m_objTarget.Left
directionY = m_objEntity.Top - m_objTarget.Top
length = Math.Sqrt((directionX * directionX) + (directionY * directionY))

' normalize (make it 1 unit length)
directionX = directionX / length
directionY = directionY / length

' scale to our desired speed
directionX = directionX * intSpeed
directionY = directionY * intSpeed

ReturnX = m_objTarget.Left + directionX
ReturnY = m_objTarget.Top + directionY

Return New Point(CType(ReturnX, Integer), CType(ReturnY, Integer))

BTW,

I can make it work using atan2, sin & cos with the following. Note that I want to return what I want to add to m_objEntity.. That's why the last two lines are commented.

Dim intSpeed As Integer = 4
Dim directionX As Double
Dim directionY As Double
Dim speedX As Double
Dim speedY As Double
Dim dblAngle As Double

directionX = m_objTarget.Left - m_objEntity.Left
directionY = m_objTarget.Top - m_objEntity.Top
dblAngle = Math.Atan2(directionY, directionX)
speedX = intSpeed * Math.Cos(dblAngle)
speedY = intSpeed * Math.Sin(dblAngle)
'm_objEntity.Left += CInt(speedX)
'm_objEntity.Top += CInt(speedY)

Return New Point(CType(speedX, Integer), CType(speedY, Integer))

I got it !!!!!!

here is the working code...needs tweaking if speed wont hit dead on the target because it will always "flicker" trying to reach the target

Dim intSpeed As Integer = 4
Dim length As Double
Dim directionX As Double
Dim directionY As Double

directionX = m_objTarget.Left - m_objEntity.Left
directionY = m_objTarget.Top - m_objEntity.Top
length = Math.Sqrt(directionX * directionX + directionY * directionY)

If length = 0 Then
Return New Point(0, 0)
End If

' normalize (make it 1 unit length)
directionX /= length
directionY /= length

' scale to our desired speed
directionX *= intSpeed
directionY *= intSpeed

'enemy.x += directionX
'enemy.y += directionY

Return New Point(CType(directionX, Integer), CType(directionY, Integer))

nice tutorial-thing. Oh, and it's only after i play the real Doom that i truly appreciate the greatness of your flash doom. good job.

YOU MUST DO OBEY FOR TO MAKE GLORIOUS CHINA FOR CHAIRMAN FULP!

im not so gread in math. In fact i suck at math. at least you could help other than an idiot like me.

most people think programing is easy, i say fuck them

Every time you post a blog update, I feel a little more like an uneducated ape.

The player is at position (playerX, playerY), and an enemy is at (enemyX, enemyY). You want the enemy to move 5 units directly toward the player. A lot of times, you'll see people do:

dx = player.x - enemy.x;
dy = player.y - enemy.y;
angle = Math.atan2( dy, dx );
speedX = 5 * Math.cos( angle );
speedY = 5 * Math.sin( angle );
enemy.x += speedX;
enemy.y += speedY;

It took us three hefty trigonometry operations: atan2, sin, and cos. First, we're going from the x and y components (dx and dy) to the angle using atan2. Then we're going BACK to the components using sin and cos! Surely we can avoid running in a circle like this! (haha, in a circle, get it?)

It's a little easier if you think with vectors. We already have a vector pointing in the direction we want: the <dx, dy> vector points straight from the enemy to the player! All we have to do is resize it to our desired speed.

dx = player.x - enemy.x;
dy = player.y - enemy.y;
length = Math.sqrt( dx*dx + dy*dy );
dx /= length; dy /= length; // normalize (make it 1 unit length)
dx *= 5; dy *= 5; // scale to our desired speed
enemy.x += dx;
enemy.y += dy;

We normalize the vector by dividing it by its length. Now our vector has a length of one unit, so we can scale it by our desired speed. Instead of three trig functions, we now have just a sqrt!

It's common to rotate your enemy to face the player. In this case, you DO want to use atan2 to get the angle. You can still avoid the sin and cos, though, by doing what we did above.

Just thought I'd share this little tip, hope it's helpful to someone. Here's a bad diagram:

MAKES 0 SENCE!!!!

im confumbled

... You're a god damn genius, when I was trying to make an enemy program, I went right onto trigonometry.
._.

Mike, I remember reading this article years ago, and having no idea what the hell you were blabbering on about, I wouldn't be able to make sense of it to save my life.

And now having gone through years of studying, allow me to show you and others how to do this calculation without even the square root function.

velocity = 5; // Desired speed for enemy.

offsetSquaredX = (player.x - enemy.x) * (player.x - enemy.x);
offsetSquaredY = (player.y - enemy.y) * (player.y - enemy.y);

distSquared = offsetSquaredX + offsetSquaredY;

dirVecX = offsetSquaredX / distSquared;
dirVecY = offsetSquaredY / distSquared;

enemy.x += dirVecX * velocity;
enemy.y += dirVecY * velocity;

Ultra fast calculations.

Nevermind, obviously I must be an idiot.