(I’m using the mobile app) I have a sprite that spawns enemies away from it, which face towards the spawner, but I can’t figure out how to make the enemies move towards the spawner. There’s literally code for moving towards a sprite, but it seems not to work for some reason. Is there a way to set velocity for my enemies based on its position? Part of the problem is that when you rotate a sprite, the x velocity still goes in the x plane, and doesn’t follow the rotation
Good day!
Show you code for the enemy moving.
Basically, you code must set the enemy position in the every iteraion of the game cycle.
You can use the spawner as anchor. You hold his position and calculate from them enemies position every frame. If enemy is moving, you add to the current Enemy.Position.X
and Enemy.Position.Y
some numbers.
If you want to change enemy speed in depend of the distance to spawner, you can just use something like this:
Enemy.Speed := Min(SpeedOfLight, Enemy.DistanceToSpawner / LevelMapSize + Enemy.BasicSpeed);
Where:
SpeedOfLight - maximum speed of the enemy in you game,
LevelMapSize - distance from the one map side to another. For example, if spawner and enemy in the opposite sides of map, you add 1 to the his speed. If distance is the half of map - only 0.5.
BasicSpeed - the enemy speed without modefications.
To expand on the above answer:
-
If we could see your exact code, and/or more description how do you move things, this would help – maybe we can give more precise advise then
-
One can move and rotate things using CGE in 2 ways (broadly speaking):
- Directly, by changing
TCastleTransform.Translation
/TCastleTransform.Rotation
. - Using physics, when
TCastleTransform
hasTCastleRigidBody
attached, and you move it applying physics forces, following Physics | Manual | Castle Game Engine .
In both cases, you need to be aware that transformations form a hierarchy.
TCastleTransform.Translation
/TCastleTransform.Rotation
are local transformations. When the object is displayed, it is affected by accumulated transformations of the given object and all its parents.They are routines to convert between these coordinate systems.
- We have
TCastleTransform.GetView
/TCastleTransform.SetView
(local) andTCastleTransform.GetWorldView
/TCastleTransform.SetWorldView
(world-space, in the viewport). - We have
TCastleTransform.Translation
(local) andTCastleTransform.WorldTranslation
(world-space). - Rotations can be expressed either as 4D vector (axis and angle of rotation), like
TCastleTransform.Rotation
, or as resulting 3D Direction and Up (TCastleTransform.Direction
,TCastleTransform.Up
). The “Direction” and “Up” make sense if you follow the consistent conventions for “what axis is up”, seeTCastleTransform.Orientation
. For world-space, we haveTCastleTransform.WorldDirection
only. - You can convert between coordinate systems using
TCastleTransform.LocalToWorld
,TCastleTransform.LocalToWorldDirection
,TCastleTransform.WorldToLocal
,TCastleTransform.WorldToLocalDirection
. - (I give above names of various API properties – you can find documentation of them all in our API docs).
- The physics forces methods generally take world-space coordinates for things, but consult their documentation for details. See links from Physics | Manual | Castle Game Engine .
- Directly, by changing
I hope these links help how to deal with positions, directions, and thus velocities. The general advise is that you need to be aware in which coordinate system you “think”, and make sure to perform calculations in the same coordinate system.
Sorry I wish I understood this more. I have variable X and Y change every frame, but I don’t know how I would translate that to the velocity of a sprite
Globally, speed - is the measure of the changes.
If you walk and you position changes to the 3 kilometers after 30 minutes, it means, you speed is 6 km/hour (or literally 3 km per 30 minutes).
Simitelly, if you have (X1, Y1) and (X2, Y2) speed is distance between them, devided to the time between measures.
So, you changes of the X and Y is already is speed.
But usually you need to use special variable to scale this speed for the game reality.
You can see it in this example:
Chapter 6, “Move the player in the Update method”