Android Question Tower Defence game questions

coldteam

Active Member
Licensed User
Longtime User
Hello! Do TowerDefence game. There are a few issues. I need your help.
Use LibGDX
1. I create monsters like this:

B4X:
Dim img_monster As lgTexture
Type typeMonster(x As Float, y As Float,HP As Int,SPEED As Int)
Dim Monster As typeMonster
Dim Monster_Region As lgTextureRegion

img_monster.Initialize("monster.png")
Monster_Region.InitializeWithTexture(img_monster)

....
Sub LG_Render
....
Batch.DrawRegion2(Monster_Region,Monster.x,Monster.y,width,height)
....

question:
how to track a collision this objects?

2.
want the area around the tower, when the monster comes into the area, the tower begins to shoot

here, too, you need to keep track of the collision? how?

3.
shooting
if Rocket flies in a moving target, how to calculate the next coordinates to move the rocket


4.
how to control what is to be the foreground and the background

 

ilan

Expert
Licensed User
Longtime User
how to track a collision this objects

1. If your shapes are circles you just need to check if the distance between both is smaller the the radius of both.
Look at andymc cloney bird example for rectangle shapes.

here, too, you need to keep track of the collision? how?

2. same question like question1 just calculate the distance between both objects and do the action if distance is smaller the x

how to control what is to be the foreground and the background

What u draw last is in forground so just look at your drawing order.
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
Thank you, my friend! it just count using a triangle
when creating games this method, use a timer for calc time or render only ?
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
3.
shooting
if Rocket flies in a moving target, how to calculate the next coordinates to move the rocket
-----------------------------------------------------
I did it not very well

I think the difference in x, difference in y, and is calculated as a moving axis faster

how to do it correctly?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User

something like this:

B4X:
Sub NextPosition(Target As lgMathVector2) As lgMathVector2
    Dim distance As lgMathVector2
    distance = Target.sub(rocketposition)
    Dim time As Int = 3 'second to hit
    Dim followspeed As lgMathVector2
    followspeed.x = distance.x * (1/time)
    followspeed.y = distance.y * (1/time)
    Return followspeed
End Sub
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
thanks for the answer

ok i do:
B4X:
Dim roc_pos as lgMathVector2
Dim target_v As lgMathVector2
target_v.x = target_center_x
target_v.y = target_center_y
Dim rocet_v As lgMathVector2
rocet_v.x = rocet_center_x
rocet_v.y = rocet_center_y
...
Sub NextPosition(Target As lgMathVector2, rocketposition As lgMathVector2) As lgMathVector2
Dim distance AslgMathVector2
distance = Target.sub(rocketposition)
Dim time As Int = 3'second to hit
Dim followspeed AslgMathVector2
followspeed.x = distance.x * (1/time)
followspeed.y = distance.y * (1/time)Return followspeed
End Sub
...
and in render
...
Rocet.x = NextPosition(target_v,rocet_v).x
Rocet.y = NextPosition(target_v,rocet_v).y
...

like this ?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
You need to return the followspeed vector
So add:

Return followspeed
Before end sub (in nextpisition)

You also dont need to calculate the vector twice

Create a new vector and return followspeed and use this vector as your next step

Dim v as lgMathVector2 = NextPosition(target_v,rocet_v)

Rocet.x = rocet.x + v.x
Rocet.y = rocet.y + v.y
 
Last edited:
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
my code:
B4X:
{render}
Dim target_v As lgMathVector2
target_v.Set(target_center_x,target_center_y)
Dim rocet_v As lgMathVector2
rocet_v.Set(rocet_center_x,rocet_center_y)

Dim v As lgMathVector2 = NextPosition(target_v,rocet_v)
Rocet.x = v.x
Rocet.y = v.y
{render}
...

Sub NextPosition(Target As lgMathVector2, Rocet As lgMathVector2) As lgMathVector2
    Dim distance As lgMathVector2
    distance = Target.sub(Rocet)
    Dim time As Int = 3 'second to hit
    Dim followspeed As lgMathVector2
    followspeed.x = distance.x * (1/time)
    followspeed.y = distance.y * (1/time)
    Return followspeed
End Sub

but its not work
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
perfect, thank you my friend

Note that your rocket will slow down when it get closer to the target because you always give 3 sec for the route.
So if you want a constant speed you can store the time when you fired the rocket and calculate your route time minus the difference between start time and actual time
Like this you will have a constant speed
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
question, if want to move is not by time. by FPS. If 60 fps => object each frame is shifted by 2 pixels to the target

or may be another way for movement to the enemy to target.
target is changing
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you'll need to calculate a stepper speed.

something in the line of stepx=(endx-startx)/steps
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User

green circle move to the zone 1 with speed 2 pixels in frame. when it is within range, then it begins to move into the zone 2.

It turns and motion is possible for X and Y

the distance between the zones are very different
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
the code above will move the rocket to a static or moving target if you update the distance each frame.
you can use any approach you want time, distance...

the code will give you the distance in the x and y axis and you can move the rocket with any value you like.

if it reaches target one you update the target to target 2 and the rocket will then move to target 2

try to use instead of time distance:

B4X:
    followspeed.x = min(distance.x,2dip)
    followspeed.y = min(distance.y,2dip)


this will choose the smaller value from both values so if distance.y is 0 then it wont move in the y axis but if its larger then 2dip it will move only 2dip.

EDIT: this may not be a full solution since you have to take in account negative values its just an example. so you have to check if distance.y is negative or positive if its negative then you should use -2dip and also use MAX instead of MIN.
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
without vectors i do it like this:

B4X:
Dim zone_x As Float = ZONE_THIS.x + ZONE_THIS.W/2
                Dim zone_y As Float = ZONE_THIS.y + ZONE_THIS.H/2
                Dim zombie_x As Float = Zombie.x + Zombie.W/2
                Dim zombie_y As Float = Zombie.y + Zombie.H/2
Dim a,b,c As Float
   
                a = Abs(zombie_y - zone_y)
                b = Abs(zombie_x - zone_x)
                c = Sqrt(Power(a,2)+Power(b,2))
   
                Dim kp_x,kp_y As Float
                If b > a Then
                    kp_x = 1
                    kp_y = a/b
                else if a > b Then
                    kp_y = 1
                    kp_x = b/a
                Else if a = b Then
                    kp_x = 1
                    kp_y = 1
                End If
                If zombie_x < zone_x And zombie_y < zone_y Then
                    kp_x = Abs(kp_x)
                    kp_y = Abs(kp_y)
                else if zombie_y < zone_y And zombie_x > zone_x Then
                    kp_x = Abs(kp_x) * (-1)
                    kp_y = Abs(kp_y)
                else if zombie_y > zone_y And zombie_x > zone_x Then
                    kp_x = Abs(kp_x) * (-1)
                    kp_y = Abs(kp_y) * (-1)
                else if zombie_y > zone_y And zombie_x < zone_x Then
                    kp_x = Abs(kp_x)
                    kp_y = Abs(kp_y) * (-1)
                End If
                If c <= 2 Then
                    Zombie.POINT = ZONE_THIS.NEXT_POINT
                Else
                    Zombie.x = Zombie.x + (Zombie.SPEED_X*kp_x)
                    Zombie.y = Zombie.y + (Zombie.SPEED_X*kp_y)
                End If

its work fine, but if i try to do it with lgMathVector2, its dont work
B4X:
Dim zone_x, zone_y as float
Dim zone_vector as lgmathvector2
zone_vector.set(zone_x,zone_y)
so WHY zone_x <> zone_vector.x ?
zone_vector x and y values change every time
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…