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
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.
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
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
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
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
...
{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
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
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
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.
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