• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Curious Bouncing

Status
Not open for further replies.
So I am making a physics system and was wondering if anyone could help test it out. It is GUI and I won't change that.

It's meant to follow bowling physics as well able to bounce correctly. I am almost correct, it is just some times it bounces out of bounds oddly.

So far the demo map is only bouncing, still working on perfecting the angles.
Also I am not one for complex equations, rather stick to simple checks if possible.
 

Attachments

  • 2D physics engine.w3x
    28.1 KB · Views: 74
Level 6
Joined
Sep 18, 2013
Messages
79
Unfortunatelly this system leaks, collision code is a bit scaring:vw_death:. Bouncing formulas are simple enough but there is no easy way to detect the angle of the "cliff boundary" in collision point, you should somehow describe it (region+rects or poly-line polygon for ex.)
 
Level 25
Joined
Jul 10, 2006
Messages
3,315
Rather use components for the movement.

Assign two variables to each unit:
real dx (difference/delta x)
real dy (difference/delta y)

Each of these moves the unit along one axis (x and y respectively), so together these two numbers can represent any speed at any angle, instead of having a variable for speed and a variable for the angle.

The advantage of this approach is that it makes collisions very easy to do. If it is colliding horizontally, you change dx. If it is colliding vertically, you change dy.

Use a unit indexer to allow saving these variables per unit.

So the code will look like this:
set id = custom value of unit
Custom script: call SetUnitX(GetUnitX(picked unit) + udg_dx[udg_id])
Custom script: call SetUnitY(GetUnitY(picked unit) + udg_dy[udg_id])

For your starting conditions, just set dx and dy to some random values between -10 and 10.

Now for the fun part: collision detection. We can detect collision by checking pathing at a point, but how do we translate that into an angle?

We check all four directions around the unit.

set point = position of unit
set point2 = point1 offset by (30, 0)
(this will check to the right of the unit)

Now, if we detect point2 to be unpathable, we simply multiply dx (horizontal motion) by -1, and it will reflect.

Follow this method for the other 3 angles, and that's an accurate collision system. It will also work for corners, since you'll trigger both directions.

For reference:
North ... dy+ ... (0, 30) ... set dy = dy * -1
East ... dx+ ... (30, 0) ... set dx = dx * -1
South ... dy- ... (0, -30) ... set dy = dy * -1
West ... dx- ... (-30, 0) ... set dx = dx * -1
 
The condition ZXheld is always 0 currently and the timer is every 0.03 seconds as well there is only ever one unit.

[trigger=]
Move
Events
Time - ZXtimer expires
Conditions
ZXheld Equal to 0
Actions
Set ZXx = (Random real number between -10.00 and 10.00)
Set ZXy = (Random real number between -10.00 and 10.00)
Set ZXp = (Position of ZXball)
-------- North --------
Set ZXp2 = (ZXp offset by (0.00, 30.00))
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
(Terrain pathing at ZXp2 of type Walkability is off) Equal to True
Then - Actions
Set ZXy = (ZXy x -1.00)
Else - Actions
-------- East --------
Set ZXp2 = (ZXp offset by (30.00, 0.00))
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
(Terrain pathing at ZXp2 of type Walkability is off) Equal to True
Then - Actions
Set ZXx = (ZXx x -1.00)
Else - Actions
-------- South --------
Set ZXp2 = (ZXp offset by (0.00, -30.00))
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
(Terrain pathing at ZXp2 of type Walkability is off) Equal to True
Then - Actions
Set ZXy = (ZXy x -1.00)
Else - Actions
-------- West --------
Set ZXp2 = (ZXp offset by (-30.00, 0.00))
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
(Terrain pathing at ZXp2 of type Walkability is off) Equal to True
Then - Actions
Set ZXx = (ZXx x -1.00)
Else - Actions
-------- --------
Custom script: call SetUnitX(udg_ZXball, GetUnitX(udg_ZXball) + udg_ZXx)
Custom script: call SetUnitY(udg_ZXball, GetUnitY(udg_ZXball) + udg_ZXy)

[/trigger]


So what'd I do wrong?
 
Looks like you're re-randomising the components every iteration:

  • Set ZXx = (Random real number between -10.00 and 10.00)
  • Set ZXy = (Random real number between -10.00 and 10.00)
Put those in your initialization trigger, the rest should be fine.

Other than that, just fix your location leaks.

Oh.... Thanks again ruler. :thumbs_up:
 
Status
Not open for further replies.
Top